The use of a stack to convert infix to postfix results in an unending loop

The code for infix to postfix conversion using stack is shown below. The code runs an infinite loop, reporting stack underflow in one of the pop functions. I tried commenting out the pop function call in the right parenthesis loop, but there is no output. I’m not sure which pop function causes this problem. To get my solution I looked up on google and found this (Scaler). It would be very appreciated if someone could assist me in determining the source of the problem.

If I remove the parentheses from the infix equation that I am supplying, the code runs well.

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

struct stack{
    int size;
    int top;
    char *arr;
};

void display(struct stack *ptr)
{
    if(ptr->top == -1)
    {
        printf("Stack is Empty");
    }
    
    else
    {
        for(int i = ptr->top ; i>=0 ; i--)
        {
            printf("Element: %d\n",ptr->arr[i]);
            
        }
    }
}

int isEmpty(struct stack *ptr)
{
    if(ptr->top == -1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int isFull(struct stack *ptr)
{
    if(ptr->top == ptr->size - 1)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void push(struct stack *ptr,int data)
{
    if(isFull(ptr))
    {
        printf("Stack Overflow");
    }
    else
    {
        ptr->top = ptr->top + 1;
        ptr->arr[ptr->top] = data;
    }    
}

char pop(struct stack *ptr)
{
    if(isEmpty(ptr))
    {
        printf("Stack Underflow");
        return 0;
    }
    else
    {
        char ch = ptr->arr[ptr->top];
        ptr->top = ptr->top - 1;
        return ch;

    }    
}

char stackTop(struct stack *ptr)
{
    return ptr->arr[ptr->top];
}

int isOperator(char a)
{
    if(a == '+'|| a == '-'|| a == '*'|| a == '/')
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int precedence(char a)
{
    if(a == '*' || a == '/')
    {
        return 3;
    }
    else if(a == '+' || a == '-')
    {
        return 2;
    }
    else
    {
        return -1;
    }
}

char* infix_postfix(char *infix)
{
    struct stack *sp = (struct stack *) malloc(sizeof(struct stack));

    sp->size = 100;
    sp->top = -1;
    sp->arr = (char *) malloc(sp->size * sizeof(char));
    char *postfix = (char *) malloc((strlen(infix+1)) * sizeof(char));

    int i=0; 
    int j=0;
    
    while(infix[i] != '\0')
    {
        if(infix[i] == '(')
        {
            push(sp,infix[i]);
            i++;
        }
        else if(infix[i] == ')')
        {
            while(!(isEmpty(sp)) && stackTop(sp) != '(')
            {
                postfix[j] = pop(sp);
                j++;
            }
      
            pop(sp);
               
        }
        else if(!isOperator(infix[i]))
        {
            postfix[j] = infix[i];
            i++;
            j++;
        }
        else
        {
            while(!(isEmpty(sp)) && precedence(infix[i])<=precedence(stackTop(sp)))
            {
                postfix[j] = pop(sp);
                j++;
                
            }
            
            push(sp,infix[i]);
            i++;
            
        }
    }
    while(!isEmpty(sp))
    {
        postfix[j] = pop(sp);
        j++;
    }

    postfix[j] = '\0';
    return postfix;
}

int main(void)
{
    char *infix = "(x-y/z-k*d)";

    printf("postfix is %s",infix_postfix(infix));

    return 0;
}

(post deleted by author)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.