Class: Lista
Overview
Clase lista, que representa una lista simplemente enlazada de nodos
Instance Method Summary collapse
-
#addn(nodo2) ⇒ Object
Coloca un nuevo nodo como cabeza (push).
-
#deln ⇒ Object
Elimina el nodo cabeza (pop).
-
#each(&block) ⇒ Object
Método que sirve para iterar por todos los nodos de la lista.
-
#head ⇒ Object
Devuelve el nodo que esta en cabeza.
-
#initialize ⇒ Lista
constructor
Método constructor.
-
#ordenar ⇒ Object
Método que sirve para ordenar la lista.
-
#tail ⇒ Object
Devuelve el nodo que está al final.
-
#to_s ⇒ Object
Devuelve la lista como String (usado en testeo).
Constructor Details
#initialize ⇒ Lista
Método constructor
20 21 22 23 |
# File 'lib/exam/lista.rb', line 20 def initialize @head = nil @tail = nil end |
Instance Method Details
#addn(nodo2) ⇒ Object
Coloca un nuevo nodo como cabeza (push)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/exam/lista.rb', line 26 def addn(nodo2) #coloca un nuevo nodo como cabeza !push nodo = Nodo.new() if (nodo2.class == Nodo) nodo = nodo2 else nodo.value = nodo2 end nodo.next = @head if (!@head.nil? && @head.next.nil?) @tail = @head @tail.next = nil end if (!head.nil?) @head.previus = nodo end nodo.previus = nil #si en vez de nil ponemos tail, es una lsita circular, es decir unimos head con tail en previus @head = nodo #@tail.next = @head añadir esto para que la lista sea circular tail apuntara a head como siguiente end |
#deln ⇒ Object
Elimina el nodo cabeza (pop)
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/exam/lista.rb', line 55 def deln aux = @head if (@head.next.nil?) @tail = nil @head = nil else @head = @head.next @head.previus = @tail end aux.value end |
#each(&block) ⇒ Object
Método que sirve para iterar por todos los nodos de la lista
78 79 80 81 82 83 84 85 |
# File 'lib/exam/lista.rb', line 78 def each(&block) current_node = @head while current_node != nil block.call(current_node) current_node = current_node.next end end |
#head ⇒ Object
Devuelve el nodo que esta en cabeza
68 69 70 |
# File 'lib/exam/lista.rb', line 68 def head @head end |
#ordenar ⇒ Object
Método que sirve para ordenar la lista.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/exam/lista.rb', line 88 def ordenar() contador = (self.count) contador = contador-1 aux = self.sort @head = nil @tail = aux[0] while (contador >= 0) do self.addn(aux[contador]) contador = contador-1 end end |
#tail ⇒ Object
Devuelve el nodo que está al final
73 74 75 |
# File 'lib/exam/lista.rb', line 73 def tail @tail end |
#to_s ⇒ Object
Devuelve la lista como String (usado en testeo)
102 103 104 105 106 107 |
# File 'lib/exam/lista.rb', line 102 def to_s #para testear self.each do |l| print l.value.to_s + " -> " end puts "" end |