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 ⇒ 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
#initialize ⇒ Lista
Constructor
9 10 11 |
# File 'lib/pract/list.rb', line 9 def initialize @head = @tail = nil end |
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
39 40 41 42 43 44 45 46 47 |
# File 'lib/pract/list.rb', line 39 def add_first (val) if (@head!= nil) point = @head point.prev = Nodo.new(nil, val, point) @head = point.prev else @head = @tail = Nodo.new(nil, val, nil) end end |
#add_last(val) ⇒ Object
Add val as last element
29 30 31 32 33 34 35 36 37 |
# File 'lib/pract/list.rb', line 29 def add_last (val) if (@head!= nil) point = @tail point.next = Nodo.new(point, val, nil) @tail = point.next else @head = @tail = Nodo.new(nil, val, nil) end end |
#each {|point.val| ... } ⇒ Object
Make list enumerable
114 115 116 117 118 119 120 121 |
# File 'lib/pract/list.rb', line 114 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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pract/list.rb', line 60 def remove (val) if (@head != nil) if(@head == @tail) && (@head.val == val) @head = nil @tail = nil elsif (@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 end |
#remove_first ⇒ Object
Remove the first element
49 50 51 52 |
# File 'lib/pract/list.rb', line 49 def remove_first point = @head @head = point.next end |
#remove_last ⇒ Object
Remove the last element
54 55 56 57 |
# File 'lib/pract/list.rb', line 54 def remove_last point = @tail @tail = point.prev end |
#search(val) ⇒ Object
Returns position of val[from 1] or nil is not found.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pract/list.rb', line 97 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.
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pract/list.rb', line 86 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.
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pract/list.rb', line 15 def to_s elements = "[" point = @head while point != @tail elements = elements + point.val.to_s elements = elements + "], [" point = point.next end if (@head != nil) elements = elements + point.val.to_s end elements = elements + "]" end |