Class: Lista
Instance Attribute Summary collapse
-
#contador ⇒ Object
Returns the value of attribute contador.
-
#head ⇒ Object
Returns the value of attribute head.
-
#tail ⇒ Object
Returns the value of attribute tail.
Instance Method Summary collapse
- #each ⇒ Object
- #empty ⇒ Object
- #extract_head ⇒ Object
- #extract_tail ⇒ Object
-
#initialize ⇒ Lista
constructor
A new instance of Lista.
- #insert_head(nodo) ⇒ Object
- #insert_tail(nodo) ⇒ Object
- #insert_varios(nodos) ⇒ Object
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#contador ⇒ Object
Returns the value of attribute contador.
5 6 7 |
# File 'lib/prct11/lista.rb', line 5 def contador @contador end |
#head ⇒ Object
Returns the value of attribute head.
5 6 7 |
# File 'lib/prct11/lista.rb', line 5 def head @head end |
#tail ⇒ Object
Returns the value of attribute tail.
5 6 7 |
# File 'lib/prct11/lista.rb', line 5 def tail @tail end |
Instance Method Details
#each ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/prct11/lista.rb', line 98 def each actual = @tail while actual != nil yield actual.value actual = actual.siguiente end end |
#empty ⇒ Object
107 108 109 |
# File 'lib/prct11/lista.rb', line 107 def empty return contador==0 end |
#extract_head ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/prct11/lista.rb', line 45 def extract_head aux = @head @head = @head[:previo] if @head != nil #Si head no es nil ponemos el siguiente de head como nil para desenlazar la lista @head[:siguiente] = nil else #Si el elemento que se extrae es el unico de la lista entonces ponemos todo a nil como en initialize @tail = nil end @contador = @contador - 1 return aux.value end |
#extract_tail ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/prct11/lista.rb', line 60 def extract_tail if @contador == 1 return extract_head else actual = @tail @tail = @tail[:siguiente] if @tail != nil @tail[:previo] = nil end @contador = @contador - 1 return actual.value end end |
#insert_head(nodo) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/prct11/lista.rb', line 15 def insert_head(nodo) if empty == true #Si la lista esta vacia insertamos el elemento en la lista vacia nodo[:previo] = @tail nodo[:siguiente] = @head @head = nodo @tail = nodo else #Si no esta vacia insertamos por el head nodo[:previo] = @head nodo[:siguiente] = nil @head[:siguiente] = nodo @head = nodo end @contador = @contador + 1 #Aumentamos variable que indica tamaño de la lista end |
#insert_tail(nodo) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/prct11/lista.rb', line 32 def insert_tail(nodo) if empty #Si esta vacia insertamos en lista vacia insert_head(nodo) else #Si no insertamos por el tail nodo[:siguiente] = @tail nodo[:previo] = nil @tail[:previo] = nodo @tail = nodo @contador = @contador + 1 end end |
#insert_varios(nodos) ⇒ Object
76 77 78 79 80 |
# File 'lib/prct11/lista.rb', line 76 def insert_varios (nodos) for i in 0..(nodos.length - 1) insert_head(nodos[i]) end end |
#to_s ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/prct11/lista.rb', line 83 def to_s aux = @tail if empty != true string= "\n****** MENU DIETETICO ******" while aux.siguiente != nil string+= "\n\n#{aux.value}" aux = aux.siguiente end string += "\n\n#{aux.value}" return string else return "Lista vacia" end end |