0

Can anyone please help me with this code? I'm trying to convert from infix to postfix using a linked list. With an array, I can easily solve this, that solution is also similar to this one. But I'm not getting why this is not working. My guess is I've done anything wrong in this part -

if (precedence(ch) > precedence(top->data))
{
    push(ch);
}
else
{
    while (precedence(ch) <= precedence(top->data))
    {
        postfix = insert(postfix, pop());
    }
    push(ch);
}

Here is the complete code. Thanks.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100

struct Node
{
    char data;
    struct Node *next;
};

struct Node *top = NULL;
struct Node *postfix = NULL;

void push(char ch);
char pop();
int is_operator(char ch);
int precedence(char ch);
void convert(char *infix);
struct Node *insert(struct Node *head, char ch);
void print(struct Node *head);

int main()
{
    char infix[MAX_SIZE] = "(A+B^C)*D+E^5";
    printf("Infix: %s\n", infix);
    convert(infix);
    printf("Postfix: ");
    print(postfix);
}

void convert(char *infix)
{
    int i, len = strlen(infix);
    char ch;

    for (i = 0; i < len; i++)
    {
        ch = infix[i];
        if (is_operator(ch) == 0)
        {
            postfix = insert(postfix, ch);
        }
        else
        {
            if (ch == '(')
            {
                push(ch);
            }
            else
            {
                if (ch == ')')
                {
                    while (top->data != '(')
                    {
                        postfix = insert(postfix, pop());
                    }
                    pop();
                }
                else
                {
                    if (precedence(ch) > precedence(top->data))
                    {
                        push(ch);
                    }
                    else
                    {
                        while (precedence(ch) <= precedence(top->data))
                        {
                            postfix = insert(postfix, pop());
                        }
                        push(ch);
                    }
                }
            }
        }
    }
    while (top != NULL)
    {
        postfix = insert(postfix, pop());
    }
}

int is_operator(char ch)
{
    switch (ch)
    {
    case '+':
    case '-':
    case '*':
    case '/':
    case '^':
    case '(':
    case ')':
        return 1;
    default:
        return 0;
    }
}

int precedence(char ch)
{
    switch (ch)
    {
    case '+':
    case '-':
        return 2;
    case '*':
    case '/':
        return 3;
    case '^':
        return 4;
    default:
        return 1; // if (, ), or empty
    }
}

void push(char ch)
{
    struct Node *newP = (struct Node *)malloc(sizeof(struct Node));
    newP->data = ch;
    newP->next = top;
    top = newP;
}

char pop()
{
    char ch = top->data;
    struct Node *temp = top;
    top = temp->next;
    free(temp);
    temp = NULL;
    return ch;
}

struct Node *insert(struct Node *head, char ch)
{
    struct Node *newP = (struct Node *)malloc(sizeof(struct Node));
    newP->data = ch;
    newP->next = NULL;
    if (head == NULL)
    {
        head = newP;
    }
    else
    {
        struct Node *temp = head;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = newP;
    }
    return head;
}

void print(struct Node *head)
{
    while (head != NULL)
    {
        printf("%c", head->data);
        head = head->next;
    }
    printf("\n");
}
7
  • I ran your program and got a segfault. I rebuilt it with debug symbols and without optimization, and ran it under control of a debugger (gdb). That identified the exact line of code on which the segfault arose, and it allowed me to examine the relevant variables to determine what the nature of the problem was. These are skills that you should cultivate for yourself, so go and do likewise. Commented Oct 15, 2021 at 19:29
  • Actually, I am not getting any errors. Infix: (A+B^C)*D+E^5 this got printed and then the program exit. Commented Oct 16, 2021 at 3:15
  • The program exits prematurely, without printing the postfix, because it has crashed. It's a shame that your C implementation is so coy about that. But the fact remains that a debugger is your friend here, and developing skill in using one will serve you well. Commented Oct 16, 2021 at 4:00
  • I've tried your method. Program is crashing because of this if (precedence(ch) > precedence(top->data)), can you please give me any idea how can I solve this? Commented Oct 16, 2021 at 4:07
  • Progress. Now, do the rest of what I described: use your debugger to examine the values of the variables involved in the crash. That way you can discover why the crash is occurring, which is essential for fixing the problem. Commented Oct 16, 2021 at 4:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.