Class: Alimento::Dll

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodo = nil) ⇒ Dll

Crea una lista doblemente enlazada. Si toma un nodo de argumento apunta la cabeza y cola al mismo, si no, la crea vacía.



22
23
24
25
# File 'lib/alimento/dll.rb', line 22

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

Instance Attribute Details

#headObject (readonly)

Returns the value of attribute head.



17
18
19
# File 'lib/alimento/dll.rb', line 17

def head
  @head
end

#tailObject (readonly)

Returns the value of attribute tail.



17
18
19
# File 'lib/alimento/dll.rb', line 17

def tail
  @tail
end

Instance Method Details

#eachObject

Incluido para poder utilizar los métodos mixin de Enumerable para la Lista Doblemente Enlazada.



103
104
105
106
107
108
109
# File 'lib/alimento/dll.rb', line 103

def each
  tmp = self.head
  while tmp != nil
    yield tmp.value
    tmp = tmp.next
  end
end

#extraer(nodo) ⇒ Object

Busca el primer nodo con el valor especificado y se elimina



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/alimento/dll.rb', line 70

def extraer(nodo)
  if nodo == @head
    extraerPrimero
  elsif nodo == @tail
    extraerUltimo
  else
    tmp = @head
    while tmp != nodo
      tmp = tmp.next
    end
    if tmp == nodo
      tmp.prev.next = tmp.next
      tmp.next.prev = tmp.prev
      tmp.prev == nil
      tmp.next == nil
    end
  end
end

#extraerPrimeroObject

Elimina de la lista el primer valor



62
63
64
65
66
67
# File 'lib/alimento/dll.rb', line 62

def extraerPrimero
  if @head != nil
    @head = @head.next
    @head.prev = nil
  end
end

#extraerUltimoObject

Elimina de la lista el último valor



54
55
56
57
58
59
# File 'lib/alimento/dll.rb', line 54

def extraerUltimo
  if @tail != nil
    @tail = @tail.prev
    @tail.next = nil
  end
end

#insertarDelante(valor) ⇒ Object

Inserta el valor al inicio de la lista



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/alimento/dll.rb', line 28

def insertarDelante(valor)
  nodo = Nodo.new(valor,nil,nil)
  if @head == nil
    @head = nodo
    @tail = nodo
  else
    nodo.next = @head
    @head.prev = nodo
    @head = nodo
  end
end

#insertarDetras(valor) ⇒ Object

Inserta el valor al final de la lista



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/alimento/dll.rb', line 41

def insertarDetras(valor)
  nodo = Nodo.new(valor,nil,nil)
  if @tail == nil
    @head = nodo
    @tail = nodo
  else
    nodo.prev = @tail
    @tail.next = nodo
    @tail = nodo
  end
end

#to_sObject

Operador de salida de la Lista Doblemente enlazada



90
91
92
93
94
95
96
97
98
99
# File 'lib/alimento/dll.rb', line 90

def to_s
  if @head != nil
    temp = @head
    temp.value.to_s
    while temp.next != nil
      temp = temp.next
      temp.value.to_s
    end
  end
end