class BiblioRefs::List
Clase para representar una lista doblemente enlazada.
Attributes
head[RW]
tail[RW]
Public Class Methods
new(*nodo)
click to toggle source
Constructor de la clase. Recibe un número indefinido de nodos y los añade a la lista.
# File lib/biblio_refs/list.rb, line 13 def initialize(*nodo) @tail = @head = Nodo.new(nodo[0], nil) if nodo.size > 1 nodo.shift push(*nodo) end end
Public Instance Methods
each() { |aux| ... }
click to toggle source
Método para hacer la clase Enumerable. Hace ‘yield’ a todos los elementos de la lista.
# File lib/biblio_refs/list.rb, line 23 def each aux=@head while aux[:next] yield aux[:value] aux=aux[:next] end yield aux[:value] end
pop()
click to toggle source
Método que elimina el elemento a la cabeza de la lista y lo devuelve.
# File lib/biblio_refs/list.rb, line 33 def pop nodo = @head @head = @head[:next] nodo[:value] end
push(*nodo)
click to toggle source
Método que añade uno o más nodos a la lista.
# File lib/biblio_refs/list.rb, line 40 def push(*nodo) aux = @head nodo.each do |n| while aux[:next] do aux = aux[:next] end @tail = aux[:next] = Nodo.new(n, nil, aux) end end
to_s()
click to toggle source
Método que muestra todos los elementos de la lista correctamente formateados.
# File lib/biblio_refs/list.rb, line 51 def to_s aux = @head string = "Lista: " while aux[:next] do string += "#{aux[:value]}" + " -> " aux = aux[:next] end string += "#{aux[:value]}" end