Skip to content

Instantly share code, notes, and snippets.

@priyadarshitathagat
Last active November 20, 2022 15:17
Show Gist options
  • Save priyadarshitathagat/91c330a3c7c89b84d95f0bff98f8cfb7 to your computer and use it in GitHub Desktop.
Save priyadarshitathagat/91c330a3c7c89b84d95f0bff98f8cfb7 to your computer and use it in GitHub Desktop.
Prefix to Postfix conversion in c using stack
#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();
}
@y01cu
Copy link

y01cu commented Mar 22, 2022

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment