Class: LinkedList
- Inherits:
-
Object
- Object
- LinkedList
- Includes:
- Enumerable
- Defined in:
- lib/prct06/linkedList.rb
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#deleteFirst ⇒ Any Type
Elemento eliminado.
-
#deleteLast ⇒ Any Type
Elemento eliminado.
-
#each ⇒ Any Type
Bloques seleccionados.
-
#initialize ⇒ LinkedList
constructor
A new instance of LinkedList.
- #Estructura(Globalquerepresentar) ⇒ Object
- #putFirst(value) ⇒ Object
- #putLast(value) ⇒ Object
-
#to_s ⇒ String
String con el contenido de la lista.
Constructor Details
#initialize ⇒ LinkedList
Note:
La clase se inicializa con la cabeza a nil
Returns a new instance of LinkedList
29 30 31 32 33 |
# File 'lib/prct06/linkedList.rb', line 29 def initialize() @head = nil end |
Instance Attribute Details
#M ⇒ Object
21 22 23 |
# File 'lib/prct06/linkedList.rb', line 21 def head @head end |
Instance Method Details
#deleteFirst ⇒ Any Type
Note:
Método que permite eliminar un elemento por el principio
Returns Elemento eliminado
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/prct06/linkedList.rb', line 54 def deleteFirst() #Si la lista no está vacía movemos una posición head #y el prev apuntará a nil: tmp = @head if @head != nil @head = @head.next @head.prev = nil end tmp end |
#deleteLast ⇒ Any Type
Note:
Método que permite eliminar un elemento por el final
Returns Elemento eliminado
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 157 158 159 160 161 162 163 |
# File 'lib/prct06/linkedList.rb', line 127 def deleteLast() #Si la lista no está vacía: if @head != nil #Desplazamos el head hasta la penúltima posición: while @head.next.next != nil tmp = @head @head = @head.next @head.prev = tmp end #Ponemos el siguiente en la penúltima posición a nil: foo = @head.next @head.next = nil #Desplazamos nuevamente el head a la posición inicial: while @head.prev != nil @head = @head.prev end end foo end |
#each ⇒ Any Type
Note:
Función retorna los bloques que se indique por parámetro en la invocación del método
Returns Bloques seleccionados
195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/prct06/linkedList.rb', line 195 def each tmp = @head while tmp != nil do yield tmp.value tmp = tmp.next end end |
#Estructura(Globalquerepresentar) ⇒ Object
25 |
# File 'lib/prct06/linkedList.rb', line 25 Struct.new("Node", :value, :prev, :next) |
#putFirst(value) ⇒ Object
Note:
Método por el cual se introduce un elemento por el principio
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/prct06/linkedList.rb', line 38 def putFirst(value) #Inicializamos un nodo auxiliar con el valor a introducir #Y apuntando como siguiente a head: node = Struct::Node.new(value,nil, @head) #Hacemos que el head tome el comportamiento del nodo creado @head = node end |
#putLast(value) ⇒ Object
Note:
Función que permite añadir un nuevo nodo a la lista por el final
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/prct06/linkedList.rb', line 76 def putLast(value) #Si aún no se ha introducido ningún nodo en la lista, #tanto el head como el tail apuntarán al mismo nodo #de lo contratio, añadiremos un nodo a continuación de tail: if @head != nil #Desplazamos el head hasta la última posición: while @head.next != nil tmp = @head @head = @head.next @head.prev = tmp end #Inicializamos un nodo auxiliar con el valor a introducir: node = Struct::Node.new(value, @head, nil) #Colocamos el node a continuación del último @head.next = node #Desplazamos nuevamente el head a la posición inicial: while @head.prev != nil @head = @head.prev end else #Creamos el nodo inicial: node = Struct::Node.new(value, nil, nil) @head = node end end |
#to_s ⇒ String
Note:
Función que permite mostrar una salida formateada de la lista
Returns String con el contenido de la lista
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/prct06/linkedList.rb', line 168 def to_s() #Para no modificar la estructura de la lista, #utilizaremos una variable temporal, la cual será #desplazada al siguiente nodo en cada iteración: tmp = @head #String que almacenará el contenido a mostrar: aux = "" while tmp != nil aux += "#{tmp.value}\n" tmp = tmp.next end aux end |