C Program for conversion of postfix to prefix using stack

By | 13.04.2017

Postfix to Prefix using stack


Write a C Program for conversion of postfix to prefix using stack. Here’s simple Program for conversion of postfix to prefix 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 Program for conversion of postfix to prefix using stack which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program for conversion of postfix to prefix 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 postfix[MAX];
char stack[MAX][MAX];
void push(char *str);
int isempty();
int white_space(char symbol);
void postfix_to_prefix();
int top;

int main()
{
        top = -1;
        printf("Enter postfix expression : ");
        gets(postfix);
        postfix_to_prefix();

        return 0;

}/*End of main()*/

void postfix_to_prefix()
{
        unsigned int i;
        char operand1[MAX], operand2[MAX];
        char symbol;
        char temp[2];
        char strin[MAX];
        for(i=0;i<strlen(postfix);i++)
        {
                symbol=postfix[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,temp);
                                strcat(strin,operand2);
                                strcat(strin,operand1);
                                push(strin);
                                break;
                        default: /*if an operand comes*/
                             push(temp);
                        }
                }
        }
        printf("\nPrefix Expression :: ");
        puts(stack[0]);
}/*End of postfix_to_prefix()*/


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 for conversion of postfix to prefix using stack  */

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

Enter postfix expression : A B + C + D +

Prefix Expression :: +++ABCD

Process returned 0

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

Enter postfix expression : 123+*

Prefix Expression :: *1+23

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 2 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
shivam

thank you,I was searching for this program everywhere