Class: LDE

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/Alimento/lista.rb

Overview

Clase LDE que es Una lista doblemente enlazada

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLDE

Returns a new instance of LDE.



9
10
11
12
13
# File 'lib/Alimento/lista.rb', line 9

def initialize
    @head = nil
    @tail = nil
    @size = 0
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



6
7
8
# File 'lib/Alimento/lista.rb', line 6

def head
  @head
end

#sizeObject (readonly)

Obtienes el tama�o de la lista



16
17
18
# File 'lib/Alimento/lista.rb', line 16

def size
  @size
end

#tailObject (readonly)

Returns the value of attribute tail.



6
7
8
# File 'lib/Alimento/lista.rb', line 6

def tail
  @tail
end

Instance Method Details

#eachObject

Funcion para el modulo enumerable



21
22
23
24
25
26
27
# File 'lib/Alimento/lista.rb', line 21

def each
    tmp = @head
    while(tmp != nil)
        yield tmp
        tmp = tmp[:next]
    end
end

#list_to_arrayObject



83
84
85
# File 'lib/Alimento/lista.rb', line 83

def list_to_array()
    self.map { |e| e[:value] }
end

#ord_eachObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/Alimento/lista.rb', line 99

def ord_each
    s = self.list_to_array
    (1..(s.size-1)).each do |i|
      (0..(s.size-i-1)).each do |j|
        if s[j].valor_energetico > s[j+1].valor_energetico
          s[j],s[j+1] = s[j+1],s[j]
        end
      end
    end
    s
end

#ord_forObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/Alimento/lista.rb', line 87

def ord_for
    s = self.list_to_array
    for i in 1..(s.size-1)
      for j in 0..(s.size-i-1)
        if s[j].valor_energetico > s[j+1].valor_energetico
          s[j],s[j+1] = s[j+1],s[j]
        end
      end
    end
    s
end

#ord_sortObject



111
112
113
114
# File 'lib/Alimento/lista.rb', line 111

def ord_sort
  s = self.list_to_array
  s.sort { |a,b| a.valor_energetico <=> b.valor_energetico }
end

#pop_backObject

Sacas elemento por el final



73
74
75
76
77
78
79
80
81
# File 'lib/Alimento/lista.rb', line 73

def pop_back()
    if(@size!=0)
        tail = @tail
        @tail = tail.prev
        @tail.next = nil if @tail
        @size -=1
        return tail
    end
end

#pop_frontObject

Sacas elemento por el principio



62
63
64
65
66
67
68
69
70
# File 'lib/Alimento/lista.rb', line 62

def pop_front()
    if(@size!=0)
        head = @head
        @head = head.next
        @head.prev = nil
        @size -=1
        return head
    end
end

#push_back(valor) ⇒ Object

Insertas elemento por el final



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/Alimento/lista.rb', line 30

def push_back(valor)
    node = Node.new(valor,nil,nil)
    if(@size==0)
        @head = node
        @tail = node
        @size +=1
    elsif
        @tail.next = node
        node.prev = @tail
        @tail=node
        @size +=1
    end
    true
end

#push_front(valor) ⇒ Object

Insertas elemento por el principio



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/Alimento/lista.rb', line 46

def push_front(valor)
    node = Node.new(valor,nil,nil)
    if(@size==0)
        @head = node
        @tail = node
        @size +=1
    elsif
        @head.prev = node
        node.next = @head
        @head=node
        @size +=1
    end
    true
end