Class: List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/InforNutricional/CodeDList.rb

Overview

Clase List que representa una lista doblemente enlazada

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



9
10
11
# File 'lib/InforNutricional/CodeDList.rb', line 9

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



9
10
11
# File 'lib/InforNutricional/CodeDList.rb', line 9

def tail
  @tail
end

Instance Method Details

#eachObject

Método para enumerar lista



20
21
22
23
24
25
26
# File 'lib/InforNutricional/CodeDList.rb', line 20

def each 
    nodo = @head
    while(nodo != nil)
        yield nodo.value
        nodo = nodo.prev
    end
end

#empty?Boolean

Método que comprueba si la lista está vacía

Returns:

  • (Boolean)


72
73
74
75
76
77
78
# File 'lib/InforNutricional/CodeDList.rb', line 72

def empty?()
    if (@tail != nil and @head != nil)
        return false
    else
        return true
    end
end

#extract_headObject

Método para extraer un elemento por la cabeza en la lista



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/InforNutricional/CodeDList.rb', line 147

def extract_head()
   
    node = @head
    @head = @head.prev
    
    if (@head == nil)
        @tail = nil
    else
        @head.next = nil
    end
    
    node.prev = nil
    node.next =nil
    
    return node.value
end

#extract_tailObject

Método para extraer un elemento por la cola en la lista



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/InforNutricional/CodeDList.rb', line 166

def extract_tail()
    
    node = @tail
    @tail = @tail.next
    
    if (@tail == nil)
        @head = nil
    else
        @tail.prev = nil
    end
    
    node.prev = nil
    node.next =nil
    return node.value
end

#initializateObject

Metodo initialize de la clase



15
16
17
# File 'lib/InforNutricional/CodeDList.rb', line 15

def initializate()
    @tail = @head = nil
end

#insert_head(value) ⇒ Object

Método para introducir un elemento por la cabeza en la lista



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/InforNutricional/CodeDList.rb', line 112

def insert_head(value)
    
    node = Node.new(value)
    
    if (@head == nil)
        @head = @tail = node
    else
        node.prev = @head
        @head.next = node
        @head = node
    end
   
end

#insert_tail(value) ⇒ Object

Método para introducir un elemento por la cola en la lista



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/InforNutricional/CodeDList.rb', line 129

def insert_tail(value)
    
    node = Node.new(value)
    
    if (@tail == nil)
        @head = @tail = node
    else
        node.next = @tail
        @tail.prev = node
        @tail = node
    end
   
end

#ordenar_EachObject

__#



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/InforNutricional/CodeDList.rb', line 49

def ordenar_Each ()
    lista = self.map { |x| x}
    indice = 0
    lista.each do |x|
        lista.each do |y|
            if (indice < lista.length-1)
                if (lista[indice] > lista[indice+1])
                    temp = lista[indice]
                    lista[indice] = lista[indice+1]
                    lista[indice+1] = temp 
                end
            end
            indice = indice+1
        end
        indice = 0
    end
    lista
end

#ordenar_ForObject

Método para ordenar una lista pasandola a array empleando el for



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/InforNutricional/CodeDList.rb', line 32

def ordenar_For ()
    lista = self.map { |x| x}
    for i in 0..(lista.length) do
        for j in 0..(lista.length-2) do 
            if(lista[j] > lista[j+1])
                temp = lista[j]
                lista[j] = lista[j+1]
                lista[j+1] = temp 
            end
        end 
    end
    lista
end

#to_sObject

Método para mostrar la lista



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/InforNutricional/CodeDList.rb', line 84

def to_s

    if (@head != nil)
        node_aux = @head
        @output = node_aux.value
        
        if ( node_aux.prev != nil )
                @output += " "
        end
        
        while (node_aux.prev != nil) do
            node_aux = node_aux.prev
            @output += node_aux.value
            
            if ( node_aux.prev != nil )
                @output += " "
            end
        end
        
        "Lista: #{@output}"
    end
end