C/C++ Program to Convert Infix to Postfix Expression Using Linked Lists

You are currently viewing C/C++ Program to Convert Infix to Postfix Expression Using Linked Lists

Now, I’m going to present the tutorial with the concept of “C/C++ Program to Convert Infix to Postfix Expression Using Linked Lists“. Without wasting the time, Move on to the article directly.

C/C++ Program to Convert Infix to Postfix Expression Using Linked Lists

Source Code

#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;
}

Conclusion

I hope this article will help you to know about “C/C++ Program to Convert Infix to Postfix Expression Using Linked Lists“. Please let us know via the comment section if you have any issues during the program execution. Don’t forget to bookmark us.

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply