Class: List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/FoodImpact/list.rb

Overview

Contenedor de objetos

Modo de uso

lista = List.new lista.insert(1) lista.insert(2) lista.last

Instance Method Summary collapse

Constructor Details

#initialize(head, tail) ⇒ List

Construye una lista con una cabeza y una cola



15
16
17
18
19
# File 'lib/FoodImpact/list.rb', line 15

def initialize (head, tail)
  @head_ = head
  @tail = tail
  @sz_ = 0
end

Instance Method Details

#eachObject

Yield con cada elemento de la lista



56
57
58
59
60
61
62
# File 'lib/FoodImpact/list.rb', line 56

def each
  node = @head_
  while node != nil do
    yield node[:value]
    node = node[:next]
  end
end

#firstObject

Extrae el primer objeto de la lista



41
42
43
# File 'lib/FoodImpact/list.rb', line 41

def first
  @head_[:value]
end

#headObject

Devuelve head



46
47
48
# File 'lib/FoodImpact/list.rb', line 46

def head
  @head_
end

#insert(object) ⇒ Object

Inserta en la lista un nodo en la última posición



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/FoodImpact/list.rb', line 22

def insert(object)
  node = Node.new(object, nil, nil)
  if @sz_ == 0
    @head_ = node
    @tail_ = node
  else
    @tail_[:next] = node
    node[:prev] = @tail_
    @tail_ = node
  end
  @sz_+=1
end

#lastObject

Extrae el último objeto de la lista



36
37
38
# File 'lib/FoodImpact/list.rb', line 36

def last
  @tail_[:value]
end

#tailObject

Devuelve tail



51
52
53
# File 'lib/FoodImpact/list.rb', line 51

def tail
  @tail_
end