Class: Lista
- Inherits:
-
Object
- Object
- Lista
- Includes:
- Comparable, Enumerable
- Defined in:
- lib/prct06/lista.rb
Overview
Esta clase representa una lista doblemente enlazada
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Esto permite la lectura de la cola y la cabeza de la lista.
-
#node ⇒ Object
Returns the value of attribute node.
-
#tail ⇒ Object
readonly
Esto permite la lectura de la cola y la cabeza de la lista.
Instance Method Summary collapse
-
#each ⇒ Object
Método para que la clase sea Enumerable.
-
#empty? ⇒ bool
Comprueba si la lista está vacía.
-
#extract_head ⇒ Object
Extrae un nodo por la cabeza de la lista.
-
#initialize ⇒ Lista
constructor
A new instance of Lista.
-
#insert_tail(value) ⇒ Object
Inserta un nodo por la cola.
- #sz ⇒ Object
- #to_array_each ⇒ Object
- #to_array_for ⇒ Object
-
#to_s ⇒ Object
Convierte a un string la lista.
Constructor Details
#initialize ⇒ Lista
Returns a new instance of Lista.
14 15 16 17 |
# File 'lib/prct06/lista.rb', line 14 def initialize @head = nil @tail = nil end |
Instance Attribute Details
#head ⇒ Object (readonly)
Esto permite la lectura de la cola y la cabeza de la lista
8 9 10 |
# File 'lib/prct06/lista.rb', line 8 def head @head end |
#node ⇒ Object
Returns the value of attribute node.
9 10 11 |
# File 'lib/prct06/lista.rb', line 9 def node @node end |
#tail ⇒ Object (readonly)
Esto permite la lectura de la cola y la cabeza de la lista
8 9 10 |
# File 'lib/prct06/lista.rb', line 8 def tail @tail end |
Instance Method Details
#each ⇒ Object
Método para que la clase sea Enumerable
66 67 68 69 70 71 72 |
# File 'lib/prct06/lista.rb', line 66 def each aux = @head while aux != nil do yield aux.value aux = aux.next end end |
#empty? ⇒ bool
Comprueba si la lista está vacía
22 23 24 |
# File 'lib/prct06/lista.rb', line 22 def empty? return (head == nil) && (tail == nil) end |
#extract_head ⇒ Object
Extrae un nodo por la cabeza de la lista
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/prct06/lista.rb', line 42 def extract_head aux = @head.value if(@head == @tail) @head,@tail = nil else @head = @head[:next] @head[:prev] = nil end return aux end |
#insert_tail(value) ⇒ Object
Inserta un nodo por la cola
29 30 31 32 33 34 35 36 37 |
# File 'lib/prct06/lista.rb', line 29 def insert_tail(value) if(empty?) @head = Node.new(value,nil,nil) @tail = @head else @tail[:next] = Node.new(value,nil,@tail) @tail = @tail[:next] end end |
#sz ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/prct06/lista.rb', line 74 def sz cont = 0 aux = @head while(aux != nil) do cont = cont + 1 aux=aux.next end return cont end |
#to_array_each ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/prct06/lista.rb', line 94 def to_array_each arr = [] aux = @head for i in self do arr.push(aux.value) aux = aux.next end arr.ordenar_each end |
#to_array_for ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/prct06/lista.rb', line 84 def to_array_for arr = [] aux = @head for i in self do arr.push(aux.value) aux = aux.next end arr.buclesfor end |
#to_s ⇒ Object
Convierte a un string la lista
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/prct06/lista.rb', line 54 def to_s string = "(" aux = @head while(aux != nil) do string += "#{aux.to_s}" @aux = @aux.next end string += ")" return string end |