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)


34
35
36
37
38
39
40
# File 'lib/InforNutricional/CodeDList.rb', line 34

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



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

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



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

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



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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/InforNutricional/CodeDList.rb', line 91

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

#to_sObject

Método para mostrar la lista



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

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