class ListaEnlazada

Clase que implementa una lista enlazada

Attributes

bottom[R]
size[R]
top[R]

Public Class Methods

new() click to toggle source

Al inicializarse el objeto. El tamaño de la lista es cero y tanto @bottom como @top son nil.

# File lib/prct06/ListaEnlazada.rb, line 14
def initialize
  @bottom = nil
  @top = nil
  @size = 0
end

Public Instance Methods

each() { |value| ... } click to toggle source
# File lib/prct06/ListaEnlazada.rb, line 49
def each
  i = bottom
  while i!=nil do
    yield i.value
    i = i.next;
  end
end
pop() click to toggle source

Elimina el último elemento añadido a la lista. En este caso, @top apuntará al elemento anterior.

# File lib/prct06/ListaEnlazada.rb, line 37
def pop
  if @top==nil
    output = "Error. La lista está vacía."
  else
    @top = @top.previous
    if @top == nil
      @bottom = nil
    end
    @size -= 1
  end
end
push(element) click to toggle source

Añade un elemento al final de la lista. @top apuntará al último elemento añadido

# File lib/prct06/ListaEnlazada.rb, line 21
def push (element)
    aux = @top
    @top = Node.new(element, nil ,@top)
    if (aux!=nil)
      aux.next = @top
    end
    if (@bottom != nil && @bottom.next == nil )
      @bottom.next = @top
    end
    if @bottom == nil
      @bottom = @top
    end
    @size += 1
  end
to_s() click to toggle source

Método que permite mostrar el último elemento añadido en la lista. En caso de que la lista esté vacía, mostrará el mensaje “Error. La lista está vacía”

# File lib/prct06/ListaEnlazada.rb, line 59
def to_s
  if @top==nil
    output = "Error. La lista está vacía."
  else
    @top.value.to_s
  end
end