使用数组和双向链表存储书籍的数据结构

时间:2015-04-03 12:59:58

标签: c arrays list data-structures

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

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

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

创建新的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)

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

2 个答案:

答案 0 :(得分:0)

试试这个:

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));

答案 1 :(得分:0)

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

typedef struct _book_info
{
    unsigned int book_id;
    size_t quantity;
    clist * clients;
}book_info_t;

book_info_t books[100];
相关问题