Write a C Program to convert prefix to postfix using stack

By | 13.04.2017

Prefix to postfix using stack


Write a C Program to convert prefix to postfix using stack. Here’s simple Program to convert prefix to postfix using stack in C Programming Language.


What is Stack ?


Stack is an abstract data type with a bounded(predefined) capacity. It is a simple data structure that allows adding and removing elements in a particular order. Every time an element is added, it goes on the top of the stack, the only element that can be removed is the element that was at the top of the stack, just like a pile of objects.


Basic Operations : :


  • push() − Pushing (storing) an element on the stack.
  • pop() − Removing (accessing) an element from the stack.
  • peek() − get the top data element of the stack, without removing it.
  • isFull() − check if stack is full.
  • isEmpty() − check if stack is empty.

Below is the source code for C Program to convert prefix to postfix using stack which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to convert prefix to postfix using stack */

#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 symbol);
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 symbol;
        char temp[2];
        char strin[MAX];
        for(i=strlen(prefix)-1;i>=0;i--)
        {
                symbol=prefix[i];
                temp[0]=symbol;
                temp[1]='\0';
                                
                if(!white_space(symbol))
                {
                        switch(symbol)
                        {
                        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("\nStack 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 symbol)
{
        if(symbol==BLANK || symbol==TAB || symbol=='\0')
                return 1;
        else
                return 0;
}/*End of white_space()*/

OUTPUT : :


/*  C Program to convert prefix to postfix using stack */

---------------------------------------------------------

Enter Prefix Expression : + + A * B C D

Postfix Expression :: ABC*+D+

Process returned 0


---------------------------------------------------------

Enter Prefix Expression : / 10 5 = 2

Postfix Expression :: 2

Process returned 0

---------------------------------------------------------

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3.3 3 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mohd Haadi Akhter

Why are you taking a 2D array of stack?

Reva

Can you explain the if statement and the switch case statement