資料結構與演算法 C語言鍊錶案例

2021-10-14 08:02:38 字數 4187 閱讀 2337

#define _crt_secure_no_warnings

#include

"stdio.h"

#include

"stdlib.h"

#include

"string.h"

//設計鍊錶節點

typedef

struct __linknode

linknode;

//設計鍊錶管理結構體 由於多了m_size 可以更方便管理節點

typedef

struct __linklist

linklist;

//初始化鍊錶

//為 防止隨意操作,將鍊錶結構體指標做"隱身"

typedef

void

* linkptr;

linkptr init_linklist()

//插入資料

void

insert_linklist

(linkptr handle,

void

*data,

int pos)

if(pos<

0||pos>p-

>m_size)

linknode*pcurrent = p-

>pheader;

for(

int i =

0; i < pos ; i++

) linknode*newnode =

malloc

(sizeof

(linknode));

newnode-

>data = data;

newnode-

>next = pcurrent-

>next;

pcurrent-

>next = newnode;

p->m_size++;}

//遍歷鍊錶

void

foreach_linklist

(linkptr handle,

void

(*callback)

(void*)

) linknode*pcurrent = p-

>pheader-

>next;

for(

int i =

0; i < p-

>m_size;i++)}

//根據位置刪除資料

void

del_bypos_linklist

(linkptr handle,

int pos)

linknode*pprev =

null

; linknode*pcur = p-

>pheader;

if(pos>=p-

>m_size||pos<0)

for(

int i =

0; i <= pos; i++

) pprev-

>next = pcur-

>next;

free

(pcur)

; pcur =

null

; p-

>m_size--;}

//根據值刪除節點

void

del_byvalue_linklist

(linkptr handle,

void

*data,

int(

*callback)

(void*,

void*)

) linklist*p = handle;

linknode*pcur = p-

>pheader-

>next;

for(

int i =

0; i < p-

>m_size;i++

) pcur = pcur-

>next;}}

//返回鍊錶長度

intlen_linklist

(linkptr handle)

linklist*p = handle;

return p-

>m_size;

}//清空鍊錶

void

destroy_linklist

(linkptr * handle)

linklist*p =

*handle;

linknode*pcur = p-

>pheader-

>next;

for(

int i =

0; i < p-

>m_size;i++

)free

(p->pheader)

; p-

>pheader =

null

;free

(p);

*handle =

null;}

//------------------------------使用者區------------------------------------//

typedef

struct __hero

hero;

void

print_hero

(void

*data)

intcmp_hero

(void

*data1,

void

*data2)

void

test()

; hero h2 =

; hero h3 =

; hero h4 =

; hero h5 =

; hero h6 =

; hero h[3]

=,,}

;for

(int i =

0; i <

3; i++

)insert_linklist

(handle,

&h3,-1

);foreach_linklist

(handle, print_hero)

;printf

("當前鍊錶長度:%d\n"

,len_linklist

(handle));

printf

("--------------------------\n");

del_bypos_linklist

(handle,1)

;foreach_linklist

(handle, print_hero)

;printf

("當前鍊錶長度:%d\n"

,len_linklist

(handle));

printf

("--------------------------\n");

del_byvalue_linklist

(handle,

&h3, cmp_hero)

;foreach_linklist

(handle, print_hero)

;printf

("當前鍊錶長度:%d\n"

,len_linklist

(handle));

printf

("--------------------------\n");

insert_linklist

(handle,

&h3,-1

);insert_linklist

(handle,

&h1,-1

);insert_linklist

(handle,

&h2,-1

);foreach_linklist

(handle, print_hero)

;printf

("當前鍊錶長度:%d\n"

,len_linklist

(handle));

printf

("--------------------------\n");

destroy_linklist

(&handle)

;printf

("清空鍊錶後handle值為:%d\n"

, handle)

;printf

("--------------------------\n");

}int

main()

資料結構與演算法 c語言 03 鍊錶

1.單鏈表的建立與輸出 請用鍊錶的形式儲存使用者輸入的n個整數。要求使用堆記憶體,注意記憶體的分配和釋放。輸入 第一行整數n,第二行n個整數 輸出 n個整數之和 例如 輸入 53 6 9 10 1 輸出 29 include include typedef struct node node 不能直接...

C語言 資料結構與演算法 靜態鍊錶

靜態鍊錶 用陣列描述的鍊錶 備用鍊錶 未被使用的陣列元素 建立 define size 1000 為了方便插入資料通常會把陣列建立的大些 typedef struct stlist stlist space size 初始化 初始化靜態鍊錶 void inilist space size 1 cur...

資料結構與演算法 C語言 單向鍊錶1

將儲存資料元素資訊的域稱為資料域,將儲存直接後繼位置的域稱為指標域。指標域中儲存的資訊稱做指標或鏈。這兩部分資訊組成資料元素稱為結點。1 頭指標是指向鍊錶第乙個結點的指標。注,並不一定是頭結點的指標。無論鍊錶是否為空,頭指標均不為空。2 頭結點,為了操作的統一和方便而設立的,放在第乙個元素結點之前,...