Class: List
Overview
Esta clase implementa una lista doblemente enlazada Se ha incluido el mixin Enumerable
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Getters de las distintas variables de instancia.
-
#num_elem ⇒ Object
readonly
Getters de las distintas variables de instancia.
-
#tale ⇒ Object
readonly
Getters de las distintas variables de instancia.
Instance Method Summary collapse
-
#each ⇒ Object
Se incluye el metodo del mixin Enumerable Se define como una iteración cada uno de los valores de los nodos.
- #each_sort ⇒ Object
-
#extract_head ⇒ Object
Método para extraer el primer elemento (el head).
-
#extract_tale ⇒ Object
Método para extraer el último elemento (el tale).
-
#for_sort ⇒ Object
Se usan los bucles for para ordenar la lista, esta programación NO es funcional.
-
#initialize(node) ⇒ List
constructor
Con el primer valor que nos pasen creamos el nodo inicial que es head y tale de las lista.
-
#insert_head(node) ⇒ Object
Metodo para insertar un valor por el head.
-
#insert_mto_head(node_array) ⇒ Object
Metodo para insertar más de un valor por la head.
-
#insert_mto_tale(node_array) ⇒ Object
Metodo para insertar más de un valor por la cola.
-
#insert_tale(node) ⇒ Object
Metodo para insertar un valor por la cola.
-
#to_s ⇒ Object
Función to_string, que solo muestra el nombre de los valores (los alimentos).
Constructor Details
#initialize(node) ⇒ List
Con el primer valor que nos pasen creamos el nodo inicial que es head y tale de las lista. La lista ya tiene un elemento por tanto el numero de elementos es 1
20 21 22 23 24 |
# File 'lib/Alimento/List.rb', line 20 def initialize (node) @head = Node.new(node,nil,nil) @tale = @head @num_elem = 1 end |
Instance Attribute Details
#head ⇒ Object (readonly)
Getters de las distintas variables de instancia
16 17 18 |
# File 'lib/Alimento/List.rb', line 16 def head @head end |
#num_elem ⇒ Object (readonly)
Getters de las distintas variables de instancia
16 17 18 |
# File 'lib/Alimento/List.rb', line 16 def num_elem @num_elem end |
#tale ⇒ Object (readonly)
Getters de las distintas variables de instancia
16 17 18 |
# File 'lib/Alimento/List.rb', line 16 def tale @tale end |
Instance Method Details
#each ⇒ Object
Se incluye el metodo del mixin Enumerable Se define como una iteración cada uno de los valores de los nodos
95 96 97 98 99 100 101 102 103 |
# File 'lib/Alimento/List.rb', line 95 def each aux = Node.new aux = @head tam = @num_elem for i in (1..tam) yield aux.value aux = aux.next end end |
#each_sort ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/Alimento/List.rb', line 125 def each_sort sort_list = [@head.value] self.each_with_index do |x, pos| if(pos != 0) sort_list.each_with_index do |y, index| if (index == (sort_list.size - 1)) if(x < y) sort_list.insert(index, x) break else sort_list.push(x) break end else if(x < y) sort_list.insert(index, x) break end end end end end return sort_list end |
#extract_head ⇒ Object
Método para extraer el primer elemento (el head)
83 84 85 86 87 88 89 90 91 |
# File 'lib/Alimento/List.rb', line 83 def extract_head extracted_node = Node.new extracted_node = @head @head = @head.next @head.prev = nil extracted_node.next = nil @num_elem -= 1 return extracted_node end |
#extract_tale ⇒ Object
Método para extraer el último elemento (el tale)
72 73 74 75 76 77 78 79 80 |
# File 'lib/Alimento/List.rb', line 72 def extract_tale extracted_node = Node.new extracted_node = @tale @tale = @tale.prev @tale.next = nil extracted_node.prev = nil @num_elem -= 1 return extracted_node end |
#for_sort ⇒ Object
Se usan los bucles for para ordenar la lista, esta programación NO es funcional
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/Alimento/List.rb', line 106 def for_sort sort_list = [@head.value] auxnode = @head for i in(1...@num_elem) auxnode = auxnode.next for j in(0..sort_list.size) if(j == sort_list.size) sort_list.push(auxnode.value) else if(auxnode.value < sort_list[j]) sort_list.insert(j,auxnode.value) break end end end end return sort_list end |
#insert_head(node) ⇒ Object
Metodo para insertar un valor por el head
50 51 52 53 54 55 |
# File 'lib/Alimento/List.rb', line 50 def insert_head(node) insertnode = Node.new(node,@head,nil) @head.prev = insertnode @head = insertnode @num_elem += 1 end |
#insert_mto_head(node_array) ⇒ Object
Metodo para insertar más de un valor por la head
65 66 67 68 69 |
# File 'lib/Alimento/List.rb', line 65 def insert_mto_head(node_array) #insert More Than One in head node_array.each do |food| insert_head(food) end end |
#insert_mto_tale(node_array) ⇒ Object
Metodo para insertar más de un valor por la cola
58 59 60 61 62 |
# File 'lib/Alimento/List.rb', line 58 def insert_mto_tale(node_array) #insert More Than One in tale node_array.each do |food| insert_tale(food) end end |
#insert_tale(node) ⇒ Object
Metodo para insertar un valor por la cola
42 43 44 45 46 47 |
# File 'lib/Alimento/List.rb', line 42 def insert_tale(node) insertnode = Node.new(node,nil,@tale) @tale.next = insertnode @tale = insertnode @num_elem += 1 end |
#to_s ⇒ Object
Función to_string, que solo muestra el nombre de los valores (los alimentos)
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/Alimento/List.rb', line 27 def to_s aux="{" node = Node.new node = @head while (node != tale) do aux += node.value.name aux += ", " node = node.next end aux += node.value.name aux += "}" return aux end |