简体   繁体   中英

Linked List of Linked List Implementation

I recently looked at linked list for saving large amount of data. However I am stuck at coming up with a good way to save data for linked list of linked list.

The following is a pseudo code of what I was trying to accomplish

struct Student
{
    string Name;
}

struct Classroom
{
    string Teacher;
    <list> Student *student;
};


struct School
{
    string School_Name;
    <list> Classroom *room;
};

struct State
{
     string State_Name;
     <list> School *school;
};

I was wondering if this how you implement a linked list within a linked list. So when I read through an XML file. I can assign multiple schools under one State, and multiple Classrooms to one School and so on.

The implementation always depends on what problem you are trying to solve. Without knowing more about the nature of the problem it is difficult to say whether your implementation is optimal. It is definitely legit (although, I would use actual lists as members instead of pointers to lists - there seems to be no compelling reason for the latter). Is your data write once read many? Are you expected to read all students (for example) in order? Are you planning to add new students often? Based on these answers, one could chose an optimal representation within a program.

Still, the pseudo-code above would work.

Just a little example:

list<list<int>> MainList; //you create a list that will contain lists
list<int> SecondList; //Let's say that you have an ordinary list
SecondList.push_back(1);
SecondList.push_back(2); //And also let's say that you enter some values in it
//Then you just add that list to the main list
MainList.push_back(SecondList); //Because MainList should contain other lists.

I hope you needed that. If I haven't understand the question correctly I'm so sorry. As @Alexander L. Belikoff said, it depends on the problem that you want to solve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM