Hi guys...cant work out to search for a customer name in my binary tree....any help wold be appreciated....keep getting lost with the tutorials
Code:
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
struct custdata
{
char cname[20];
char custemail[30];
int numorders;
};
struct node
{
struct node *left;
struct node *right;
struct custdata *cdata;
};
struct node * init(struct node *);
struct custdata * initcustdata(struct custdata *);
int menu(int);
struct custdata * getdetails(struct custdata *);
struct node * add(struct node *,struct custdata *);
struct node * display(struct node *);
int flag = 0;
int main()
{
int choice;
struct node * tree;
struct custdata * cdata;
tree = init (tree);
cdata = initcustdata (cdata);
do
{
choice = menu(choice);
switch(choice)
{
case 1 :
cdata = getdetails(cdata);
tree = add(tree,cdata);
break;
case 2 :
display(tree);
break;
case 3 :
printf("EXITING ! \n");
break;
case 4 :
break;
}
}while(choice != 4);
}
struct node * init (struct node *tree)
{
printf("in init\n");
tree = NULL;
return tree;
}
struct custdata * initcustdata (struct custdata *cdata)
{
cdata = NULL;
return cdata;
}
int menu (int choice)
{
printf("PRESS 1 to ADD to BINARY TREE\n");
printf("PRESS 2 to DISPLAY SORTED BINARY TREE\n");
printf("PRESS 3 to EXIT the BINARY TREE\n");
scanf("%d",&choice);
return choice;
}
struct custdata * getdetails(struct custdata *cdata)
{
cdata = malloc(30);
printf("Enter Customer Name to add to record \n");
fflush ( stdin );
gets(cdata->cname);
fflush ( stdin );
printf("Enter Customer email to add to record \n");
scanf("%s",cdata->custemail);
fflush ( stdin );
printf("Enter quantity required to add to record \n");
scanf("%d",&cdata->numorders);
fflush ( stdin );
return cdata;
}
struct node * add (struct node *tree,struct custdata *cdata)
{
if(tree == NULL)
{
tree = malloc(30);
tree->left = NULL;
tree->right= NULL;
tree->cdata = cdata;
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)<0)
{
tree->left= add(tree->left,cdata);
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)==1)
{
tree->right = add(tree->right,cdata);
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)==0)
{
printf("Name matches incrementing orders field \n");
tree->cdata->numorders++;
return tree;
}
}
struct node * display (struct node *tree)
{
if(tree!=NULL)
{
display(tree->left);
printf("Customer Name is %s \n",tree->cdata->cname);
printf("Customer Email is %s \n",tree->cdata->custemail);
printf("Number of Orders is %d \n",tree->cdata->numorders);
display(tree->right);
}
}