雙向迴圈鍊錶

2021-07-22 17:01:38 字數 3468 閱讀 5753

標頭檔案

//

// c_double_list.h

// cycle_doublelist

//// created by bikang on 16/9/1.

//#ifndef __cycle_doublelist__c_double_list__

#define __cycle_doublelist__c_double_list__

#include

#include

//定義基礎的結構

typedef struct cdlistelmt_cdlistelmt;

//定義列表

typedef struct cdlist_cdlist;

//初始化

void list_init(cdlist *

list,void(*destroy)(void

*data));

//在element元素後插入乙個元素,element不允許為空

int list_ins_next(cdlist *

list, cdlistelmt *element,const void

*data);

//在element元素前面插入乙個元素,element不允許為空

int list_ins_pre(cdlist *

list, cdlistelmt *element,const void

*data);

//刪除乙個元素

int list_rem(cdlist *

list,cdlistelmt *element, void

**data);

//刪除乙個列表,刪除列表裡面儲存的data

void list_destroy(cdlist *

list);

#define cdlist_size(list) ((list)->size)

#define cdlist_head(list) ((list)->head)

#define cdlist_is_head(list,element) ((element) == (list)->head?

1:0)

#define cdlist_data(element)((element)->

data)

#define cdlist_next(element)((element)->next)

#define cdlist_pre(element)((element)->pre)

#endif

/* defined(__cycle_doublelist__c_double_list__) */

實現

//

// c_double_list.c

// cycle_doublelist

//// created by bikang on 16/9/1.

//#include

"c_double_list.h"

#include

#include

<

string

.h>

//初始化

void list_init(cdlist*

list,void(*destroy)(void

*data))

int list_ins_next(cdlist *

list, cdlistelmt *element,const void

*data)

if((new_element = malloc(sizeof(cdlistelmt))) ==

null) return-1;

new_element->next =

null;

new_element->pre =

null;

new_element->

data

= (void

*)data;

if(cdlist_size(list) ==

0)else

list

->size++;

return0;}

//在element元素前面插入乙個元素,element不允許為空

int list_ins_pre(cdlist *

list, cdlistelmt *element,const void

*data)

if((new_element = malloc(sizeof(cdlistelmt))) ==

null) return-1;

new_element->next =

null;

new_element->pre =

null;

new_element->

data

= (void

*)data;

if(cdlist_size(list) ==

0)else

list

->size++;

return0;}

//刪除乙個元素

int list_rem(cdlist *

list,cdlistelmt *element, void

**data)else

element->pre->next = element->next;

element->next->pre = element->pre;

}free(oldelement);

list

->size--;

return0;}

//delete

void list_destroy(cdlist *

list)

}memset(list, 0, sizeof(cdlistelmt));

return;

}

測試**

//

// main.c

// cycle_doublelist

//// created by bikang on 16/9/1.

//#include

#include "c_double_list.h"

typedef

struct info_info;

void print_list_info(cdlist *list);

int main(int argc, const

char * argv)

void print_list_info(cdlist *list)

cdlistelmt *pl;

pl = cdlist_head(list);

int i;

printf("#########start#############\n");

for(i=0;ilist);i++)

printf("#########end#############\n");

}

雙向鍊錶和雙向迴圈鍊錶

和單向鍊錶相比,多了乙個前驅結點。如果他為空,那麼next和prior都指向自己。而對於雙迴圈鍊錶,只需要最後乙個元素的next指向head next,head next的prior指向最後乙個節點即可。新節點s插入鍊錶,s next給p結點,s prior給p prior,然後,p prior n...

迴圈鍊錶,雙向鍊錶

迴圈鍊錶 迴圈鍊錶與順序鍊錶之間的區別 迴圈鍊錶最後乙個資料的next指標域不為空,而是指向頭結點,其他基本操作大體相同,只是在判斷表結束的條件變為判斷節點的引用域是否為頭引用 雙向鍊錶 author neosong date oct 10,2017 4 43 01 pm program of in...

鍊錶 雙向迴圈鍊錶

雙向迴圈鍊錶與單鏈表一樣,都是邏輯連續 物理不連續的儲存方式,但它的效果要遠遠優於單鏈表,其結構如下 雙向迴圈鍊錶首先要有乙個頭節點,頭節點中不存放資料,真正的資料從頭節點的下乙個節點開始存放 然後每乙個節點都有兩個指標,分別指向前乙個節點和後乙個節點 最後頭尾相連,就成了雙向迴圈鍊錶。includ...