【问题标题】:Data structure for storing books using an array and a doubly linked list使用数组和双向链表存储书籍的数据结构
【发布时间】:2015-04-03 12:59:58
【问题描述】:

我正在尝试使用数组和链表为库程序创建数据结构。数组的索引代表图书ID,数组中的元素代表图书数量。

为了跟踪哪些借款人拥有每本书的副本,我希望每个索引都指向一个链表。

我已经有了双向(循环)链表 [clist] 的库文件。

创建一个新的列表:

clist *xs;
xs = new_clist();

存放书籍的数组是:

books[100]

从图表上看,这是我想要做的:

 i (qty) -> (list of borrowers)

 0  5    -> (1,5,6)
 1  8    -> ()
 2  6    -> (8,5)
 .  .     .
 .  .     .
 .  .     .
 99 7     ->(8,5,6)

我正在努力编写这个数据结构,如果有人能告诉我如何做到这一点,我将不胜感激。提前谢谢!

【问题讨论】:

    标签: c arrays list data-structures


    【解决方案1】:

    您可以简单地将列表的头节点放入书籍结构中:

    typedef struct _book_info
    {
        unsigned int book_id;
        size_t quantity;
        clist * clients;
    }book_info_t;
    
    book_info_t books[100];
    

    【讨论】:

      【解决方案2】:

      试试这个:

      struct book{
          int id;
          int quantity;
          struct client* borrowers;
      };
      

      并根据需要定义您的borrower 结构:

      struct client{
          char* data;
          struct client* next;
          struct client* previous;
      };
      

      然后你可以像这样初始化你的书:

      struct book b = {NULL};
      b.id = 0;
      b.quantity = 0;
      b.borrowers = malloc (n * sizeof(client));
      

      【讨论】:

        猜你喜欢
        • 2014-07-10
        • 1970-01-01
        • 1970-01-01
        • 2019-12-21
        • 1970-01-01
        • 2015-08-09
        • 1970-01-01
        • 2015-12-27
        • 1970-01-01
        相关资源
        最近更新 更多