Hi guys totaly new to this so please be gentle.
I cant get the following code to work.....i know its prob sonething stupid...any ideas would be appreciated
Code:
#include<stdio.h>
struct custdata
{
char cname[20];
char custemail[30];
int numorders;
};
struct node
{
struct node *left;
struct node *right;
struct custdata *cdata; //declares a pointer to the custdata struct
};
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;
tree = init (tree);
struct custdata *cdata;
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;
}
}while(choice != 3);
}
struct node * init (struct node *tree)
{
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 BININARY TREE\n");
printf("PRESS 3 to EXIT the BINARY TREE\n");
scanf("%d",&choice);
return choice;
}
struct custdata * getdetails(struct custdata *cdata)
{
cdata = new custdata; //allocates memory for cdata
printf("Enter Customer Name to add to record \n");
scanf("%s",cdata->cname);
printf("Enter Customer Email to add to record \n");
scanf("%s",cdata->custemail);
printf("Enter quantity required to add to record \n");
scanf("%d",&cdata->numorders);
return cdata;
}
struct node * add (struct node *tree,struct custdata *cdata)
{
if(tree == NULL) //No data
{
tree = new node; //create new node
tree->left = NULL;
tree->right= NULL;
tree->cdata = cdata;//assign struct customer pointer variable the cdata value which contains the entered user customer data
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)<0) //tests to see if entered details name is < tree->cdata->name alphabetically
{
tree->left= add(tree->left,cdata);
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)>0) //tests to see if entered details name is >tree->cdata->name alphabetically
{
tree->right = add(tree->right,cdata);
return tree;
}
if(strcmp(cdata->cname,tree->cdata->cname)==0) //tests to see if entered details name matches tree->cdata->name alphabetically
{
printf("Name matches incrementing orders field \n");
tree->cdata->numorders = tree->cdata->numorders++;
return tree;
}
}
struct node * display (struct node *tree)
{
if(tree!=NULL)
{
display(tree->left); //recurses and passes in left subtree which is all the smallest data alphabetically first
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); //recurses and passes in left subtree which is all the largest data alphabetically from smallest right side data to largest right side data
}
}