Class: Lista_doble

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

Overview

Lista_doble class It’s a double linked list in this case, we used a struct to describe the class too we include the mixin Enumerable

  • empty

  • initialize

  • insertar_elemento

  • extraer_elemento

  • each

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLista_doble

We set the initial and last node to nil



86
87
88
89
90
# File 'lib/menud/lista.rb', line 86

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

Instance Attribute Details

#headObject

Returns the value of attribute head.



76
77
78
# File 'lib/menud/lista.rb', line 76

def head
  @head
end

#tailObject

Returns the value of attribute tail.



76
77
78
# File 'lib/menud/lista.rb', line 76

def tail
  @tail
end

Instance Method Details

#convert_a_eachObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/menud/lista.rb', line 169

def convert_a_each
   

    n_array= []
    
    enum=@head
    
    for i in self do
            
        n_array.push(enum.value)
        enum = enum.next
    end
   n_array.bubbleach
  
end

#convert_a_forObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/menud/lista.rb', line 152

def convert_a_for
    
 
     n_array= []
     
     enum=@head
     
     for i in self do
             
         n_array.push(enum.value)
         enum = enum.next
     end
    n_array.bubblefor
   
end

#eachObject

Its a define like a iteration on the nodes of the list to get the value



143
144
145
146
147
148
149
# File 'lib/menud/lista.rb', line 143

def each
    enum = @head
    while (enum != nil) 
         yield enum.value
         enum = enum.next
    end
end

#empty?Boolean

We set the initial node to nil and we see if the dll is empty

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/menud/lista.rb', line 80

def empty?
   
   @head == nil
end

#extraer_elementoObject

We extract a new element on the double linked list



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/menud/lista.rb', line 112

def extraer_elemento
    
    @nodo = Nodo_.new(nil, @head.value, nil)
    
    @head = @head.next
    @head.prev = nil
    return @nodo
     
    #nodo = @head
    #if @head.next == nil
   #     @head = nil
  #  else
 #       @head = @head.next
    #end
    #return nodo
    
    #nodo = @head
    #@head = @head.next
    
    #nodo.next = nil
    #nodo.prev = nil
    
    #if @head == nil
     #  @tail = nil
    #end
    
    #return nodo
end

#insertar_elemento(nodo) ⇒ Object

We insert a new element on the double linked list



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/menud/lista.rb', line 93

def insertar_elemento(nodo)
   
   @nodo =  Nodo_.new(nil, nodo, nil)
   
   if @tail == nil
      @head = @nodo
      @tail = @nodo
      #@nodo
   else
       @nodo.next = @head
       @head.prev = @nodo
       @head = @nodo
       #@nodo
   end
   
end