class DLL::DLL

Esta clase permite crear una lista doblemente enlazada. Se ha incluido el mixin Enumerable.

Constants

Node

Attributes

head[R]
tail[R]

Public Class Methods

new() click to toggle source

La lista se crea vacía por defecto.

# File lib/DLL/fuente.rb, line 24
def initialize()
 @head, @tail = nil, nil
end

Public Instance Methods

each() { |value| ... } click to toggle source

Se incluye el metodo del mixin Enumerable Se define como la iteración entre los valores de los nodos.

# File lib/DLL/fuente.rb, line 97
def each # :yields: value
  aux = @tail
  while (aux != nil) do
    yield aux[:value]
    aux = aux.next
  end
end
empty() click to toggle source

Devuelve true si la lista está vacía, false en otro caso.

# File lib/DLL/fuente.rb, line 29
def empty()
  return true if (@head == nil && @tail == nil)
  false
end
erase_head() click to toggle source

Elimina el elemento al que apunta head.

# File lib/DLL/fuente.rb, line 82
def erase_head()
  aux = @head[:prev]
  aux[:next] = nil
  @head = aux
end
erase_tail() click to toggle source

Elimina el elemento al que apunta tail.

# File lib/DLL/fuente.rb, line 89
def erase_tail()
  aux = @tail[:next]
  aux[:prev] = nil
  @tail = aux
end
insert_head(*objects) click to toggle source

Inserta los elementos por el principio de la lista.

# File lib/DLL/fuente.rb, line 52
def insert_head(*objects)
  objects.each{ |x|
    if(self.empty)
      aux = Node.new(x, nil, @head)
      @tail = aux
      @head = aux
    else
      aux = Node.new(x, nil, @head)
      @head[:next] = aux
      @head = aux
    end
  }
end
insert_tail(*objects) click to toggle source

Inserta los elementos por el final de la lista.

# File lib/DLL/fuente.rb, line 67
def insert_tail(*objects)
  objects.each{ |x|
    if(self.empty)
      aux = Node.new(x, @tail, nil)
      @tail = aux
      @head = aux
    else
      aux = Node.new(x, @tail, nil)
      @tail[:prev] = aux
      @tail = aux
    end
  }
end
to_s() click to toggle source

Muestra los elementos de la lista como si fuese un string.

# File lib/DLL/fuente.rb, line 35
def to_s()
  out = ""
  aux = @tail
  while (aux != nil) do
    if(aux[:next] == nil)
      out << "#{aux[:value]}"
    else
      out << "#{aux[:value]}, "
    end
    aux = aux.next
  end

  out

end