Stacks and Queues: Saurav Karmakar Spring 2007
Stacks and Queues: Saurav Karmakar Spring 2007
Saurav Karmakar
Spring 2007
Objective
In this chapter we will learn:
Stacks
Queues
Different implementations (arrays and
linked list) of both
Comparison of implementation
Stack
A stack is a data structure that
works on the principle of
Last In First Out (LIFO).
As Vector
Storing the items contiguously .
As List
Storing items noncontiguously.
Stack
Can be implemented with an array and
an integer that indicates the top
element of the stack (tos).
tos=1 b
tos=0 a a
tos=0
a
tos= -1
Stack – Vector Implementation
template <class Object>
A stack can be implemented with class Stack{
an vector and an integer that {
indicates the index of the top public:
element. Stack( );
//construct the stack bool isEmpty( ) const;
Template <class Object> const Object & top( ) const;
Stack<Object>::Stack():the Array(1){ void makeEmpty( );
topOfStack = -1; void pop( );
} void push( const Object & x );
//test if the stack is logically empty Object topAndPop( );
Template <class Object> private:
Stack<Object>::isEmpty() const{ vector<Object> theArray;
return topOfStack == -1; int topOfStack;
} };
The push/pop function (vector-based)
template <class Object>
void Stack<Object>::push( const Object & x )
{
if( topOfStack == theArray.size( ) - 1 )
theArray.resize( theArray.size( ) * 2 + 1 );
theArray[ ++topOfStack ] = x;
} // If there is no vector doubling, push takes constant time, otherwise it
// takes O(N) time. But it does not happen often.
topOfStack
Linked List
Implementation of Stack
Each stack item stores
element value
pointer to next element
Application of Stack
Recognizing palindromes
Checking balanced expressions
Evaluating algebraic expressions is easier.
Searching networks, traversing trees
(keeping a track where we are).
Infix to Postfix Conversion
Scan the Infix string from left to right.
Initialise an empty stack.
If the scannned character is an operand, add it to the Postfix string.
If the scanned character is an operator and if the stack is empty Push
the character to stack.
If the scanned character is an Operator and the stack is not
empty, compare the precedence of the character with the
element on top of the stack (topStack).
If topStack has higher precedence over the scanned
character Pop the stack
else Push the scanned character to stack.
Repeat this step as long as stack is not empty and topStack has
precedence over the character.
Repeat this step till all the characters are scanned.
(After all characters are scanned, we have to add any character that the
stack may have to the Postfix string.) If stack is not empty add topStack
to Postfix string and Pop the stack.
Repeat this step as long as stack is not empty.
Return the Postfix string.
Example : String a+b*c-d
STACK PostfixString
Postfix Expression Evaluation
for each character C in a given string
{
if C is an operand
push C onto stack;
else // C is an operator
{
pop item from stack, and store in Opr2;
pop item from stack, and store in Opr1;
result = Opr1 C Opr2, using C as an operator;
push result onto stack;
}
}
QUEUE
A queue is an abstract
data struture where
various entities such as
data, objects, persons, or
events are stored and
waiting to be processed.
Back
Step 4. dequeue()
a b
Step 3. enqueue() After dequeue() :
Back Back
a b b
Better Idea
Keep a Front index.
To Dequeue, increment Front. Therefore,
Dequeue takes constant time now.
a b c d
Front Back
a b c d
Front Back
Queue
Step 1. makeEmpty()
Step 4. dequeue()
Back
Back
Front
Step 2. enqueue() b
Back
Front
a
After dequeue() :
Front Step 3. enqueue()
Back
Back
a b b
Front Front
Circular Implementation
c d
Front
e c d
Front
Circular Example
Both Front and Back wraparound as
needed.
b c d e f
Front Back
g b c d e f
Back Front
QUEUE--Vector Implementation
Mostly straightforward; maintain
Front
Back
front back
Comparison of the Two Methods