//To convert postfix expression into an expression tree
 
#include"stdio.h"
#include"conio.h"
#include"process.h"
#include"string.h"
#include"stdlib.h"
 
struct node{
char item;
struct node *lnode;
struct node *rnode;
}*stc[30],*temp,*root;

typedef struct node *NODEPTR;
char expression[50];
char c;
int top=0;
NODEPTR pop();
void push(NODEPTR);
void getinput();
void inorder(NODEPTR p);
     
int main (){
    getinput();
     int i,j;
     for(i=0;i<strlen(expression);i++) 
     {
                                        printf("\nstrlength=%d--------for i=%d\n",strlen(expression),i);
                                        c=expression[i];
                                        switch(c)
                                        {
                                                 case '+':
                                                 case '-':
                                                 case '*':
                                                 case '/':
                                                 case '^':
                                                   {   temp=(NODEPTR)malloc(sizeof(struct node));
                                                      temp->item=c;
                                                      temp->rnode=pop();
                                                      temp->lnode=pop();
                                                      push(root);
                                                   }
                                                      default :
                                                    {  temp=(NODEPTR)malloc(sizeof(struct node));
                                                      temp->item=c;
                                                      temp->lnode=NULL;
                                                      temp->rnode=NULL;
                                                      push(temp);
                                                     } void free(void *ptr);
                                                      break;
                                         }
     }
     root=pop();
     inorder(root);  
     getch();           
                                         } 
                                                                     
NODEPTR pop(NODEPTR p) {
      if(top==0)
      printf("Underflow condition !!");
      else {
          // printf("\npopped  \n");
           return(stc[top--]);
           }
           } 
void push(NODEPTR p) {
     if(top==29)
     printf("Overflow condition !!");
     else {
          //printf("pushed ");
          stc[++top]=p;
          }
          }

void getinput() {
     printf("Enter a valid postfix string : ");
     gets(expression);
     }

    
void inorder(NODEPTR p)
	{if(p!=NULL)
		{
		inorder(p->lnode);
		printf("Data :%c",p->item);
		inorder(p->rnode);
		}
	else
	{printf("\nNull Tree");
		return;}
  }

Can anyone explain what mistake i made in the program ??.....pls its urgent and if possible please provide solutions..

Edited 5 Years Ago by sneha_: n/a

In line# 41
push(root);

Are you trying to push root. You should have done:
push(temp);

In line#50
void free(void *ptr);

What you are trying to do here. Are you trying to free the memory or you want to redefine the function free() ?

Better read about free().


Also, review your push() and pop() operation, test them independantly before using them with your logic.

int main (){
    getinput();
     int i,j;
     for(i=0;i<strlen(expression);i++) 
     {
                                        printf("\nstrlength=%d--------for i=%d\n",strlen(expression),i);
                                        c=expression[i];
                                        switch(c)
                                        {
                                                 case '+':
                                                 case '-':
                                                 case '*':
                                                 case '/':
                                                 case '^':
                                                   {   temp=(NODEPTR)malloc(sizeof(struct node));
                                                      temp->item=c;
                                                      temp->rnode=pop();
                                                      temp->lnode=pop();
                                                      push(root);
                                                   }
                                                      default :
                                                    {  temp=(NODEPTR)malloc(sizeof(struct node));
                                                      temp->item=c;
                                                      temp->lnode=NULL;
                                                      temp->rnode=NULL;
                                                      push(temp);
                                                     } void free(void *ptr);
                                                      break;
                                         }
     }
     root=pop();
     inorder(root);  
     getch();           
                                         } 

Could you please explain why you chose the above formatting? It makes little sense to me so I am quite curious.

root posseses some junk value..

Just a correction, but since root is a global variable, it's properly initialized to 0 (ie. a null pointer). It doesn't contain a junk value, though line 41 is indeed erroneous; it should be pushing temp rather than root.

This article has been dead for over six months. Start a new discussion instead.