Reputation: 65
Currently trying to figure out what to write in the main.c file. The program given to me have a BST to store long data, while there is a linked list that stores strings. The data structure so far is a binary search tree with nodes, and each of these nodes contains an empty linked list.
My goal here is to find a node within the BST by user input, then add strings to the linked list. Here's my code so far, I'm currently stuck as I'm not sure how I'm gonna implement this in the main.c function.
//list.h file
typedef struct bstNode { //pointer to the bst
long data; //storing long data types from -2,147,483,648 to 2,147,483,647
List courses; //this is the list of courses
struct bstNode *left; //left child of the tree
struct bstNode *right; //right child of the tree
} *BSTNodePtr;
typedef struct bst {
BSTNodePtr root; //points to the root of the tree
} BST;
BST new_bst();
BSTNodePtr find_bst_node(BSTNodePtr self, long n); //finds a student
void find_bst(BST *self, long n); //wrapper for finding a student
//bst.c file
BSTNodePtr find_bst_node(BSTNodePtr self, long n) {
if (self == NULL || (n == self->data)) {
return self;
}
else if (n < self->data) {
return find_bst_node(self->left, n);
}
else {
return find_bst_node(self->right, n);
}
}
void find_bst(BST *self, long n) { //wrapper function for find_bst_node
return find_bst_node(self->root, n);
}
// list.h file
typedef struct listNode {
char* data;
struct listnode *next;
} *ListNodePtr;
typedef struct list {
ListNodePtr head;
} List;
List new_list(); //creates a new list
void insert_at_front(List *self, char* data);
//main.c file
void option_enrol_student(BST *self) { //finds the student, and then adds the courses into the empty list.
long input = 0;
char course_input;
long result; //var result to return BSTNodePtr
printf("Enter the ID of the student you want to enrol\n");
scanf("%ld", &input);
result = find_bst_node(self, input); //looks for students in the BST this returns the result
printf("What course would you like to enrol this student into?");
scanf("%ld", &course_input);
//access the result and adds user input into linked list.
insert_at_front(self, course_input);insert_at_front
}
find_bst_node returns a node, and I'm not sure how to get that return and add user input into the linked list for that node.
Upvotes: 1
Views: 980
Reputation: 44274
Here are some problems but I'm not sure it covers all.
char course_input;
....
scanf("%ld", &course_input);
You make course_input
a char and then scan using %ld
. That is not correct for a char. Further, I doubt you want a char. I guess you want a string. Like:
char course_input[64] = {0};
....
scanf("%63s", course_input);
Another issue is the type of result
long result; //var result to return BSTNodePtr
As you write in the comment it shall be BSTNodePtr
so why make it a long? Further you don't use result
at all. Maybe you wanted:
BSTNodePtr result;
....
result = find_bst(self, input);
....
insert_at_front(result, course_input);
Upvotes: 1