[英]Linked List of Linked List Implementation
我最近查看了可保存大量數據的鏈表。 但是,我堅持提出一種很好的方法來為鏈接列表的鏈接列表保存數據。
以下是我嘗試完成的偽代碼
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;
};
我想知道這是否可以在鏈接列表中實現鏈接列表。 因此,當我通讀XML文件時。 我可以在一個州下分配多個學校,並在一個學校中分配多個教室,依此類推。
實施總是取決於你試圖解決什么問題。 在不了解問題本質的情況下,很難說您的實現是否最佳。 這絕對是合法的(盡管我會使用實際列表作為成員而不是指向列表的指針-后者似乎沒有令人信服的理由)。 您的數據一次寫入多次嗎? 您是否希望按順序閱讀所有學生(例如)? 您是否打算經常增加新學生? 根據這些答案,可以選擇程序中的最佳表示形式。
仍然可以使用上面的偽代碼。
只是一個小例子:
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.
我希望你需要。 如果我對問題的理解不正確,對不起。 正如@Alexander L. Belikoff所說,這取決於您要解決的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.