#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int count=0;
typedef struct node{
char value;
struct stack *next;
struct stack *prev;
}stack;
stack *bottom=NULL;
stack *top=NULL;
stack *createnode(char a)
{
stack *newstack;
newstack
=(stack
*)calloc(1,sizeof(stack
)); newstack->value=a;
newstack->prev=NULL;
newstack->next=NULL;
return newstack;
}
int isempty()
{
if(count==0)
{return 1;}
else
{return 0;}
}
int isfull()
{
if(count==SIZE)
{return 1;}
else
{return 0;}
}
void push(char a)
{ if(isfull()==1)
{
printf("overflow condition: stack full"); }
else
{count+=1;
stack *newstack = createnode(a);
if(bottom==NULL)
{
bottom=newstack;
top=bottom;
}
else
{
top->next=newstack;
newstack->prev=top;
top=newstack;
}
}
}
char peek()
{
return top->value;
}
char pop()
{ if(isempty()==1)
{
printf("underflow condition:empty stack"); }
else
{if(count==1)
{ char c;
c=top->value;
top=NULL;
bottom=NULL;
count=count-1;
return c;
}
else
{char c;
c=top->value;
top=top->prev;
top->next=NULL;
count=count-1;
return c;
}
}
}
char plus=43;
char multi=42;
char divi=47;
char power=94;
char left=40;
char right=41;
char subtract=45;
int precedence(char c)
{ int precide=0;
if(c==subtract)
{precide+=0;}
if(c==plus)
{precide+=1;}
if(c==multi)
{precide+=2;}
if(c==divi)
{precide+=3;}
if(c==power)
{precide+=4;}
if(c==left || c==right)
{precide+=5;}
return precide;
precide=0;
}
char *postfix_converter(char *string,int length)
{ char s[100]="\0"; int i; stack operators;
for(i=0;i<length;i++)
{
char c;
c=string[i];
if(c==plus || c==subtract || c==multi || c==divi || c==power || c==left)
{
if(top==NULL)
{
push(c);
}
else
{
if(precedence(c)>precedence(peek()) || peek()==left)
{
push(c);
}
else
{
char r[2]="\0";
r[0]=pop();
push(c);
}
}
}
else
{
if(c==right)
{
while(peek()!=left)
{
char r[2]="\0";
r[0]=pop();
}
pop();
}
else
{
char r[2]="\0";
r[0]=c;
// printf("%s\n",s);
// char j=peek();
// printf("%c",j);
}
}
if(i==(length-1))
{// printf("%d",count);
while(count>0)
{
char r[2]="\0";
r[0]=pop();
// printf("lool");
}
}
// printf("%d\n",count);
// printf("%s\n",s);
}
//printf("%d\n",pop());
}
// driver code , the main function
int main(void)
{ char inversion[200];
printf("give the infix expression\n"); postfix_converter(inversion,longer);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojZGVmaW5lIFNJWkUgIDEwMCAgIAppbnQgY291bnQ9MDsgICAgICAgIAp0eXBlZGVmIHN0cnVjdCBub2RlewogY2hhciB2YWx1ZTsKIHN0cnVjdCBzdGFjayAqbmV4dDsKIHN0cnVjdCBzdGFjayAqcHJldjsKfXN0YWNrOwoKc3RhY2sgKmJvdHRvbT1OVUxMOwpzdGFjayAqdG9wPU5VTEw7CgpzdGFjayAqY3JlYXRlbm9kZShjaGFyIGEpCnsKIHN0YWNrICpuZXdzdGFjazsKIG5ld3N0YWNrPShzdGFjayopY2FsbG9jKDEsc2l6ZW9mKHN0YWNrKSk7CiBuZXdzdGFjay0+dmFsdWU9YTsKIG5ld3N0YWNrLT5wcmV2PU5VTEw7CiBuZXdzdGFjay0+bmV4dD1OVUxMOwogcmV0dXJuIG5ld3N0YWNrOwp9CgppbnQgaXNlbXB0eSgpCiAgIHsKICAgICAgIGlmKGNvdW50PT0wKQogICAgICAge3JldHVybiAxO30KICAgICAgIGVsc2UKICAgICAgIHtyZXR1cm4gMDt9CiAgICB9CgppbnQgaXNmdWxsKCkKICAgIHsKICAgICBpZihjb3VudD09U0laRSkKICAgICB7cmV0dXJuIDE7fQogICAgIGVsc2UKICAgICB7cmV0dXJuIDA7fQogICAgfQoKdm9pZCBwdXNoKGNoYXIgYSkgCnsgICBpZihpc2Z1bGwoKT09MSkgCiAgIHsKICAgIHByaW50Zigib3ZlcmZsb3cgY29uZGl0aW9uOiBzdGFjayBmdWxsIik7CiAgIH0KIGVsc2UgICAgICAgICAgICAgICAKICAge2NvdW50Kz0xOyAgICAgICAKIHN0YWNrICpuZXdzdGFjayA9IGNyZWF0ZW5vZGUoYSk7CiBpZihib3R0b209PU5VTEwpICAgCiB7CiBib3R0b209bmV3c3RhY2s7CiB0b3A9Ym90dG9tOwogfQogZWxzZSAgICAgICAgICAgICAgIAogewogIHRvcC0+bmV4dD1uZXdzdGFjazsKICBuZXdzdGFjay0+cHJldj10b3A7CiAgdG9wPW5ld3N0YWNrOwogfQogICB9IAp9CmNoYXIgcGVlaygpICAgICAgICAgCnsgCiAgIAlyZXR1cm4gdG9wLT52YWx1ZTsKfQpjaGFyIHBvcCgpICAgICAgICAKICAgIHsgICBpZihpc2VtcHR5KCk9PTEpICAgICAgICAgIAogICAgICAgIHsKICAgICAgICAgcHJpbnRmKCJ1bmRlcmZsb3cgY29uZGl0aW9uOmVtcHR5IHN0YWNrIik7CiAgICAgICAgfQogICAgICAgIGVsc2UgCiAgICAgICAge2lmKGNvdW50PT0xKSAgICAgICAgICAgICAKICAgICAgICB7ICAgY2hhciBjOwogICAgICAgICBjPXRvcC0+dmFsdWU7CiAgICAgICAgIHRvcD1OVUxMOwogICAgICAgICBib3R0b209TlVMTDsKICAgICAgICAgY291bnQ9Y291bnQtMTsKICAgICAgICAgcmV0dXJuIGM7CiAgICAgICAgIAogICAgICAgIH0KICAgICAgICBlbHNlICAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAge2NoYXIgYzsKICAgICBjPXRvcC0+dmFsdWU7CiAgdG9wPXRvcC0+cHJldjsKICB0b3AtPm5leHQ9TlVMTDsKICBjb3VudD1jb3VudC0xOwogIHJldHVybiBjOwogIAogICAgIH0KICAgICAgIH0gCiAgICB9CiAgICBjaGFyIHBsdXM9NDM7CiAgICBjaGFyIG11bHRpPTQyOwogICAgY2hhciBkaXZpPTQ3OwogICAgY2hhciBwb3dlcj05NDsKICAgIGNoYXIgbGVmdD00MDsKICAgIGNoYXIgcmlnaHQ9NDE7CiAgICBjaGFyIHN1YnRyYWN0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