HO 05 Stack
HO 05 Stack
HO 05 Stack
top F
top E E
D D
C C
B B
bottom A bottom A
• Linear list.
• One end is called top.
• Other end is called bottom.
• Additions to and removals from the top end
only.
Stack operations
• Push
– the operation to place a new item at the top of
the stack
• Pop
– the operation to remove the next item from the
top of the stack
Last In First Out
E top
D top D D top
C top C C C
B top B B B B
A top A A A A A
Last In First Out cont..
M
C C C
R push(M) item = pop()
R R
item = M
X X X
A A A
Stack Specification
• Definitions: (provided by the user)
– MAX_ITEMS: Max number of items that might be on the stack
– ItemType: Data type of the items on the stack
• Operations
– MakeEmpty
– Boolean IsEmpty
– Boolean IsFull
– Push (ItemType newItem)
– Pop (ItemType& item) (or pop and top)
Stacks operations
– Standard operations:
• IsEmpty … return true iff stack is empty
• IsFull … return true iff stack has no remaining capacity
• Top … return top element of stack
• Push … add an element to the top of the stack
• Pop … delete the top element of the stack
Implementing a Stack
• Real life
– Pile of books
– Plate trays
• More applications related to computer science
– Program execution stack (read more from your
text)
– Evaluating expressions
The Stack Abstract Data Type
• Example : The following table shows a series of stack operations and their
effects on an initially empty stack S of integers.
push(5) - (5)
push(3) - (5, 3)
pop() 3 (5)
push(7) - (5, 7)
pop() 7 (5)
top() 5 (5)
pop() 5 ()
pop() "error“ ()
isEmpty() ()
push(9) true
push(7) - (9)
push(3) - (9, 7)
push(5) - (9, 7, 3)
size() - (9, 7, 3, 5)
pop() 4 (9, 7, 3, 5)
push(8) 5 (9, 7, 3)
pop() - (9, 7, 3, 8)
pop() 8 (9, 7, 3)
3 (9, 7)
Exercise: Stacks
25
Simple Uses of a Stack
A trace of the algorithm that converts the infix expression a – ( b + c * d ) / e to postfix form
Parentheses Matching
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
– Output pairs (u,v) such that the left parenthesis at
position u is matched with the right parenthesis at v.
• (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38)
• (a+b))*((c+d)
– (0,4)
– right parenthesis at 5 has no matching left parenthesis
– (8,12)
– left parenthesis at 7 has no matching right parenthesis
Parentheses Matching
• scan expression from left to right
• when a left parenthesis is encountered, add its
position to the stack
• when a right parenthesis is encountered, remove
matching position from stack
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
2
1
0
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
15
1
0 (2,6) (1,13)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
21
1
0 (2,6) (1,13) (15,19)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
27
1
0 (2,6) (1,13) (15,19) (21,25)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
1
0 (2,6) (1,13) (15,19) (21,25)(27,31) (0,32)
• and so on
Infix to Postfix Conversion
(Intuitive Algorithm)
/ - +* -*
(3) Delete all parentheses.
ab/c-de*+ac*-
two passes
The orders of operands in infix and postfix are the same.
a+b*c
4
3
2
1
A B C
• 64 gold disks to be moved from tower A to tower C
• each tower operates as a stack
• cannot place big disk on top of a smaller one
Towers Of Hanoi/Brahma
3
2
1
A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
2
1 3
A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
1 2 3
A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
3
1 2
A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
3
2 1
A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
3 2 1
A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
2
3 1
A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma
3
2
1
A B C
• 3-disk Towers Of Hanoi/Brahma
• 7 disk moves
Recursive Solution
A B C
• n > 0 gold disks to be moved from A to C using B
• move top n-1 disks from A to B using C
Recursive Solution
A B C
• move top disk from A to C
Recursive Solution
A B C
• move top n-1 disks from B to C using A
Recursive Solution
A B C
• moves(n) = 0 when n = 0
• moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
Towers Of Hanoi/Brahma
End pin 36 15
=> start 35 16
pin must 34 17
be at top
33 18
of stack.
32 19
31 20
30 29 28 27 26 25 24 23 22 21
Method Invocation And Return
public void a()
{ …; b(); …}
public void b()
{ …; c(); …} return address in d()
public void c() return address in c()
{ …; d(); …} return address in e()
public void d() return address in d()
{ …; e(); …}
return address in c()
return address in b()
public void e()
return address in a()
{ …; c(); …}
Rat In A Maze
Rat In A Maze
• Move down.
Rat In A Maze
• Move left.
Rat In A Maze
• Move down.
Rat In A Maze
• Move right.
• Backtrack.
Rat In A Maze
• Move downward.
Rat In A Maze
• Move right.
Rat In A Maze