prefix to infix in C

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



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='';

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.





Don't cast the result of malloc in C
– phuclv
Aug 11 at 8:47


malloc




1 Answer
1



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


char *


char


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=''; /* This line is never reached */



And here:


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



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


temp->data


char *


"%s"



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


gets



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


malloc





Good inputs there! +1. Additionally you can suggest getting rid of the useless casting there in top=(NODE*)malloc(sizeof(NODE)); , to check if top is NULL or not, also to free the memory that is allocated before exiting from main. :+)
– WedaPashi
Aug 11 at 9:19



top=(NODE*)malloc(sizeof(NODE));


top


main






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

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard