Class: Lista
Overview
Esta clase permite representar una lista doblemente enlazada.
Instance Attribute Summary collapse
-
#first ⇒ Object
readonly
Returns the value of attribute first.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
Instance Method Summary collapse
-
#each ⇒ Object
Método each que hace un yield de cada elemento de la lista.
-
#initialize(first, last) ⇒ Lista
constructor
Constructor al que se le pasa el primer y ultimo nodo de una lista.
-
#insertEnd(*args) ⇒ Object
Método para insertar uno o varios elementos al final de la lista.
-
#pop_first ⇒ Object
Método para extraer el primer nodo de la lista.
-
#pop_last ⇒ Object
Método para extraer el último nodo de la lista.
- #seleccion ⇒ Object
- #seleccion_each ⇒ Object
Constructor Details
#initialize(first, last) ⇒ Lista
Constructor al que se le pasa el primer y ultimo nodo de una lista.
22 23 24 25 |
# File 'lib/food/list.rb', line 22 def initialize(first, last) @first = first @last = last end |
Instance Attribute Details
#first ⇒ Object (readonly)
Returns the value of attribute first.
19 20 21 |
# File 'lib/food/list.rb', line 19 def first @first end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
19 20 21 |
# File 'lib/food/list.rb', line 19 def last @last end |
Instance Method Details
#each ⇒ Object
Método each que hace un yield de cada elemento de la lista.
54 55 56 57 58 59 60 |
# File 'lib/food/list.rb', line 54 def each node = self.first while node != nil do yield node.value node = node.next end end |
#insertEnd(*args) ⇒ Object
Método para insertar uno o varios elementos al final de la lista.
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/food/list.rb', line 28 def insertEnd *args args.each do |arg_item| node = Node.new(arg_item, nil, nil) if @last == nil @last = @first = node else @last.next = node node.prev = @last @last = node end end end |
#pop_first ⇒ Object
Método para extraer el primer nodo de la lista.
42 43 44 45 |
# File 'lib/food/list.rb', line 42 def pop_first @first = @first.next @first.prev = nil end |
#pop_last ⇒ Object
Método para extraer el último nodo de la lista.
48 49 50 51 |
# File 'lib/food/list.rb', line 48 def pop_last @last = @last.prev @last.next = nil end |
#seleccion ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/food/list.rb', line 62 def seleccion vector = self.map { |x| x } for i in 0..self.count-1 aux = vector[i] c = i for j in i + 1..self.count-1 if aux > vector[j] aux = vector[j] c = j end end vector[c] = vector[i] vector[i] = aux end vector end |
#seleccion_each ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/food/list.rb', line 81 def seleccion_each vector = self.map { |x| x } indice=0 vector.each do |x| aux = x c=indice indice2=indice+1 vector[indice2..vector.length-1].each do |y| if aux > y aux = y c = indice2 end indice2+=1 end vector[c] = x vector[indice] = aux indice+=1 end vector end |