Class: Alimento::Lista

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

Overview

Esta clase es la definición de una lista doblemente enlazada.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLista

Inicia la lista (vacía).



13
14
15
16
# File 'lib/alimento/lista.rb', line 13

def initialize
    @head = nil
    @tail = nil
end

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



10
11
12
# File 'lib/alimento/lista.rb', line 10

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



10
11
12
# File 'lib/alimento/lista.rb', line 10

def tail
  @tail
end

Instance Method Details

#eachObject

Se incluye el metodo del mixin Enumerable. Iteración sobre los elementos de la lista desde el principio hasta el final.



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

def each
    iterador = @head
    		while iterador != nil
  yield iterador.valor
    			iterador = iterador.siguiente
    		end
end

#extraer_headObject

Elimina el primer elemento de la lista.



49
50
51
# File 'lib/alimento/lista.rb', line 49

def extraer_head
    @head = @head.siguiente
end

#extraer_tailObject

Elimina el último elemento de la lista.



54
55
56
# File 'lib/alimento/lista.rb', line 54

def extraer_tail
    @tail = @tail.anterior
end

#insertar_head(valor) ⇒ Object

Permite insertar un valor en la lista por delante.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/alimento/lista.rb', line 19

def insertar_head(valor)
    
    nodo = Nodo.new(valor,nil,nil)
    
    if (@head == nil)
        @head = nodo
        @tail = nodo
    else
        @head.anterior = nodo
        nodo.siguiente = @head
        @head = nodo
    end
end

#insertar_tail(valor) ⇒ Object

Permite insertar un valor en la lista por detrás.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/alimento/lista.rb', line 34

def insertar_tail(valor)
    
    nodo = Nodo.new(valor,nil,nil)
    
    if (@tail == nil)
        @tail = nodo
        @head = nodo
    else
        @tail.siguiente = nodo
        nodo.anterior = @tail
        @tail = nodo
    end
end