Class: LifoStack

Inherits:
Object
  • Object
show all
Defined in:
lib/stack.rb

Defined Under Namespace

Classes: Node

Instance Method Summary collapse

Instance Method Details

#clearObject



24
25
26
27
28
# File 'lib/stack.rb', line 24

def clear
	while self.peek
		pop
	end
end

#emptyObject



38
39
40
41
42
43
44
# File 'lib/stack.rb', line 38

def empty
	if @top.nil?
		puts "Stack is empty"
	else
		puts "Stack is full"
	end
end

#fullObject



30
31
32
33
34
35
36
# File 'lib/stack.rb', line 30

def full
	if @top
		puts "Stack is full"
	else
		puts "Stack is empty"
	end
end

#peekObject



16
17
18
19
20
21
22
# File 'lib/stack.rb', line 16

def peek
	if @top == 0
		puts "Peek is empty"
	else
		puts @top
	end
end

#popObject



46
47
48
49
50
51
52
53
54
# File 'lib/stack.rb', line 46

def pop
	if not @top
		return nil
	else
		tmp = @top
		@top = @top.node
		tmp.element
	end
end

#push(element) ⇒ Object



12
13
14
# File 'lib/stack.rb', line 12

def push(element)
	@top = Node.new(element, @top)
end