2015-04-03 25 views
0

我正在尝试为使用数组和链接列表的库程序创建数据结构。数组的索引代表书籍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) 

我我正在努力编写这个数据结构,如果有人会告诉我这是怎么回事,我将非常感激即先谢谢你!

回答

0

您可以简单地把您的列表中的头节点变为书籍结构:

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

book_info_t books[100]; 
+0

非常感谢你 – user4746271 2015-04-03 13:31:00

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