Class: List
Overview
Esta clase permite representar una lista doblemente enlazada con un head y un tail. Contiene metodos para el manejo de la misma facilmente. Se ha incluido el mixin Enumerable.
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Permite acceder a los atributos de la clase en forma de lectura.
-
#tail ⇒ Object
readonly
Permite acceder a los atributos de la clase en forma de lectura.
Instance Method Summary collapse
- #bucleEach ⇒ Object
- #bucleFor! ⇒ Object
-
#each ⇒ Object
Se incluye el metodo del mixin Enumerable Se define como una iteraciĆ³n sobre los nodos de la lista.
-
#empty ⇒ Object
Comprueba si la lista esta vacia y devuelve un boolean.
-
#extract_head ⇒ Object
Extrae el elemento que se encuentre en la cabeza de la lista.
-
#extract_tail ⇒ Object
Extrae el elemento que se encuentre en la cola de la lista.
- #extraerNodo(lista, valor) ⇒ Object
-
#initialize ⇒ List
constructor
Se asignan los valores de la cabeza y cola de la lista, inicialmente no hay nada.
-
#insert_head(value) ⇒ Object
Inserta un valor por la cabeza a la lista.
-
#insert_tail(value) ⇒ Object
Inserta un valor por la cola a la lista.
-
#insert_values(vec) ⇒ Object
Permite insertar varios valores al mismo tiempo con una sola llamada.
- #setHead(nodo) ⇒ Object
- #setTail(nodo) ⇒ Object
- #swap(nodoActual, nodoSiguiente, nodoFinal) ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize ⇒ List
Se asignan los valores de la cabeza y cola de la lista, inicialmente no hay nada.
17 18 19 20 |
# File 'lib/Alimento/List.rb', line 17 def initialize @head = nil @tail = nil end |
Instance Attribute Details
#head ⇒ Object (readonly)
Permite acceder a los atributos de la clase en forma de lectura.
12 13 14 |
# File 'lib/Alimento/List.rb', line 12 def head @head end |
#tail ⇒ Object (readonly)
Permite acceder a los atributos de la clase en forma de lectura.
12 13 14 |
# File 'lib/Alimento/List.rb', line 12 def tail @tail end |
Instance Method Details
#bucleEach ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/Alimento/List.rb', line 158 def bucleEach copia = self.dup copiaVariable = copia.dup listafinal = List.new copia.each do |m| aux = copiaVariable.tail.value copiaVariable.each do |elem| if (aux > elem) aux = elem end end copiaVariable = extraerNodo(copiaVariable, aux) listafinal.insert_head(aux) end listafinal end |
#bucleFor! ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/Alimento/List.rb', line 94 def bucleFor! nodoActual = @tail nodoSiguiente = nodoActual.next nodoFinal = @head while nodoFinal != @tail while nodoSiguiente != nil if (nodoActual.value > nodoSiguiente.value) swap(nodoActual, nodoSiguiente, nodoFinal) end nodoActual = nodoSiguiente nodoSiguiente = nodoSiguiente.next end nodoActual = @tail nodoSiguiente = nodoActual.next nodoFinal = nodoFinal.prev end end |
#each ⇒ Object
Se incluye el metodo del mixin Enumerable Se define como una iteraciĆ³n sobre los nodos de la lista.
86 87 88 89 90 91 92 |
# File 'lib/Alimento/List.rb', line 86 def each nodoActual = @tail while nodoActual != nil yield nodoActual.value nodoActual = nodoActual.next end end |
#empty ⇒ Object
Comprueba si la lista esta vacia y devuelve un boolean.
51 52 53 54 55 56 57 |
# File 'lib/Alimento/List.rb', line 51 def empty if (@head == nil) true else false end end |
#extract_head ⇒ Object
Extrae el elemento que se encuentre en la cabeza de la lista.
67 68 69 70 71 72 73 |
# File 'lib/Alimento/List.rb', line 67 def extract_head aux = @head if (!empty()) @head = @head.prev end return aux end |
#extract_tail ⇒ Object
Extrae el elemento que se encuentre en la cola de la lista.
76 77 78 79 80 81 82 |
# File 'lib/Alimento/List.rb', line 76 def extract_tail aux = @tail if (!empty()) @tail = @tail.next end return aux end |
#extraerNodo(lista, valor) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/Alimento/List.rb', line 176 def extraerNodo(lista, valor) nodo = lista.tail while nodo != nil if (nodo.value == valor) checkTail = false checkHead = false nodoTmpIzq = nodo.prev nodoTmpDch = nodo.next if (nodo == lista.tail) checkTail = true end if (nodo == lista.head) checkHead = true end if(nodoTmpIzq != nil) nodoTmpIzq.next = nodo.next end if(nodoTmpDch != nil) nodoTmpDch.prev = nodo.prev end if (checkHead) lista.setHead(nodo.prev) end if (checkTail) lista.setTail(nodo.next) end end nodo = nodo.next end lista end |
#insert_head(value) ⇒ Object
Inserta un valor por la cabeza a la lista.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/Alimento/List.rb', line 23 def insert_head (value) newNode = Node.new(value, nil, nil) if (empty()) @head = newNode @tail = newNode else @head.next = newNode newNode.prev = @head @head = newNode end end |
#insert_tail(value) ⇒ Object
Inserta un valor por la cola a la lista.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/Alimento/List.rb', line 37 def insert_tail (value) newNode = Node.new(value, nil, nil) if (empty) @head = newNode @tail = newNode else @tail.prev = newNode newNode.next = @tail @tail = newNode end end |
#insert_values(vec) ⇒ Object
Permite insertar varios valores al mismo tiempo con una sola llamada.
60 61 62 63 64 |
# File 'lib/Alimento/List.rb', line 60 def insert_values(vec) vec.each do |i| insert_head(i) end end |
#setHead(nodo) ⇒ Object
226 227 228 |
# File 'lib/Alimento/List.rb', line 226 def setHead(nodo) @head = nodo end |
#setTail(nodo) ⇒ Object
222 223 224 |
# File 'lib/Alimento/List.rb', line 222 def setTail(nodo) @tail = nodo end |
#swap(nodoActual, nodoSiguiente, nodoFinal) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/Alimento/List.rb', line 112 def swap (nodoActual, nodoSiguiente, nodoFinal) checkerTail = false checkerHead = false if (nodoActual == @tail) checkerTail = true end if (nodoSiguiente == @head) checkerHead = true end nodoTmpIzq = nodoActual.prev nodoTmpDch = nodoSiguiente.next nodoActual = nodoSiguiente nodoSiguiente = nodoSiguiente.prev nodoActual.next = nodoSiguiente nodoSiguiente.prev = nodoActual if(nodoTmpIzq != nil) nodoTmpIzq.next = nodoActual nodoActual.prev = nodoTmpIzq else nodoActual.prev = nil end if(nodoTmpDch != nil) nodoTmpDch.prev = nodoSiguiente nodoSiguiente.next = nodoTmpDch else nodoSiguiente.next = nil end if (checkerTail) @tail = nodoActual end if (checkerHead) @head = nodoSiguiente nodoFinal = nodoSiguiente end end |
#write ⇒ Object
214 215 216 217 218 219 220 |
# File 'lib/Alimento/List.rb', line 214 def write nodo = @tail while nodo != nil p nodo.value.to_s nodo = nodo.next end end |