Class: Lista

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/food/list.rb

Overview

Esta clase permite representar una lista doblemente enlazada.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#firstObject (readonly)

Returns the value of attribute first.



19
20
21
# File 'lib/food/list.rb', line 19

def first
  @first
end

#lastObject (readonly)

Returns the value of attribute last.



19
20
21
# File 'lib/food/list.rb', line 19

def last
  @last
end

Instance Method Details

#eachObject

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_firstObject

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_lastObject

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

#seleccionObject



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_eachObject



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