Last active
November 20, 2022 15:17
-
-
Save priyadarshitathagat/91c330a3c7c89b84d95f0bff98f8cfb7 to your computer and use it in GitHub Desktop.
Prefix to Postfix conversion in c using stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<conio.h> | |
#include<string.h> | |
#include<stdlib.h> | |
# define MAX 20 | |
char str[MAX],stack[MAX]; | |
int top=-1; | |
void push(char c) | |
{ | |
stack[++top]=c; | |
} | |
char pop() | |
{ | |
return stack[top--]; | |
} | |
void pre_post() | |
{ | |
int n,i,j=0; char c[20]; | |
char a,b,op; | |
printf("Enter the prefix expression\n"); | |
gets(str); | |
n=strlen(str); | |
for(i=0;i<MAX;i++) | |
stack[i]='\0'; | |
printf("Postfix expression is:\t"); | |
for(i=0;i<n;i++) | |
{ | |
if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/') | |
{ | |
push(str[i]); | |
} | |
else | |
{ c[j++]=str[i]; | |
while((top!=-1)&&(stack[top]=='@')) | |
{ | |
a=pop(); c[j++]=pop(); | |
} | |
push('@'); | |
} | |
} | |
c[j]='\0'; | |
printf("%s",c); | |
} | |
main() | |
{ | |
pre_post(); | |
} | |
yeah man... me too..
yeah man... me too..
It (@) acts as a checker or a flag which stops excess popping..
Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
why there is '@' in both 34 and 38th lines