Class: Lista

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/prct11/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/prct11/lista.rb', line 8

def initialize
    @head = Nodo.new(nil,nil,nil) #inicio de la lista
    @tail = Nodo.new(nil,nil,nil) #fin de la lista
    @contador=0  #variable que determina el tamaño actual de la lista
end

Instance Attribute Details

#contadorObject

Returns the value of attribute contador.



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

def contador
  @contador
end

#headObject

Returns the value of attribute head.



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

def head
  @head
end

#tailObject

Returns the value of attribute tail.



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

def tail
  @tail
end

Instance Method Details

#eachObject



98
99
100
101
102
103
104
105
# File 'lib/prct11/lista.rb', line 98

def each
actual = @tail
	
    while actual != nil
        yield actual.value
        actual = actual.siguiente
    end
end

#emptyObject



107
108
109
# File 'lib/prct11/lista.rb', line 107

def empty
    return contador==0
end

#extract_headObject



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

def extract_head
    aux = @head
    @head = @head[:previo]
    
    if @head != nil             #Si head no es nil ponemos el siguiente de head como nil para desenlazar la lista
        @head[:siguiente] = nil
    else                        #Si el elemento que se extrae es el unico de la lista entonces ponemos todo a nil como en initialize
        @tail = nil
    end
    
    @contador = @contador - 1
    return aux.value
end

#extract_tailObject



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

def extract_tail
    if @contador == 1
        return extract_head
    else    
        actual = @tail
        @tail = @tail[:siguiente]
  
        if @tail != nil
            @tail[:previo] = nil
        end   
        @contador = @contador - 1
        return actual.value
    end
end

#insert_head(nodo) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/prct11/lista.rb', line 15

def insert_head(nodo)
    
    if empty == true            #Si la lista esta vacia insertamos el elemento en la lista vacia
        nodo[:previo] = @tail
        nodo[:siguiente] = @head
        @head = nodo
        @tail = nodo
    else                        #Si no esta vacia insertamos por el head 
        nodo[:previo] = @head
        nodo[:siguiente] = nil
        @head[:siguiente] = nodo
        @head = nodo
    end
    @contador = @contador + 1   #Aumentamos variable que indica tamaño de la lista 
    
end

#insert_tail(nodo) ⇒ Object



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

def insert_tail(nodo)
    if empty                    #Si esta vacia insertamos en lista vacia
        insert_head(nodo)
    else                        #Si no insertamos por el tail
        nodo[:siguiente] = @tail
        nodo[:previo] = nil
        @tail[:previo] = nodo
        @tail = nodo
        @contador = @contador + 1
    end
end

#insert_varios(nodos) ⇒ Object



76
77
78
79
80
# File 'lib/prct11/lista.rb', line 76

def insert_varios (nodos)
    for i in 0..(nodos.length - 1)
      insert_head(nodos[i])
    end
end

#to_sObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/prct11/lista.rb', line 83

def to_s
    aux = @tail
    if empty != true
        string= "\n****** MENU DIETETICO ******"
        while aux.siguiente != nil
            string+= "\n\n#{aux.value}"
            aux = aux.siguiente
        end
        string += "\n\n#{aux.value}"
        return string
    else
        return "Lista vacia"
    end
end