0

I am having trouble understanding how a linked list of structs containing a name and BST works. An example of my lack of understanding is how I am trying to write a function to remove a node from the list; I cant figure out how to connect user input to the specific node i want to delete:

char courseName[100];
printf("Enter name of course to remove\n");
scanf("%s", &courseName);
Course *deleteCourse = courseName; //definitely wrong, assuming something here to connect the input to the struct node goes here
delete_from_list(&listcourses, Whatgoeshere); //I already have code for delete function
//set bst of course to null

HEADER FILE FOR COURSE

typedef struct course{ 
char *name; 
BST students; 
} Course;

typedef struct courseNode {
Course data;
struct courseNode *next;
} *CourseList;

HEADER FILE FOR BST

typedef struct bstNode { 
long student_id; 
struct bstNode *left;
struct bstNode *right; 
} *BST;

I have the standard insert and delete functions already set up.

Secondly, how do I go about accessing the BST within the struct course? For example, inserting a student ID into course "maths". I am only used to dealing with ints and chars in the data section of nodes. Please try not to just throw walls of code at me, I'd rather learn and not have to ask again. Thankyou for your time

1 Answer 1

1

1) For deleting specific node based on course name you don't need to create new Course type object (just waste of memory), you can write one more wrapper to your delete node function where it will iterate over each node and comparing user string courseName with currentCourseNode->data.name.

2) you can access student_id as simple as (currentCourse.students)->student_id=xyz

also note while deleting node do not forget to dealloc memory pointed by Course.name, Course.students else it may create memory leak.

Sign up to request clarification or add additional context in comments.

4 Comments

also for your line Course *deleteCourse = courseName it can be written as Course deleteCourse = {courseName,null} but no need as explained
yea My delete function needs work: trying to do strcmp but it wont take strcmp(current->data,data), as the first parameter is of type "Course" and not char. thanks for the input mate, given me something to think about ill have a hard crack at it tonight and see where i get
oooooh current->data.name works out, thats the correct way to do it?
yes. updated in my answer also I think mine was little confusing.

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.