雙向鍊錶 基於C語言

2021-10-09 02:55:01 字數 4121 閱讀 9584

最近複習了c語言的相關知識,實現了資料結構裡面的雙向鍊錶。

下面是main.c檔案:

#include

"duallinklist.h"

#include

intmain()

printf

("開始列印鍊錶:\n");

i =0;

for(

duallinklistmove

(list,

duallinklistlength

(list)-1

);!duallinklistend

(list)

;duallinklistpre

(list)

, i++

)duallinklistdelete

(list,0,

null);

duallinklistdelete

(list,0,

null);

printf

("此時雙向鍊錶長度為:%d\n"

,duallinklistlength

(list));

printf

("刪除狀態:%d\n"

,duallinklistdelete

(list,1,

null))

;printf

("此時雙向鍊錶長度為:%d\n"

,duallinklistlength

(list));

list =

duallinklistdestroy

(list);}

return0;

}

下面是duallinklist.**件:

#ifndef __duallinklist_h__

#define __duallinklist_h__

typedef

int datatype;

#define debug() printf("file = %s\nline = %d\n", __file__, __line__)

#include

#include

#ifndef __cplusplus

typedef

int bool;

#define true 1

#define false 0

#else

typedef bool bool;

#endif

#define struct(type) typedef struct _struct##type type; \

struct _struct##type

#define malloc(type, size) (type*)malloc(sizeof(type) * size)

#define free(p) (free(p), p = null)

struct

(dllnode)

;struct

(duallinklist)

;duallinklist*

duallinklistcreate

(void);

duallinklist*

duallinklistdestroy

(duallinklist *list)

;int

duallinklistlength

(duallinklist *list)

;bool duallinklistinsert

(duallinklist *list,

int i, datatype data)

;bool duallinklistdelete

(duallinklist *list,

int i, datatype *data)

;int

duallinklistfind

(duallinklist *list, datatype data)

; bool duallinklistset

(duallinklist *list,

int i, datatype data)

;bool duallinklistget

(duallinklist *list,

int i, datatype *data)

;dllnode*

duallinklistmove

(duallinklist *list,

int i)

;bool duallinklistend

(duallinklist *list)

;void

duallinklistnext

(duallinklist *list)

;void

duallinklistpre

(duallinklist *list)

;datatype duallinklistcurrent

(duallinklist *list)

;#endif

下面是duallinklist.c檔案:

#include

#include

"duallinklist.h"

duallinklist*

duallinklistcreate

(void

)//o(1)

return list;

}duallinklist*

duallinklistdestroy

(duallinklist *list)

//o(n)

free

(list);}

return

null;}

intduallinklistlength

(duallinklist *list)

//o(1)

bool duallinklistinsert

(duallinklist *list,

int i, datatype data)

//o(n)

else

list->len++;}

else

}return ret;

}bool duallinklistdelete

(duallinklist *list,

int i, datatype *data)

//o(n)

else

free

(node);

list->len--;}

return ret;

}bool duallinklistset

(duallinklist *list,

int i, datatype data)

//o(n)

bool duallinklistget

(duallinklist *list,

int i, datatype *data)

//o(n)

//以下五個函式配合使用以遍歷雙向鍊錶,時間複雜度為o(n)

dllnode*

duallinklistmove

(duallinklist *list,

int i)

ret = node;

list->m_current = node;}}

return ret;

}bool duallinklistend

(duallinklist *list)

void

duallinklistnext

(duallinklist *list)

}void

duallinklistpre

(duallinklist *list)

}datatype duallinklistcurrent

(duallinklist *list)

intduallinklistfind

(duallinklist *list, datatype data)

//o(n)}}

return ret;

}

下面是執行結果:

雙向鍊錶C語言

鍊錶結構定義 typedef struct node 建立鍊錶 int creat list struct node h h data 0 0 h llink null h rlink null return 1 清空鍊錶 int free list struct node h return 1 查...

C語言雙向鍊錶

雙向鍊錶基本結構 typedef struct pnode pnode 建立乙個雙向鍊錶,並列印出煉表中儲存的資料 include include 包含malloc函式 void main pnode 第乙個節點head pnode head malloc sizeof pnode head dat...

C語言雙向鍊錶

原文 c語言雙向鍊錶 今天寫了點雙向鍊錶的各種操作,寫插入的時候費了點時間,不過,現在看來還是值得耗費那點時間去寫的,這種小東西應該能信手拈來才行啊。1 雙向鍊錶 2 include 3 include strcmp const char const char return 0 is equal 4...