2015-04-03 56 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));