×

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

Stack vs Heap Memory Allocation Data Structure

Difference Between Stack and Heap Memory Allocation Stack Memory Stack memory allocation is a way to use the system memory as a temporary storage of the data which is act like last-in-first-out...

3 minutes read.

Advantages and Disadvantages of Linked List

Advantages of Linked List The linked list is a dynamic data structure.You can also decrease and increase the linked list at run-time. That is, you can allocate and deallocate memory at...

3 minutes read.

Find all possible words from board

We have been given a dictionary of words and a board of characters from which we can form strings. Now, we have to check if the string is present in...

5 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Operations on 1D-Arrays

One Dimensional Array Operations Basic Methods The fundamental operations enabled by an array are listed below. Traverse prints each element of the array one by one.Insert a new element at the specified index.Delete...

8 minutes read.

Data Structure Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Heap Data Structure

In this article, we will learn in detail about Heap (Min heap and Max heap). Before going to the main topics, let’s have a look at what is complete binary...

19 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

8 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Heap Sort in Data Structure

Heap Sort A heap is a tree-based data structure that has specific properties. Heap is always a complete binary tree (CBT). That is, all the nodes of the tree are completely filled.If...

6 minutes read.

Strictly binary tree in Data Structures?

What is a strictly Binary Tree in Data Structures? There are various kinds of binary trees that we know exist in data structures, and they all have their purposes. In this...

4 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.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.

Berkley’s Algorithm

Berkley’s Algorithm is mainly used in clock synchronization system. It is used in distributed systems. To implement this algorithm, we have to think that the network has no accurate time...

4 minutes read.

Sort the linked list of 0s, 1s and 2s

Sort the linked list of 0s, 1s and 2s In this, we are given a linked list of 0s, 1s, and 2s, and we need to sort it. Examples: Input: 1  ->  1 ...

2 minutes read.

Treap data structure

In this article, we will discuss the treap data structure. The word treap is a combination of 'tree' and 'heap'. So, treap data structure is a combination of a heap...

8 minutes read.

Compare Balanced Binary Tree and Complete Binary Tree

Complete and balanced binary trees are important and general topics in the concept – Tree data structure. Before discussing the complete and balanced binary tree, we need to have an...

8 minutes read.

Common Operations on various Data Structures

Data structures are ways to organise data in computer memory for quick and effective use. The storage of data uses a variety of data-structures. It is also possible to define...

7 minutes read.

Stack Using Array

Stack – A Stack is a linear abstract data type used to store elements. It is also called last in first out or first in last out data structure because...

6 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.