Class: Lista
Instance Attribute Summary collapse
-
#head ⇒ Object
Returns the value of attribute head.
-
#tail ⇒ Object
Returns the value of attribute tail.
Instance Method Summary collapse
-
#add_first(val) ⇒ Object
Add val as first element.
-
#add_last(val) ⇒ Object
Add val as last element.
-
#each {|point.val| ... } ⇒ Object
Make list enumerable.
-
#initialize(val) ⇒ Lista
constructor
Constructor.
-
#remove(val) ⇒ Object
Remove an element.
-
#remove_first ⇒ Object
Remove the first element.
-
#remove_last ⇒ Object
Remove the last element.
-
#search(val) ⇒ Object
Position of val[from 1] or nil is not found.
-
#sz ⇒ Object
Size of element.
-
#to_s ⇒ Object
String format.
Constructor Details
Instance Attribute Details
#head ⇒ Object
Returns the value of attribute head
5 6 7 |
# File 'lib/pract/list.rb', line 5 def head @head end |
#tail ⇒ Object
Returns the value of attribute tail
5 6 7 |
# File 'lib/pract/list.rb', line 5 def tail @tail end |
Instance Method Details
#add_first(val) ⇒ Object
Add val as first element
35 36 37 38 39 |
# File 'lib/pract/list.rb', line 35 def add_first (val) point = @head point.prev = Nodo.new(nil, val, point) @head = point.prev end |
#add_last(val) ⇒ Object
Add val as last element
29 30 31 32 33 |
# File 'lib/pract/list.rb', line 29 def add_last (val) point = @tail point.next = Nodo.new(point, val, nil) @tail = point.next end |
#each {|point.val| ... } ⇒ Object
Make list enumerable
101 102 103 104 105 106 107 108 |
# File 'lib/pract/list.rb', line 101 def each point = @head while(point != @tail) do yield point.val point = point.next end yield(point.val) end |
#remove(val) ⇒ Object
Remove an element
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/pract/list.rb', line 52 def remove (val) if (@head.val == val) remove_first elsif (@tail.val == val) remove_last else point = @head while (point.next != nil) && (point.val != val) point = point.next end if point != @tail before = point.prev after = point.next before.next = after after.prev = before point.prev = nil point.next = nil end end end |
#remove_first ⇒ Object
Remove the first element
41 42 43 44 |
# File 'lib/pract/list.rb', line 41 def remove_first point = @head @head = point.next end |
#remove_last ⇒ Object
Remove the last element
46 47 48 49 |
# File 'lib/pract/list.rb', line 46 def remove_last point = @tail @tail = point.prev end |
#search(val) ⇒ Object
Returns position of val[from 1] or nil is not found
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/pract/list.rb', line 84 def search(val) num = 1 point = @head while(point != @tail) if(point.val == val) return num else num = num+1 point = point.next end end if @tail.val == val return num else return 0 end end |
#sz ⇒ Object
Returns size of element
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pract/list.rb', line 73 def sz size_list = 0 point = @head while(point != tail) size_list = size_list + 1 point = point.next end size_list = size_list+1 return size_list end |
#to_s ⇒ Object
Returns string format
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pract/list.rb', line 17 def to_s elements = "[" point = @head while point != @tail elements = elements + point.val.to_s elements = elements + "], [" point = point.next end elements = elements + point.val.to_s elements = elements + "]" end |