Stacks and Queues
Stacks and Queues
Narendra L Lokhande
Email: [email protected]
Content
I ADT Stack and Its operations
I Algorithms and their complexity analysis
I Applications of stacks
I Expression Conversion and Evaluation
I ADT Queue
I Type of Queue
I Simple Queue
I Circular Queue
I Priority Queue
I Operations on each type of Queue
Data Structure and Algorithms Using Java Programming
What is Stack?
A stack is a simple data structure used for storing data (similar to
Linked Lists).
In a stack, the order in which the data arrives is important.
Definition: A stack is an ordered list in which insertion and deletion
are done at one end, called top. The last element inserted is the first
one to be deleted. Hence, it is called the Last in First out(LIFO) or First
in Last out (FILO) list.
When an element is inserted in a stack, the concept is called push,
and when an element is removed from the stack, the concept is called
pop.
Trying to pop out an empty stack is called underflow and trying to
push an element in a full stack is called overflow.
Data Structure and Algorithms Using Java Programming
Push Algorithm
Insert a new element ‘data’ at the top of the stack represented by an
array ‘S’ of size ‘Max’ with a stack index variable ‘Top’ pointing to
the topmost element of the stack.
initialization;
if Top = NULL then
Print: “Stack is already Empty, underflow condition”;
Exit;
end
Set Data = S[Top];
Set Top = Top - 1;
Exit;
Algorithm 1: Push Operation
Data Structure and Algorithms Using Java Programming
Pop Algorithm
Delete an element from the stack represented by an array ‘S’ and
returns the element ‘Data’ which is at the top of the stack.
initialization;
if Top = Max then
Print: “Stack is already full”;
Exit;
end
Set Top = Top + 1;
Set S[Top] = data;
Exit;
Algorithm 2: Pop Operation
Data Structure and Algorithms Using Java Programming
Applications of Stack
I Evaluation of Arithmetic Expression
I Infix Notation
I Prefix Notation
I Postfix Notation
I Matching Parenthesis
I Quick Sort
I Recursion
Data Structure and Algorithms Using Java Programming
Operator Priority
Precedence Example
Consider an expression:
e = 4 - 2 ∧ 4 + 8 * 3 + 18 / 3 + 6
∧ is having highest precedence over the other operators will be
solved first
e = 4 - 16 + 8 * 3 + 18 / 3 + 6
Now , * and / operations will be performed from left to right because
both are having same level of precedence
e = 4 - 16 + 24 + 18 / 3 + 6
e = 4 - 16 + 24 + 6 + 6
Now , + and - operations will be performed from left to right because
both are having same level of precedence
e = -12 + 24 + 6 + 6
e = 12 + 6 + 6
e = 18 + 6
e = 24
Data Structure and Algorithms Using Java Programming
Iin = (a − b)/c
= [ab−]/c
IPost = ab − c/
Example 2:
Parenthesis Checking
procedure check()
Declare a character stack S.
Now traverse the expression from left to right
a) If the current character is a starting bracket then push it to stack.
b) If the current character is a closing bracket then pop from stack and
if the popped character is the matching starting bracket then fine else
parenthesis are not balanced.
After complete traversal, if there is some starting bracket left in stack
then “not balanced”
End procedure
Data Structure and Algorithms Using Java Programming
Parenthesis Check