Class: List
Overview
Es una clase que representa una lista doblemente enlazada usando la clase Node
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Devuelve el nodo cabeza de la Lista.
-
#tail ⇒ Object
readonly
Devuelve el nodo cola de la Lista.
Instance Method Summary collapse
-
#each ⇒ Object
Definición del método each necesario para que la clase sea enumerable.
-
#extract_head ⇒ Object
Extrae el nodo de la cabeza, el nodo siguiente pasa a ser la siguiente cabeza y devuelve el valor de la antigua cabeza.
-
#extract_tail ⇒ Object
Extrae el nodo de la cola, el anterior pasa a ser la nueva cola y luego devuelve el valor de la cola antigua.
-
#initialize(head_, tail_) ⇒ List
constructor
Construye una lista doblemente enlazada tras pasarle dos parámetros, head y tail.
-
#insert_head(value) ⇒ Object
Inserta un nuevo nodo por la cabeza y la cabeza antigua pasa a ser el next.
-
#value_head ⇒ Object
Devuelve el valor de la cabeza sin extraerla.
-
#value_tail ⇒ Object
Devuelve el valor de la cola sin extraerla.
Constructor Details
#initialize(head_, tail_) ⇒ List
Construye una lista doblemente enlazada tras pasarle dos parámetros, head y tail
21 22 23 24 |
# File 'lib/p6/list.rb', line 21 def initialize(head_,tail_) @head = head_ @tail = tail_ end |
Instance Attribute Details
#head ⇒ Object (readonly)
Devuelve el nodo cabeza de la Lista
15 16 17 |
# File 'lib/p6/list.rb', line 15 def head @head end |
#tail ⇒ Object (readonly)
Devuelve el nodo cola de la Lista
15 16 17 |
# File 'lib/p6/list.rb', line 15 def tail @tail end |
Instance Method Details
#each ⇒ Object
Definición del método each necesario para que la clase sea enumerable
77 78 79 80 81 82 83 |
# File 'lib/p6/list.rb', line 77 def each node=@head while(node!=nil) do yield node.value node = node.next end end |
#extract_head ⇒ Object
Extrae el nodo de la cabeza, el nodo siguiente pasa a ser la siguiente cabeza y devuelve el valor de la antigua cabeza.
41 42 43 44 45 46 |
# File 'lib/p6/list.rb', line 41 def extract_head temp = @head @head = @head.next @head.prev = nil return temp end |
#extract_tail ⇒ Object
Extrae el nodo de la cola, el anterior pasa a ser la nueva cola y luego devuelve el valor de la cola antigua
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/p6/list.rb', line 57 def extract_tail if @tail!=nil then temp = @tail @tail = @tail.prev if @tail!=nil then @tail.next = nil end return temp end end |
#insert_head(value) ⇒ Object
Inserta un nuevo nodo por la cabeza y la cabeza antigua pasa a ser el next
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/p6/list.rb', line 28 def insert_head(value) if (head==nil) then @head=Node.new(value,nil,nil) @tail=@head else temp=Node.new(value,@head,nil) @head.prev=temp @head=temp end end |
#value_head ⇒ Object
Devuelve el valor de la cabeza sin extraerla
50 51 52 53 |
# File 'lib/p6/list.rb', line 50 def value_head temp = @head return temp end |
#value_tail ⇒ Object
Devuelve el valor de la cola sin extraerla
70 71 72 73 |
# File 'lib/p6/list.rb', line 70 def value_tail temp = @tail return temp end |