Declaring linked list inside linked list

I got confused in my program. I want to make a struct class which is linked list and then inside it I want to have another struct called student. Can I declare struct student as a linked list too? How to access it?

Anyway, you can look my code in here

Ignore the menu function. I still can’t use function insertMhs properly. It will crash after I insert 1 struct. And in function insertKelas I can’t make another struct after I make 1 struct. Can anybody help me? Btw is my freeMemory function doing correctly?

what is your problem exactly ? if your question is : can i declare linked lists inside linked lists, the answer is YES, you can do almost whatever you want. MY question is : as the linked list is an ordered structure, do you really need it ? don’t you prefer simple sets ? like set of students, and set of classes ?

1 Like

Yes you can basically you will have a link link and the element inside it will be a struct rather than a simple element.

Eg :

struct stud_data
{
...
};
struct node
{
stud_data student; 
...
node* next;
};

Just take care about two things

  1. The structure is declared before the link list structure,need not be defined, just declared.
  2. As cyberax mentioned do you really need it ? If you could add all your data withing the link list structure itself you would be better off.

if you want to access a nested structure, you need to use scope resolution (remember)
something like
outer_struct::inner_struct obj;
obj.member

hope this helps

Well… Actually I need to make it because I want if my struct classes deleted it will also deleted all of struct students inside it. I can’t do it if I make it as own struct right?

then simply use C++ classes and implement an appropriate destructor, which erases the class content with the students inside at the same time. it’s not a matter of lists or sets or whatever. for the C struct thing, the first answer of abhimanyuma should be correct for you.

I already post the link in my 1st post. can you help me fix it? Btw look #9 post

  • writing code in indonesian is not a very good idea, if read by english-speaking developers :slight_smile:
  • in your “int insertMhs()” function, why is the “return 0;” statement inside your while loop ?