鍊錶基礎(增刪查改)

2021-10-07 21:14:01 字數 2930 閱讀 7069

靜態鍊錶的應用範圍只限於本函式,有侷限性,無法跨函式呼叫,所以在子函式中要malloc分配記憶體。

鍊錶的資料型別是結構體。

#define _crt_secure_no_warnings

#include

"stdio.h"

#include

"stdlib.h"

#include

"string.h"

typedef

struct node

slist;

slist *

creat_slist()

;//增

intslist_nodedel

(slist *phead,

int y)

;//刪

intslist_nodeinsrt

(slist *phead,

int x,

int y)

;//查,改

intslist_reverse

(slist *phead)

;//逆置

intslist_print

(slist *phead)

;//顯示

intslist_destory

(slist *phead)

;//free節點

//在結點數為x的前面插入y

intslist_nodeinsrt

(slist *phead,

int x,

int y)

ppre = phead;

pcur = phead->next;

//不斷的malloc新結點,並且資料域賦值

pm =

(slist *

)malloc

(sizeof

(slist));

pm->data = y;

pm->next =

null

;while

(pcur)

//讓ppre下移

ppre = pcur;

//讓當前節點下移

pcur = pcur->next;

}//讓新節點連線後繼節點

pm->next = ppre->next;

//讓前驅節點連線新節點

ppre->next = pm;

return0;

}//刪除結點為y的鍊錶結點

intslist_nodedel

(slist *phead,

int y)

ppre = phead;

pcur = phead->next;

while

(pcur)

ppre = pcur;

pcur = pcur->next;}if

(pcur ==

null

)//讓新節點連線後繼節點

ppre->next = pcur->next;

//讓前驅節點連線新節點

free

(pcur)

;return0;

}int

slist_destory

(slist *phead)

slist *p = phead,

*tmp =

null

;while

(p)return0;

}slist *

creat_slist()

phead->data =0;

phead->next =

null

;//2、迴圈建立結點,結點資料域的數值從鍵盤輸入。

//以-1作為輸入結束標誌。

printf

("\nplease input the date of node:\n(noting:-1:quit)");

scanf

("%d"

,&data)

;//初始化,使輔助指標指向phead

pcur = phead;

while

(data !=-1

) pm->data = data;

pm->next =

null

;//新結點入鍊錶

pcur->next = pm;

//當前結點下移(新節點變成當前結點)

pcur = pm;

//or pcur=pcur->next;

printf

("\nplease input the date of node:\n(noting:-1:quit)");

scanf

("%d"

,&data);}

//printf("\nphead :%x\n", phead);

return phead;

}int

slist_print

(slist *phead)

//準備環境

p = phead->next;

printf

("\n開始");

while

(p)printf

("結束\n");

return0;

}int

slist_reverse

(slist *phead)

//逆置

if(phead->next ==

null

|| phead->next->next ==

null

) p = phead->next;

q = phead->next->next;

while

(q !=

null

) phead->next->next =

null

; phead->next = p;

return0;

}void

main()

鍊錶 增 刪 查 改 排序

define crt secure no warnings include include include typedef struct num ss ss head null int count 0 void add node void remove node int id void seek n...

鍊錶的增刪查改

include include typedef struct student stu,pstu void list head insert pstu pphead,stu pptail,int i 鍊錶為空,頭尾指標都指向新節點 else 新節點 pnext指向原本頭節點,新節點作為頭節點 void...

陣列和鍊錶的增刪查改

查詢 陣列中的資料都是連續的在查詢陣列中的元素時候,只要進行遍歷,或者跟據下標尋找。增刪在陣列中如果對某乙個資料進行插入,那麼後面的每乙個資料都要進行右移。如果進行刪除那麼它後面的每乙個資料都要進行左移。查詢 在進行查詢時,如果要遍歷所有的元素那麼鍊錶的速度很快,但是如果需要某乙個資料,那麼每次都要...