1

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.

5
  • 2
    This smells a lot like you should have been attending your lectures. Commented Apr 24, 2018 at 4:44
  • I should clarify that only the structs in the h file were given in the assignment. Commented Apr 24, 2018 at 4:51
  • OT: printf("Enter the name of the student. Seems the inpu is number - not name Commented Apr 24, 2018 at 4:53
  • Hiding pointers behind typedefs is evil. Commented Apr 24, 2018 at 5:10
  • @JLWK Please be careful about editing stuff in the question so that already given answers looks strange. Changing insert_in_order to insert_at_front made my answer seem strange (but I have changed it also in the answer) Commented Apr 24, 2018 at 5:19

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

7 Comments

You might want to hint that course_input should be duplicated in insert_in_order... unless you want the OP to find out the hard way. Testing the return value of scanf() is advisable too.
@chqrlie True but I wasn't sure whether insert_in_order is a function that is provided for OP or a function OP is going to write.
@chqrlie. My bad, I found out I removed that function in the .c file and forgot to update the main.c file. Edited it here to make it look clearer, but forgotten 4386427 answered it
@JLWK After looking a bit more at the code I think result = find_bst_node(self, input); shall be result = find_bst(self, input);
@JLWK oh... that's another mistake. find_bst shall not be void! It shall be BSTNodePtr find_bst(... Notice that the code has a return ... so it can't be void.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.