Infix to postfix implementation using linked lists in C


Infix to postfix implementation using linked lists in C



I've been trying to debug this program for a long time. It works fine when i input expressions
like a+b-c or a/b+c where the first operator has a greater or equal precedence than the second. But for expressions like a-b/c where the first operator has a lesser precedence than the second, the compiler throws a breakpoint.


struct stack
{
char ele;
struct stack *next;
};

void push(int);
int pop();
int precedence(char);

struct stack *top = NULL;

int main()
{
char infix[20], postfix[20];
int i=0,j=0;

printf("ENTER INFIX EXPRESSION: ");
gets(infix);

while(infix[i]!='')
{
if(isalnum(infix[i]))
postfix[j++]=infix[i];
else
{
if(top==NULL)
push(infix[i]);

else
{
while(top!=NULL && (precedence(top->ele)>=precedence(infix[i])))
postfix[j++]=pop();
push(infix[i]);
}
}

++i;
}

while(top!=NULL)
postfix[j++]=pop();

postfix[j]='';
puts(postfix);
getchar();
return 0;
}

int precedence(char x)
{
switch(x)
{
case '^': return 4;
case '*':
case '/': return 3;
case '+':
case '-': return 2;

default: return 0;
}
}

void push(int x)
{
int item;
struct stack *tmp;

if(top==NULL)
{
top=(struct stack *)malloc(sizeof(struct stack));
top->ele=x;
top->next=NULL;
}

else
{
tmp=top;
top->ele=x;
top->next=tmp;
}
}

int pop()
{
struct stack *tmp;
int item;

if(top==NULL)
puts("EMPTY STACK");

else if(top->next==NULL)
{
tmp=top;
item=top->ele;
top=NULL;
free(tmp);
}

else
{
tmp=top;
item=top->ele;
top=top->next;
free(tmp);
}

return item;
}



Any advice on how to improve my coding would be helpful.
Thanks for the help, i really appreciate it :)





When I see code and a task like this, I always thank God for having given smart computer scientists the idea of recursive descent parsers...
– user529758
Mar 2 '13 at 11:08






For parsing, think "trees". For example, the expression 1 + 2 can be seen as a tree with + as the root node, and 1 and 2 the child nodes. Having such a tree it's very easy to output it in a postfix, prefix or infix notation. What techniques such as recursive descent does is to make that tree on the stack, instead of creating a tree structure manually, although the latter is more flexible. Recursive-descent is very good at that too.
– Some programmer dude
Mar 2 '13 at 11:13



1 + 2


+


1


2





What does "the compiler throws a breakpoint" mean?
– John Zwinck
Mar 2 '13 at 12:04





For the "breakpoint", run your program in a debugger. It will stop where the problem is, and let you examine the function call-stack to see how you ended up there, and also let you examine variables to help you figure out what the problem might be. A good tip is to look for NULL pointers.
– Some programmer dude
Mar 2 '13 at 12:19


NULL





So who you expect will allocate items other than the top for your stack?
– n.m.
Apr 2 at 9:29





1 Answer
1


it works fine for me.


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{

char data;
struct node *next;


}*top=NULL,*pstart=NULL;
/*-------------------- insertion in postfix expression linked list -------*/


void insert(char ch)
{
struct node *t,*baby;
baby=(struct node *)malloc(sizeof(struct node));
baby->next=NULL;
baby->data=ch;
t=pstart;

if(pstart==NULL)
{
pstart=baby;
}
else
{
while(t->next!=NULL)
t=t->next;
t->next=baby;

}
//printf(" inserted in list- %c",baby->data);

}

/* --------- push operation ------- */



void push (char symbol)
{

struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->data=symbol;
if(top==NULL)
{
top=p;
p->next=NULL;

}
else
{

p->next=top;
top=p;

}
}

char pop()
{
struct node *x,*y;
char k;
if(top==NULL)
{
printf("stack underflown");
return 0;

}
else
{
x=top;
top=top->next;
k=x->data;
//printf("node %d is deletedn",top->data);
free(x);
x=NULL;
return k;


}



}


void displaypost()
{
struct node *to;
if(pstart==NULL)
printf("");
else`enter code here`
{
to=pstart;
while(to!=NULL)
{
printf("%c",to->data);
to=to->next;

}

}


}


/*============== precedence selector ================= */

int precedence(char ch)
{

if(ch=='^')
return (5);
else if(ch=='*' || ch== '/')
return (4);
else if (ch== '+' || ch== '-')
return (3);
else
return (2);
}


/*=================== infix to postfix conversion ================ */

void intopost(char infix)
{

int len;
int index=0;
char symbol,temp;
len= strlen(infix);
//printf("%d",len);
while(len>index)
{
symbol=infix[index];

switch(symbol)
{

case '(':
push(symbol);
break;

case ')':
temp=pop();
while(temp!='(')
{
insert(temp);
temp=pop();
}
break;

case '^':
case '+':
case '-':
case '*':
case '/':
if(top==NULL)
{
push(symbol);
// break;

}
else
{
while(top!=NULL && (precedence(top->data)>=precedence(symbol)))
{
temp=pop();
insert(temp);

}
push(symbol);

}
break;
default:
insert(symbol);

}
index=index+1;


}
while(top!=NULL)
{
temp=pop();
insert(temp);

}
displaypost();
return;




}


int main()
{
char infix[50];
system("clear");
printf("enter infix expression: ");
gets(infix);

printf("nn equivalent postfix expression is---> ");
intopost(infix);
getchar();
return 0;
}
`






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?