class DataStructList::Stack

Attributes

first[R]
head[R]
last[R]

Public Class Methods

new() click to toggle source
# File lib/data_struct_list.rb, line 26
def initialize
  @head = Head.new
  @last = nil
  @first = nil
end

Public Instance Methods

find(id) click to toggle source
# File lib/data_struct_list.rb, line 48
def find(id)
  elm = @first

  until elm == nil
    if elm.id == id then break end
    elm = elm.next
  end

  return elm
end
insert(hash) click to toggle source
# File lib/data_struct_list.rb, line 32
def insert(hash)
  if @head.quant == 0 then
    @head.next = Node.new(hash)
    @first = @head.next
    @last = @head.next
    @head.quant += 1

  else
    @last.next =  Node.new(hash)
    @last = @last.next
    @head.quant += 1
  end

  return @last.id
end
remove() click to toggle source
# File lib/data_struct_list.rb, line 59
def remove
  return nil if @head.quant == 0

  elm = @head.next
  @head.quant-=1

  until elm.next == @last
    if elm == @last then break end
    elm = elm.next
  end

  if @first == @last then
    @first = nil
    @last = nil
    @head.next = nil
    return nil
  end

  aux = @last
  @last = elm
  elm.next = nil
  aux = nil
end