Search code examples
cprefixinfix-notation

prefix to infix in C


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

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


NODE* push(NODE* top, char* item){
    NODE* newNode;
    newNode=(NODE*)malloc(sizeof(NODE));
    newNode->next=NULL;
    newNode->data=item;
    if(top==NULL){
        top=newNode;
    }
    else{
        newNode->next=top;
        top=newNode;
    }
    return top;
}
char pop(NODE* top){
    NODE* temp=top;
    int i, returnValue;
    if(top==NULL){
        return top;
        returnValue='\0';
    }
    else{
        top=top->next;
        returnValue=temp->data;
        free(temp);
    }
    return returnValue;
}

void display(NODE* top){
    NODE* temp=top;
    while(temp->next!=NULL){
        printf("%c",temp->data);
        temp=temp->next;
    }
    printf("\n");
}
int isOperator(char c){
    switch(c)
    {
    case '*':
    case '/':
    case '+':
    case '-':
        return 1;
    }
    return 0;
}

char* prefix_to_infix(char* expression){
    NODE* top;
    top=(NODE*)malloc(sizeof(NODE));
    top->next=NULL;

    int i;
    for(i=strlen(expression)-1;i>=0;i--)
    {
        if(isOperator(expression[i])){
                char a=pop(top);
                char b=pop(top);

                char temp[5]={'(',a,expression[i],b,')'};
                push(top,temp);
        }
        else{
            top=push(top, expression[i]);
        }
    }
    return top->data;
}

int main(){
    NODE* top;
    top=(NODE*)malloc(sizeof(NODE));
    top->next=NULL;
    char expression[30];
    gets(expression);

    puts(prefix_to_infix(expression));
}

Please Help, I've written this C code to print convert a prefix expression to infix expression. But the code would give some run-time error.

https://www.geeksforgeeks.org/prefix-infix-conversion/ Above is the algorithm that I have used, and the only problem is in the implementation of the code

Explanation of the code

I have used Linked list to create my stack. I'm simply Inserting at head to push and deleting at head to pop. *In order to convert from prefix to infix, firstly I am taking in the expression(as character array) as an argument to the function prefix_to_infix().

Now I am checking if the element of expression is an operand or an operator. ->If Operand I am pushing it in stack. ->If Operator I am popping out the first two operands and merging them with the operator in between.


Solution

  • Too many problems, check how to activate warnings in your compiler.

    NODE* push(NODE* top, char* item){
    

    The second parameter is expecting a char *, but you are passing a single char in

    top=push(top, expression[i]);
    

    This is also wrong:

    char pop(NODE* top){
        NODE* temp=top;
        int i, returnValue;
        if(top==NULL){
            return top; /* You can not return NULL, pop returns a char */
            returnValue='\0'; /* This line is never reached */
        }
    

    And here:

    printf("%c",temp->data);
    

    temp->data is a char *, use "%s" as format specifier.

    Also, don't use gets, it's no longer part of the standard and it is very dangerous in previous versions, instead, use fgets

    Finally, as pointed out in comments, don't cast the result of malloc