Class: Dieta::Lista

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLista

Returns a new instance of Lista.



8
9
10
11
12
# File 'lib/dieta/lista.rb', line 8

def initialize
    @cabecera = nil
    @cola = nil
    @longitud = 0
end

Instance Attribute Details

#cabeceraObject (readonly)

Returns the value of attribute cabecera.



5
6
7
# File 'lib/dieta/lista.rb', line 5

def cabecera
  @cabecera
end

#colaObject (readonly)

Returns the value of attribute cola.



5
6
7
# File 'lib/dieta/lista.rb', line 5

def cola
  @cola
end

#longitudObject (readonly)

Returns the value of attribute longitud.



5
6
7
# File 'lib/dieta/lista.rb', line 5

def longitud
  @longitud
end

Instance Method Details

#eachObject



14
15
16
17
18
19
20
# File 'lib/dieta/lista.rb', line 14

def each
      temp = @cabecera
      while temp != nil do
          yield temp.valor
          temp = temp.siguiente
      end
end

#getObject



60
61
62
63
64
65
66
67
# File 'lib/dieta/lista.rb', line 60

def get
  unless @cola.nil?
    node = @cola
    @cola = @cola.anterior
    @cola.siguiente = nil
    node.valor
  end
end

#get_e(indice) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/dieta/lista.rb', line 69

def get_e(indice)
    temp = @cabecera
    count = 0
    while count < indice - 1 do
        temp = temp.siguiente
        count += 1
    end
    temp.valor
end

#popObject



51
52
53
54
55
56
57
58
# File 'lib/dieta/lista.rb', line 51

def pop
    unless @cabecera.nil?
        node = @cabecera
        @cabecera = @cabecera.siguiente
        @cabecera.anterior = nil
        node.valor
    end
end

#push(valores) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dieta/lista.rb', line 22

def push(valores)
    if valores.instance_of? Array
        valores.each do |valor|
            node = Node.new(valor, nil, nil)
            if @cabecera.nil?
                @cabecera = node
                @cola = node
            else
                node = Node.new(valor, nil, @cola)
                @cola.siguiente = node
                @cola = node
            end
            @longitud += 1
        end
    else
        node = Node.new(valores, nil, nil)
        if @cabecera.nil?
            @cabecera = node
            @cola = node
        else
            node = Node.new(valores, nil, @cola)
            @cola.next = node
            @cola = node
        end
        @longitud += 1
      end
    @cola.valor
end

#set_e(indice, val) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/dieta/lista.rb', line 79

def set_e(indice, val)
    temp = @cabecera
    count = 0
    while count < indice - 1 do
        temp = temp.siguiente
        count += 1
    end
    temp.valor = val
end

#to_sObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/dieta/lista.rb', line 89

def to_s
  count = 2
  output = @cabecera.valor.to_s
  while count < @longitud - 1 do
    obj = get_e(count)
    output += obj.to_s
    count += 1
  end
  output
end