資料結構 鍊錶及鍊錶功能實現

2021-10-16 09:45:34 字數 3990 閱讀 1967

typedef

struct node

listnode,

*linklist;

舉例

1.設p指向鍊錶中結點ai

2.可呼叫c語言中malloc()函式向系統申請結點的儲存空間

linklist  p; 

p =(linklist)

malloc

(sizeof

(listnode));

//強制型別轉換

則建立乙個型別為linklist的結點,該結點的位址已存入指標變數p中,且儲存空間由轉到

鍊錶建立

實現建立鍊錶**

linklist list_create()

h->data =0;

h->next =

null

;return h;

}// h--head

鍊錶遍歷

鍊錶遍歷實現**

int

list_show

(linklist h)

p = h;

while

(p->next !=

null

)puts(""

);return0;

}

獲取鍊錶中資料位置

獲取位置實現**

linklist list_get

(linklist h,

int pos)

if(pos ==-1

)if(pos

) p = h;

i =-1

;while

(i < pos)

i++;}

return p;

}

鍊錶插入

尾部插入

鍊錶尾插實現**

int

list_tail_insert

(linklist h,data_t value)

nodeif(

(p =

(linklist)

malloc

(sizeof

(listnode)))

==null

) p-

>data = value;

p->next =

null

; tail node

q = h;

while

(q->next !=

null

)q->next = p;

return0;

}

按位置插入

鍊錶按位置插入實現**

int

list_insert

(linklist h,data_t value,

int pos)

//1. locate node p(pos-1)

p =list_get

(h,pos-1)

;if(p ==

null

)//2. new node qif(

(q =

(linklist)

malloc

(sizeof

(listnode)))

==null

) q-

>data = value;

q->next =

null

;//3. insert p->q

q->next = p-

>next;

p->next = q;

return0;

}

鍊錶查詢

按序號查詢

鍊錶按位置插入實現**

linklist list_get

(linklist h,

int pos)

if(pos ==-1

)if(pos

) p = h;

i =-1

;while

(i < pos)

i++;}

return p;

}

鍊錶的刪除

按位置刪除

鍊錶按位置刪除實現**

int

list_delete

(linklist h,

int pos)

p =list_get

(h,pos-1)

;if(p ==

null

)return-1

;if(p->next ==

null)

q = p-

>next;

p->next = q-

>next;

printf

("free:%d\n"

,q->data)

;free

(q);

q =null

;return0;

}

鍊錶的釋放

鍊錶釋放實現**

linklist list_free

(linklist h)

puts(""

);return

null

;}

單鏈表的反轉

單鏈表反轉實現**

int

list_reverse

(linklist h)

if(h-

>next ==

null

||h-

>next-

>next ==

null)

p = h-

>next-

>next;

h->next-

>next =

null

;while

(p !=

null

)return0;

}

鍊錶中求相鄰兩個結點最大值

求相鄰兩個結點最大值實現**

linklist list_adjmax

(linklist h,data_t *value)

if(h-

>next ==

null

|| h-

>next-

>next ==

null

|| h-

>next-

>next-

>next ==

null

) q = h-

>next;

p = h-

>next-

>next;

r = q;

sum = q-

>data + p-

>data;

while

(p->next ==

null)}

*value = sum;

return r;

}

有序鍊錶合併

有序鍊錶合併實現**

int

list_merge

(linklist h1,linklist h2)

p = h1-

>next;

q = h2-

>next;

r = h1;

h1->next =

null

; h2-

>next =

null

;while

(q && p)

else}if

(p ==

null

)else

return0;

}

資料結構鍊錶實現

二 實驗基本原理與設計 三 主要儀器裝置及耗材 四 附錄 利用linux gnu make c 專案管理軟體工具實現資料結構鍊錶 linked list 要求實現以下功能 push,pop,insert,delete,search,visit go through,clear。節點的資料必須具有一般...

資料結構 線性表功能實現

seqlist.h include typedef int datatype typedef struct seqlist seqlist void seqlistinit seqlist sl 初始化資料表 void seqlistdestory seqlist sl 銷毀順序表 void seq...

通訊錄(用鍊錶功能實現)

include include include typedef struct peoplepeople void create people l 尾插法建立鍊錶 people p,r int i r l for i 0 i 5 i p people malloc sizeof people prin...