Professional Documents
Culture Documents
Algorithm
BESE-2A/B
Lecture 08
Aasma Zahid
Stacks
• Example
– Which is the first coin to pick up
from the stack of gold coins?
9/28/2012 DSA - Fall 2012 - SEECS, NUST 3
Last in First Out
E top
top
D top D D
C top C C C
B top B B B B
A A A A A
A top
• Top
– Points to the top most element on the stack. This
refers to NULL if stack is empty or size = 0
• Pop
– Remove an item at
the top of stack
9/28/2012 DSA - Fall 2012 - SEECS, NUST 6
Stack Operations
• Push(element) – insert the element on the
top of the stack
• Pop() – removes the top most element
• Peek() – return the top most element without
removing it
• isEmpty() – check to see if stack is empty
• isFull() – check to see if stack is full
• Clear() – clear the stack
9/28/2012 DSA - Fall 2012 - SEECS, NUST 7
Stack Operations
• Computer Science
– Program Execution Stack
– Evaluating Expressions and Syntax Parsing
– Convert decimal to binary
– Tower of Hanoi
– Undo operations
9/28/2012 DSA - Fall 2012 - SEECS, NUST 9
Choice of Implementation
• Array based Stack
– Maximum stack size is known ahead of time
• Linked List based Stack
– Maximum stack size unknown