Jonno Williams
Jonno Williams

Reputation: 107

Accessing structs in a linked list

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

Upvotes: 0

Views: 387

Answers (1)

Hardik Panchal
Hardik Panchal

Reputation: 343

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.

Upvotes: 1

Related Questions