You are on page 1of 20

Stack and its

Lecture on
Applications
Stack
Stack

• One side open another side closed


• Works on LIFO (Last in First Out) or FILO (First in Last Out)
• Top is a variable which contains the position of the Top Most
element in the stack.
Stack Basic Operations
push() − Insert element on the stack.

pop() − Removing an element from the stack.

peek() − Accessing the top data element of the stack, without removing it.

isFull() − To check overflow condition (required when you want to insert new element).

isEmpty() − check if stack is empty (required when you want to delete an element).
Stack Creation: Using Array

int stack[100], n = 100, top = -1;


Stack Operations: Traversal
void traverse() {
if(top>= 0) {
cout<<"Stack elements are:";
for(int i = top; i>= 0; i--)
cout<<stack[i]<<" ";
cout<<endl;
}
else
cout<<"Stack is empty";
}
Stack Operation: Push (insertion)
void push(int val) {
if(top >= n-1)
cout<<"Stack Overflow"<<endl;
else {
top++;
stack[top] = val;
}
}
Stack Operation: Pop (Deletion)
void pop() {
if(top <= -1)
cout<<"Stack Underflow"<<endl;
else {
cout<<"The popped element is "<< stack[top] <<endl;
top--;
}
}
Applications of Stack
• Recursion
 Tail recursion
 Non Tail Recursion
 Indirect Recursion
 Nested Recursion
• Infix to Postfix
• Prefix to postfix
• Tower of Hanoi
• Fibonacci Series
• Depth First Search (DFS)
Infix to Postfix Conversion
Problem 1:
Infix: a+b*c
Postfix:?????

Problem 2:
Infix: a+b-c
Postfix:?????

Problem 3:
Infix: a + b * c / d ^ e ^ f * g – h ^ i
Postfix:?????
Infix to Postfix Conversion

Infix: a + b * c / d ^ e ^ f * g – h ^ i
Infix to Postfix Conversion
Problem 1:
Infix: (a+b)*c
Postfix:?????

Problem 2:
Infix: a+(b-c)
Postfix:?????

Problem 3:
Infix: a + b * (c / (d ^ e)) ^ ((f * g) – h) ^ i
Postfix:?????
Infix to Prefix Conversion
Problem 1:
Infix: a+b*c
Postfix:?????

Problem 2:
Infix: a+b-c
Postfix:?????

Problem 3:
Infix: a + b * c / d ^ e ^ f * g – h ^ i
Postfix:?????
Infix to Prefix Conversion
Problem 1:
Infix: (a+b)*c
Postfix:?????

Problem 2:
Infix: a+(b-c)
Postfix:?????

Problem 3:
Infix: a + b * (c / (d ^ e)) ^ ((f * g) – h) ^ i
Postfix:?????
Infix to Postfix Conversion using Stack
Step 1: Scan the infix expression from left to right.
Step 2: If the scanned character is an operand, put it in the postfix expression.
Step 3: Otherwise, do the following
 If the precedence and associativity of the scanned operator are greater than the precedence and associativity of the operator in the stack [or the stack is empty or
the stack contains a ‘(‘ ], then push it in the stack. [‘^‘ operator is right associative and other operators like ‘+‘,’–‘,’*‘ and ‘/‘ are left-associative].
 Check especially for a condition when the operator at the top of the stack and the scanned operator both are ‘^‘. In this condition, the precedence of the
scanned operator is higher due to its right associativity. So it will be pushed into the operator stack.
 In all the other cases when the top of the operator stack is the same as the scanned operator, then pop the operator from the stack because of left
associativity due to which the scanned operator has less precedence.
 Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator.
 After doing that Push the scanned operator to the stack. (If you encounter parenthesis while popping then stop there and push the scanned operator in
the stack.)
 If the scanned character is a ‘(‘, push it to the stack.
 If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is encountered, and discard both the parenthesis.
 Repeat steps 2-5 until the infix expression is scanned.
 Once the scanning is over, Pop the stack and add the operators in the postfix expression until it is not empty.
Infix to Postfix Conversion using stack
Infix Expression: A+ (B*C-(D/E^F)*G)*H
Postfix: ??

Symbol Priority Associativity


+, - 1 L-R
*, / 2 L-R
^ 3 R-L
() higher L-R
Infix to Postfix Conversion using stack
Infix Expression: A+ (B*C-(D/E^F)*G)*H
Postfix: ??

Symbol Priority Associativity


+, - 1 L-R
*, / 2 L-R
^ 3 R-L
() higher L-R

Infix: a + b * c / d ^ e ^ f * g – h ^ i
Postfix:?????
Infix to Postfix Conversion using stack
Infix: a + b * (c / (d ^ e)) ^ ((f * g) – h) ^ i
Postfix:?????
Infix to Postfix Conversion using stack
a+b*c/d^e^f*g–h^i
Thank You

You might also like