class Stack::Singly

Attributes

top[R]

Public Class Methods

new(data=nil) click to toggle source
# File lib/stack_sourav.rb, line 5
def initialize(data=nil)
  @head = @top = LinkedList::Node.new(data)
end

Public Instance Methods

add(data=nil) click to toggle source
# File lib/stack_sourav.rb, line 19
def add (data=nil)
  puts "Does not work here"
end
delete() click to toggle source
# File lib/stack_sourav.rb, line 23
def delete
  puts "Doesnot Work here"
end
edit() click to toggle source
# File lib/stack_sourav.rb, line 27
def edit
  puts "Does not work here"
end
is_empty?() click to toggle source
# File lib/stack_sourav.rb, line 31
def is_empty?
   true if @top.data == nil
end
pop() click to toggle source
# File lib/stack_sourav.rb, line 35
def pop
  poped_data = ''
  if @head.data === @top.data
    poped_data = @head.data
    @head.data = @top.data = nil
  else
    node = @head
    while (!(node.forward.equal? @top) )
      node = node.forward
    end
    poped_node = @top
    @top = node
    @top.forward = nil
    poped_data = poped_node.data
    poped_node = nil
  end
  poped_data
end
push(data) click to toggle source
# File lib/stack_sourav.rb, line 9
def push(data)
  if is_empty?
    @head.data =@top.data = data
  else
    new_node = LinkedList::Node.new(data)
    @top.forward = new_node
    @top = new_node
  end
end