//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..

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.

May i plz get a fresh code?

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.

in line 41 why are you pushing root??root posseses some junk value..

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.