I have been working on this for some time. I almost have the insert from standard input(keyboard) working, but I keep on getting a "segmentation fault - core dumped" after the third time through the control loop in the main program. It seems to be inserting the first two nodes correctly though. I am running out of time because my project is due in a week!! Please help if you can. Here is what I have for source code right now:/************************************************** ***
*This is a sample tree program for our final project.
*We will try to modify it to meet our specs
*Kirsten ************************************************** ****
*/
#include "defs.h"
int main()
{
Tree t = NULL;
//these are the variables being read in to the patient record(s)
int i;
char fname[10];
char lname[10];
int DOB;
int SSN;
char doc[10];
char ins[10];
char allergy[15];
char history[15];
char meds[15];
//these are the values needed for the search to work
Position search_val;
int key_SSN;
(void) printf("Welcome to the sample tree program!!\n");
(void) printf("Please enter values for the patient records.\n");
(void) printf("Enter none when finished.\n");
for (i = 0; i < 2; i++)
{
(void) printf("the fname to enter is: \n");
(void) scanf("%s", &fname); //enter the values
(void) printf("the lname to enter is: \n");
(void) scanf("%s", &lname);
(void) printf("the DOB to enter is: \n");
(void) scanf("%d", &DOB);
(void) printf("the SSN to enter is: \n");
(void) scanf("%d", &SSN);
(void) printf("the doctor name to enter is: \n");
(void) scanf("%s", &doc);
(void) printf("the insurance provider is: \n");
(void) scanf("%s", &ins);
(void) printf("the allergy to enter is: \n");
(void) scanf("%s", &allergy);
(void) printf("the patient condition to enter is: \n");
(void) scanf("%s", &history);
(void) printf("the patient's current medication is: \n");
(void) scanf("%s", &meds);
t = insert(t, fname, lname, DOB, SSN, doc, ins, allergy, history, meds);
printf("insert successful\n");
printf("t->fname = %s\n", t->fname);
}
//(void) printf("Please enter the patient social security
// # to search: ");
//(void) scanf("%d", &key_SSN); //enter a number to be searched
//search_val = search(key_SSN, t);
//if (search_val == NULL) //if its not found, 0 is returned
// printf("This value was not on the tree.\n");
//else
//{ //if it is found, print it
// printf("patient record was found.\n");
//printf("first name: %s\n", search_val->fname);
//printf("last name: %s\n", search_val->lname);
//printf("date of birth: %d\n", search_val->DOB);
//printf("social security #: %d\n", search_val->SSN);
//printf("primary physician: %s\n", search_val->doc);
//printf("insurance provider: %s\n", search_val->ins);
//printf("patient allergies: %s\n", search_val->allergy);
//printf("patient conditions: %s\n", search_val->history_cond);
//printf("patient medication(s): %s\n", search_val->meds);
//}
(void) printf("The tree values are: \n");
inorder(t); //traverse the tree inorder and print values
printf("****");
return 0;
}
Functions called form main:
#include "defs.h"
//TreeNode* create(int value)
//{
//TreeNode *tptr = malloc(sizeof(TreeNode));
//if (tptr != NULL) //create a new node and initialize
//{ //its values
//tptr->data = value;
//tptr->left = NULL;
//tptr->right = NULL;
//}
//return tptr;
//}
void destroy(TreeNode* tptr)
{
free (tptr); //not sure about this yet
}
Tree insert(Tree root, string t_fname, string t_lname, int t_DOB,
int t_SSN, string t_doc, string t_ins, string t_allergy,
string t_history, string t_meds)
{
TreeNode* temp;
if (root == NULL)
{
temp = malloc(sizeof(TreeNode));
temp->fname = t_fname;
temp->lname = t_lname;
temp->DOB = t_DOB;
temp->SSN = t_SSN;
temp->doc = t_doc;
temp->ins = t_ins;
temp->allergy = t_allergy;
temp->history_cond = t_history;
temp->meds = t_meds;
temp->left = NULL;
temp->right = NULL;
}
else
{
if (t_SSN < root->SSN)
{
printf("insert root->left\n");
root->left = insert(root->left, t_fname, t_lname, t_DOB, t_SSN,
t_doc, t_ins, t_allergy, t_history, t_meds);
}
else if (t_SSN > root->SSN)
{
printf("insert root->right\n");
root->right = insert(root->right, t_fname, t_lname, t_DOB, t_SSN,
t_doc, t_ins, t_allergy, t_history, t_meds);
}
}
return temp;
}
Position search(int key_SSN, Tree root)
{
if (root != NULL)
{
if (key_SSN == root->SSN)
return root;
else
if (key_SSN < root->SSN)
return search(key_SSN, root->left);
else
if (key_SSN > root->SSN)
return search(key_SSN, root->right);
}
}
void inorder(Tree t)
{
printf("@@@@@");
if (t != NULL)
{
inorder(t->left);
printf("patient first name:%s\n", t->lname);
printf("patient SSN: %d\n", t->SSN);
inorder(t->right);
}
}
definition file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
/*notes on definitins in the structure
fname = first name
lname = last name
DOB = date of birth
SSN = socsecnum
doc = primary physician
ins = insurance
*/
typedef char* string;
typedef struct TreeNode
{
string fname;
string lname;
int DOB;
int SSN;
string doc;
string ins;
string allergy;
string history_cond;
string meds;
struct TreeNode* left;
struct TreeNode* right;
}TreeNode;
typedef TreeNode* Tree;
typedef TreeNode* Position;
//function prototypes
//TreeNode* create(int);
void destroy(TreeNode*);
Tree insert(TreeNode*, char[], char[], int, int, char[], char[], char[],
char[], char[]);
Position search(int, TreeNode*);
void inorder(Tree);