Class: BiblioRefs::List

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*nodo) ⇒ List

Constructor de la clase List



11
12
13
14
15
16
17
# File 'lib/biblio_refs/list.rb', line 11

def initialize(*nodo)
  @tail = @head = Nodo.new(nodo[0])
  if nodo.size > 1
    nodo.shift
    push(*nodo)
  end
end

Instance Attribute Details

#headObject

Returns the value of attribute head.



8
9
10
# File 'lib/biblio_refs/list.rb', line 8

def head
  @head
end

#tailObject

Returns the value of attribute tail.



8
9
10
# File 'lib/biblio_refs/list.rb', line 8

def tail
  @tail
end

Instance Method Details

#each {|aux[:value]| ... } ⇒ Object

Método que recorre todos los valores de los nodos de la lista Necesario para hacer la clase List enumerable, haciendo ‘yield’ a todos los valores

Yields:

  • (aux[:value])


21
22
23
24
25
26
27
28
# File 'lib/biblio_refs/list.rb', line 21

def each
  aux = @head
  while aux[:next]
    yield aux[:value]
    aux = aux[:next]
  end
  yield aux[:value]
end

#popObject

Método que devuelve y extrae el valor del primer nodo de la lista



31
32
33
34
35
# File 'lib/biblio_refs/list.rb', line 31

def pop
  nodo = @head
  @head = @head[:next]
  nodo[:value]
end

#push(*nodo) ⇒ Object

Método que inserta uno o varios nodos al final de la lista



38
39
40
41
42
43
44
45
46
# File 'lib/biblio_refs/list.rb', line 38

def push(*nodo)
  aux = @head
  nodo.each do |n|
    while aux[:next] do
      aux = aux[:next]
    end
    @tail = aux[:next] = Nodo.new(n, nil, aux)
  end
end

#to_sObject

Método que devuelve una cadena de carácteres formateada de los objetos de la clase List



49
50
51
52
53
54
55
56
57
# File 'lib/biblio_refs/list.rb', line 49

def to_s
  aux = @head
  string = "Lista: "
  while aux[:next] do
    string += "#{aux[:value]}" + " -> "
    aux = aux[:next]
  end
  string += "#{aux[:value]}"
end