Class: ListaDoblementeEnlazada

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

Overview

Note:

Clase ListaDoblementeEnlazada - Lista doblemente enlazada

Author:

  • Nicolangelo Famiglietti

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeListaDoblementeEnlazada

Note:

Inicializador

Returns:

Inicilizacion de la cola y la cabeza



14
15
16
# File 'lib/alimento/lista.rb', line 14

def initialize
    @head = @tail = nil
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head



9
10
11
# File 'lib/alimento/lista.rb', line 9

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail



9
10
11
# File 'lib/alimento/lista.rb', line 9

def tail
  @tail
end

Instance Method Details

#<=>(other) ⇒ Object

Note:

Metodo comparable de la clase Food

Returns:

0 Si es igual 1 Si es mayor -1 Si es menor



122
123
124
125
126
127
128
129
130
131
# File 'lib/alimento/lista.rb', line 122

def <=>(other)
    if @ve == other.ve
        return 0
    elsif
        ve > other.ve
        return 1
    else
        return -1
    end
end

#eachObject

Note:

Método Enumerable

Returns:

Enumerable Metodo de acceso a acada elemento



109
110
111
112
113
114
115
# File 'lib/alimento/lista.rb', line 109

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

#emptyObject

Note:

Método empty

Returns:

true si esta vacio false si contiene algo



50
51
52
53
54
55
56
# File 'lib/alimento/lista.rb', line 50

def empty
    if(@head == nil)
        true
    elsif
        false
    end
end

#extract_headObject

Note:

Método extract_head

Returns:

El elemento a extraer por la cabeza



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/alimento/lista.rb', line 75

def extract_head
    nodo = @head
  	if nodo != nil
  		if nodo.next != nil
  			@head = nodo.next
  		else
  			@head = nil
  			@tail = nil
  		end
  	end
  	nodo
end

#extract_tailObject

Note:

Método extract_tail

Returns:

El elemento a extraer por la cola



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/alimento/lista.rb', line 90

def extract_tail
    aux = nil
    if @tail[:value]==nil
        aux=nil
    else
        aux = @tail[:value]
        @tail = @tail[:prev]
        @tail = nil
    end
    if @tail==nil
        @tail = Node.new
        @head = @tail
    end
    aux
end

#insert(value) ⇒ Object

Note:

Método insert

Returns:

Inserta un elemento en la lista Devuelve true si se realizó correctamente



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

def insert(value)
    node = Node.new(value, nil)
    if(empty)
        @head = node
        @tail = node
    elsif
        @tail.next = node
        @tail = node
    end
    true
end

#sizeObject

Note:

Método size

Returns:

Devuelve el tamaño de la lista



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/alimento/lista.rb', line 34

def size
    count = 0
    if @head[:value] != nil then
        count=1
    end
    aux= @head
    until aux[:next] == nil do
        count+=1
        aux=aux[:next]
    end
    count
end

#sortEach(lista) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/alimento/lista.rb', line 158

def sortEach(lista)
    numbers = []
    i=0
    numbers[i] = lista.extract_head.value
    lista.each {|x|
          if numbers[i] > x
            aux = numbers[i]
            numbers[i] = x
            i=i+1
            numbers[i] = aux
          else
            i=i+1
            numbers[i] = x
          end
    } 
    numbers
end

#sortFor(lista) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/alimento/lista.rb', line 133

def sortFor(lista)
    numbers=[]
    a=0
    lista.each{|x| 
        numbers[a] = x
        a=a+1
      }
    for i in 0..numbers.size-1 do
        for j in 0..numbers.size-1 do
          if(numbers[i].get_ve < numbers[j].get_ve)
            aux = numbers[j]
            numbers[j] = numbers[i]
            numbers[i] = aux
          end
        end
    end
    lista = []
    a=0
    numbers.each{|x| 
        lista[a] = x.get_ve
        a=a+1
    }
    lista
end

#to_sObject

Note:

Método to_s

Returns:

Devuelve la lista de alimentos formateada



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/alimento/lista.rb', line 20

def to_s
    aux = @head
    s= aux.value.n_grupo
    s+="\n"
    s+= "\t\t\tProteinas Glucidos Lipidos\n"
    while(aux != nil)
        s+="\t#{aux.value.nombre} \t#{aux.value.proteinas}  \t   #{aux.value.glucidos}      #{aux.value.grasas}\n"
        aux= aux[:next]
    end
    s
end