在c++中实现嵌套链表

implementing nested linked list in c++

本文关键字:嵌套 链表 实现 c++      更新时间:2023-10-16

这种嵌套链表的实现在c++中有效吗?如果是,那么如何声明嵌套链表的头?访问里面那些列表中的数据的语法是什么?这是我的通用代码的一部分。我正在尝试用c++执行一个库管理系统。

     struct course
     { 
         string course_name;
            struct course_books
              {
                 struct wait_list
                  {
                   long stu_num;
                   //date inserted
                   wait_list * next_wait_stu;
                  };
                 struct voters_list
                  {
                   long stu_num;
                   int vote;
                   voters_list * next_voter;
                  };
                  struct deposit_list
                   {
                  long stu_num;
                    //date given
                    //bring back date
                  deposit_list * next_depositor;
                   };
            };
         course * next_course;
          };

             struct demand
              {
                 int ISBN;
                 string book_name,
                           course,
                           author,
                          edition;
                 int demands_num;
                struct demanding_students
                  {
                  string demander_name;
                  int demander_stu_number;
                   //demand date
                  demanding_students * next_demanding_stu;
                      };
              demand * next_demand;
                };
      struct STUDENT_INFO
       {
           struct all_deposited_books
              {
                int ISBN;
                //date given
                //bring back date
                all_deposited_books * next_dep_book;
                  };
             struct today_deposited
               {
                int ISBN;
                 today_deposited * next_today_dep_book;
                };
             };

您可能希望使用类来实现这一点。类似于:

    class STUDENT_INFO{
        protected:
          struct all_deposited_books
          {
            int ISBN;
            //date given
            //bring back date
          };
          struct today_deposited
          {
            int ISBN;
          };
        public:
          all_deposited_books * next_dep_book;
          today_deposited * next_today_dep_book;
    };

然后创建一个类实例,比如:STUDENT_INFO theBestStudentInfo;,如果你想在里面调用一个结构变量,你只需要调用:theBestStudentInfo.all_deposited_books[0].ISBN = 5;或类似的东西。我不认为你可以在结构本身内部创建一个结构实例,比如:

struct all_deposited_books
   {
            int ISBN;
            //date given
            //bring back date
            all_deposited_books * next_dep_book; //<- this is probably gonna cause problems
   };

首先创建结构及其外观,然后可以开始根据需要创建它的实例数量:)