class Lista

Attributes

head[RW]
size[RW]
tail[RW]

Public Class Methods

new() click to toggle source
# File lib/prct08/lista.rb, line 6
def initialize
  @head = nil #Cabecera de la lista
  @tail = nil #Cola de la lista
  @size = 0 #TamaƱo de la lista
end

Public Instance Methods

each() { |value| ... } click to toggle source
# File lib/prct08/lista.rb, line 153
def each
         aux = @head
      while aux != nil
          yield aux.value
          aux = aux.next
      end
  end
empty() click to toggle source
# File lib/prct08/lista.rb, line 149
def empty
  return size==0
end
extract_head() click to toggle source
# File lib/prct08/lista.rb, line 86
def extract_head
  if @head == nil
     @tail = nil
  else
    aux = @head
    @head = @head.next
    @size = @size - 1
  end
  return aux
end
extract_pos(index) click to toggle source
# File lib/prct08/lista.rb, line 112
def extract_pos(index)
  pos = 0
  aux = @head
  if pos == index - 1
    extract_head()
  end
  if index == @size
    extract_tail()
  else
    while (pos != (index - 1)) && (aux.next != nil) do
        pos = pos + 1
        aux = aux.next
    end
    auxBef = aux.before
    auxNext =  aux.next
    auxBef[:next] = auxNext
    auxNext[:before] = auxBef
    return aux
    @size = @size - 1
  end
end
extract_tail() click to toggle source
# File lib/prct08/lista.rb, line 97
def extract_tail
  if @head == @tail
    return @tail
    @head = nil
    @tail = nil
    @size = @size - 1
  else
    aux = @tail
    @tail = aux[:before]
    @tail[:next] = nil
    @size = @size - 1
    return aux
  end
end
get_pos(index) click to toggle source
# File lib/prct08/lista.rb, line 42
def get_pos(index)
  pos = 0
  aux = @head
  while pos != (index - 1)  do
    pos = pos + 1
    aux = aux.next
  end
  return aux
end
insert_head(nodo) click to toggle source
# File lib/prct08/lista.rb, line 12
def insert_head(nodo)
  if empty
    nodo[:next] = nil
    nodo[:before] = nil
    @head = nodo
    @tail = nodo
  else
    nodo[:next] = @head
    @head[:before] = nodo
    nodo[:before] = nil
    @head = nodo
  end
    @size = @size + 1
end
insert_mul(nodos, index) click to toggle source
# File lib/prct08/lista.rb, line 79
def insert_mul (nodos, index)
  for i in 0..(nodos.length - 1)
    insert_pos(nodos[i], index)
    index = index + 1
  end
end
insert_pos(nodo, index) click to toggle source
# File lib/prct08/lista.rb, line 52
def insert_pos(nodo, index)
  pos = 0
  aux = @head
  if pos == 1
    insert_head(nodo)
  end
  if pos == size
    insert_tail(nodo)
  else
    while(pos != (index - 1) && (aux.next != nil)) do
      if pos == index - 2
        nodo[:next] = aux.next
        nodo[:before] = aux
        aux[:next] = nodo
      end
      if pos == index - 1
        aux = nodo.next
        aux[:before] = nodo
      else
        pos = pos + 1
        aux = aux.next
      end
      @size = @size + 1
    end
  end
end
insert_tail(nodo) click to toggle source
# File lib/prct08/lista.rb, line 27
def insert_tail(nodo)
  if empty
    nodo[:next] = nil
    nodo[:before] = nil
    @head = nodo
    @tail = nodo
  else
    @tail[:next] = nodo
    nodo[:next] = nil
    nodo[:before] = @tail
    @tail = nodo
  end
  @size = @size + 1
end
to_s() click to toggle source
# File lib/prct08/lista.rb, line 134
def to_s
  aux = @head
  if empty != true
    string = ""
    while aux.next != nil
      string += "\n\n#{aux.value}"
      aux = aux.next
    end
    string += "\n\n#{aux.value}"
    return string
  else
    return "Lista vacia"
  end
end