×

Data Structure Prefix to Postfix Conversion

Prefix to Postfix Conversion

Prefix: As the name suggests if the operator placed before the operands called the prefix expression.  The form of prefix expression is (operator, operand1, operand2).

Example:  *+EF-GH (Infix: (E+F) * (F-H))

Postfix: In postfix expression, the operator placed after the operands and the form of postfix expression is (operand1, operand2, operator).

Example: EF+GH-* (Infix: (E+F * (G-H))

Algorithm:

  • Firstly, we read input or prefix expression from right to left or we can say in reverse order.
  • If we encounter an operand, then we push it into the stack.
  • If we encounter an operator, then we pop two operands from the stack.
  • Then we are required to make a string which concatenate the two operands and the operator after them.
  • String = operand1 + operand2 + operator
  • Then we push the output or resultant string back to the stack.
  • We need to repeat above steps until the end of prefix expression.

Examples: 

Prefix:  * + E F – G H
Postfix: E F + G H - *
Explanation: Prefix to Infix: (E + F) * (G – H)
             Infix to Postfix:  E F + G H - *
 
Prefix:  * - E / F G - / E K L
Postfix: E F G / - E K / L - *
Explanation: Prefix to Infix: (E - ( F / G ) ) * ( ( E / K ) – L )
    Infix to Postfix :  E F G / - E K / L - * 
 

C- Program to covert prefix expression to postfix expression:

 #include<stdio.h>
 #include<string.h>
 #include<math.h>
 #include<stdlib.h>
 #define BLANK ' '
 #define TAB '\t'
 #define MAX 50
 char *pop();
 char prefix[MAX];
 char stack[MAX][MAX];
 void push(char *str);
 int isempty();
 int white_space(char element);
 void prefix_to_postfix();
 int top;
 int main()
 {
         top  =  -1;
         printf(" Enter Prefix Expression :  ");
         gets(prefix);
         prefix_to_postfix();
 }
 /* End of main() */
 void prefix_to_postfix()
 {
         int i;
         char operand1[MAX], operand2[MAX];
         char element;
         char temp[2];
         char strin[MAX];
         for(i = strlen(prefix)-1; i >= 0; i--)
         {
                 element = prefix[i];
                 temp[0] = element;
                 temp[1] = '\0';
                 if(!white_space(element))
                 {
                         switch(element)
                         {
                         case '+':
                         case '-':
                         case '*':
                         case '/':
                         case '%':
                         case '^':
                                 strcpy(operand1,pop());
                                 strcpy(operand2,pop());
                                 strcpy(strin,operand1);
                                 strcat(strin,operand2);
                                 strcat(strin,temp);            
                                 push(strin);
                                 break;
                         default:
 /* if an operand comes */
                              push(temp);
                         }
                 }
         }
         printf("\nPostfix Expression :: ");
         puts(stack[0]);
 }
 /* End of prefix_to_postfix()  */
 void push(char *str)
 {
         if(top  >  MAX)
         {
                 printf("\nStack overflow\n");
                 exit(1);
         }
         else
         {
                 top = top+1;
                 strcpy( stack[top], str);
         }
 }
 /* End of push() */
 char *pop()
 {
         if(top  ==  -1 )
         {
                 printf("\n Stack underflow \n");
                 exit(2);
         }
         else
                 return (stack[top--]);
 }
 /*  End of pop()  */
 int isempty()
 {
         if(top==-1)
                 return 1;
         else
                 return 0;
 }
 int white_space(char element)
 {
         if(element == BLANK || element == TAB || element == '\0')
                 return 1;
         else
                 return 0;
 } 

Output: -

Time Complexity:  -  

The time complexity to convert prefix expression to postfix expression is O(n) and space complexity is also O(n).


Related Topics

Arrange consonants and vowels nodes in a linked list

Arrange consonants and vowels nodes in a linked list In this problem, we have given a singly linked list. Here we will arrange the consonants and vowels nodes of the list...

2 minutes read.

Merge two sorted linked lists

Merge two sorted linked lists In this article, we are going to learn how to merge two linked lists. Here we have given two linked lists that are sorted in increasing...

7 minutes read.

DFS (Depth-first search) Algorithm: Data Structure

What is DFS (Depth-first search)? The depth first search is a graph traversal algorithm. The idea behind this algorithm is backtracking and it is a kind of recursive algorithm. In the...

3 minutes read.

Create a binary search tree

Implementation In this section of the article, we will see the usage and mechanism of how we will create a given binary tree. Let's observe these in more depth and then...

7 minutes read.

FLEX (Fast Lexical Analyzer Generator)

FLEX stands for Fast Lexical Analyzer Generator. Around 1987, Vern Paxson created Flex in C with a great deal of input and inspiration from Van Jacobson. Van Jacobson's approach is...

3 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.

Deletion in B+ Tree

Make a search for the leaf node that containing the key value by taking the value in a key value. If the required key value is found, then it will remove...

6 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Sorting Algorithms

Sorting: In the data structure, sorting is the process by which you arrange the data in a logical order. This logical order can also be an ascending order or a...

7 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Convert Binary Tree into a Threaded Binary Tree

Implementation /*Writing a C++ program that will help us change the binary tree into a threaded binary tree and help us transform. */ #include <bits/stdc++.h> using namespace std; /*Creating the structure of a node...

11 minutes read.

Sorting Algorithms in Data Structures

A sorting algorithm is used to organize the elements of an array or list. Sorting an array, for example. Unsorted array 572941 Sorted array 124579 We're sorting the array in ascending order right now. This procedure...

4 minutes read.

Singly Linked list

Singly Linked list A singly linked list is a kind of linked list which is unidirectional. If we talk about singly linked list, then we can say it can be traversed...

3 minutes read.

Assembly Line Scheduling

If we take an example of a car factory, there are two assembly lines. In an assembly line, we can assemble and repair the parts of a car. Now, suppose...

5 minutes read.

Cycle sort

Cycle sort is an examination arranging calculation which powers exhibit to be figured into the quantity of cycles where every one of them can be pivoted to create an arranged...

5 minutes read.

Insertion Sort vs Bubble Sort

In this article, we will see the major differences between Insertion Sort and Bubble Sort. Before that, let’s have a quick overview of what these sorting algorithms are and what’s...

4 minutes read.

What is the difference between DFS and BFS?

What is BFS? BFS is generally known as the low level traversal. As we already know that it stands for breadth first search and is mainly used in the queue data...

4 minutes read.

Horizontal and Vertical Scaling

Being a software engineer, you would have designed a website or application and deployed it on any server. Imagine that the developed application starts getting popular, and many users engage...

6 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

Graph Data Structure

A graph is a non-primitive and non-linear data structure. It is a group of (V, E) where V is a set of vertexes, and E is a set of edge....

3 minutes read.