Infix to Postfix Operation in Stack

To convert infix expression to postfix expression, use the stack data structure. Scan the infix expression from left to right. Whenever we get an operand, add it to the postfix expression and if we get an operator or parenthesis add it to the stack by maintaining their precedence.

Below are the steps to implement the above idea:

  1. Scan the infix expression from left to right
  2. If the scanned character is an operand, put it in the postfix expression. 
  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.) 
  4. If the scanned character is a ‘(‘, push it to the stack. 
  5. If the scanned character is a ‘)’, pop the stack and output it until a ‘(‘ is encountered, and discard both the parenthesis. 
  6. Repeat steps 2-5 until the infix expression is scanned. 
  7. Once the scanning is over, Pop the stack and add the operators in the postfix expression until it is not empty.
  8. Finally, print the postfix expression.

Stack Notes for GATE Exam [2024]

Stacks, a fundamental data structure in computer science, are crucial for understanding algorithmic paradigms and solving complex computational problems. As candidates gear up for the GATE Exam 2024, a solid grasp of stack concepts is indispensable. These notes are designed to provide a concise yet comprehensive overview of stacks, covering key topics that are likely to be assessed in the GATE examination.

Table of Content

  • Introduction to Stack:
  • LIFO (Last In First Out) in Stack:
  • Basic Operations on Stack
  • Implementation of Stack using Singly Linked List:
  • Applications, Advantages and Disadvantages of Stack:
  • Infix to Postfix Operation in Stack:
  • Postfix Evaluation using Stack:
  • Towers of Hanoi using Stack:
  • Fibonaaci Series using Stack:
  • Previously Asked GATE Questions on Stack:

Similar Reads

Introduction to Stack:

A stack is a linear data structure in which the insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack....

LIFO (Last In First Out) in Stack:

This strategy states that the element that is inserted last will come out first. You can take a pile of plates kept on top of each other as a real-life example. The plate which we put last is on the top and since we remove the plate that is at the top, we can say that the plate that was put last comes out first....

Basic Operations on Stack

In order to make manipulations in a stack, certain operations are provided to us....

Implementation of Stack using Singly Linked List:

To implement a stack using the singly linked list concept, all the singly linked list operations should be performed based on Stack operations LIFO(last in first out) and with the help of that knowledge, we are going to implement a stack using a singly linked list....

Applications, Advantages and Disadvantages of Stack:

Application of Stack Data Structure:...

Infix to Postfix Operation in Stack:

To convert infix expression to postfix expression, use the stack data structure. Scan the infix expression from left to right. Whenever we get an operand, add it to the postfix expression and if we get an operator or parenthesis add it to the stack by maintaining their precedence....

Postfix Evaluation using Stack:

To evaluate a postfix expression we can use a stack....

Towers of Hanoi using Stack:

Tower of Hanoi is a mathematical puzzle where we have three rods (A, B, and C) and N disks. Initially, all the disks are stacked in decreasing value of diameter i.e., the smallest disk is placed on the top and they are on rod A. The objective of the puzzle is to move the entire stack to another rod (here considered C), obeying the following simple rules:...

Fibonaaci Series using Stack:

The Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….....

Previously Asked GATE Questions on Stack:

Question 1:...