Class: Lista
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Instance Method Summary collapse
- #[](index) ⇒ Object
- #add_back(dato) ⇒ Object
- #add_front(dato) ⇒ Object
- #each ⇒ Object
-
#initialize ⇒ Lista
constructor
A new instance of Lista.
- #mostrar ⇒ Object
- #pop_back ⇒ Object
- #pop_front ⇒ Object
Constructor Details
#initialize ⇒ Lista
Returns a new instance of Lista
8 9 10 |
# File 'lib/alimento/lista.rb', line 8 def initialize() @head=@tail=nil end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head
5 6 7 |
# File 'lib/alimento/lista.rb', line 5 def head @head end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail
5 6 7 |
# File 'lib/alimento/lista.rb', line 5 def tail @tail end |
Instance Method Details
#[](index) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/alimento/lista.rb', line 98 def [] (index) i=0 aux=@head while i<index&&aux!=nil do i+=1 aux=aux.next_ end aux end |
#add_back(dato) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/alimento/lista.rb', line 12 def add_back(dato) if dato.instance_of?Alimento node=Nodo.new(dato,nil,nil) @head=node if @head.nil? node.prev_=@tail unless @tail.nil? @tail.next_=node unless @tail.nil? @tail=node end if dato.instance_of?Array i=0 while i<dato.length do node=Nodo.new(dato[i],nil,nil) @head=node if @head.nil? node.prev_=@tail unless @tail.nil? @tail.next_=node unless @tail.nil? @tail=node i+=1 end end end |
#add_front(dato) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/alimento/lista.rb', line 35 def add_front(dato) if dato.instance_of?Alimento node=Nodo.new(dato,nil,nil) @tail = node if @tail.nil? node.next_ = @head unless @head.nil? @head.prev_ = node unless @head.nil? @head = node end if dato.instance_of?Array i=0 while i<dato.length do @tail = node if @tail.nil? node.next_ = @head unless @head.nil? @head.prev_ = node unless @head.nil? @head = node i+=1 end end end |
#each ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/alimento/lista.rb', line 108 def each aux=@head while aux!=nil do yield aux aux=aux.next_ end end |
#mostrar ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/alimento/lista.rb', line 88 def mostrar str= "" aux=@head while aux!=nil do str+="\t"+aux.dato.to_s+"\n" aux=aux.next_ end str end |
#pop_back ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/alimento/lista.rb', line 56 def pop_back if (@tail==@head)&&(@tail==nil) return nil elsif @tail==@head aux=@tail.dup @tail=@head=nil return aux else aux=@tail.dup @tail=@tail.prev_ @tail.next_=nil aux.prev_=nil return aux end end |
#pop_front ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/alimento/lista.rb', line 72 def pop_front if (@tail==@head)&&(@tail==nil) return nil elsif @tail==@head aux=@head.dup @head=@tail=nil return aux else aux=@head.dup @head=@head.next_ @head.prev_=nil aux.next_=nil return aux end end |