C語言 單向鍊錶的增刪改查

2021-10-01 11:36:51 字數 1409 閱讀 5673

什麼是鍊錶?

1.和陣列一樣,鍊錶也是一種線性表 。

2.從記憶體結構來看:鍊錶的記憶體結構是不連續的記憶體空間,是將一組零散的記憶體塊串聯起來,從而進行資料儲存的資料結構。

3.鍊錶中的每乙個記憶體塊被稱為節點node。節點除了儲存資料之外,還要記錄鏈上 下乙個節點的位址,即後繼指標next。

1.查詢單向鍊錶的尾結點

利用單向鍊錶尾結點的特點:其指標域為null。構建的查詢函式**如下:

player *

findlast

(player *head)

2.查詢單向鍊錶的第n個節點

利用計數器去記錄已經遍歷的節點個數,查詢到第n個節點後直接返回。

player *

find

(player *head,

int index)

return pt;

//若已經找到則直接返回,沒有找到則返回0;

}

1.在尾部新增乙個節點
player *

insert

(player *head)

2.在第n個節點之後插入乙個新的節點

讓第n個節點的指標域指向新的節點,新節點的指標域指向第n+1個節點。

player *

insert

(player *head)

else

printf

("將在尾部或其它未知位置插入");

return head;

}

讓第n-1個節點的指標域直接跳過第n個節點指向第n+1個節點,要記得釋放要刪除的節點的記憶體。

player *

delete

(player *head,

int index)

else

printf

("there is no node\n");

return0;

}

先刪除指定的節點然後在原位置上增加新節點

player *

replace

(player *head,

int index)

new=

(player*

)malloc

(sizeof

(player));

scanf

(" ",)

;

old=

findnode

(head, index)

if(old!=

null

)

剛開始接觸鍊錶理解不夠深,文字表述較少,方法較笨,如有表述不當之處請多多指教。

單向鍊錶的增刪改查

define crt secure no warnings include include includetypedef struct data typedef struct node cltype 追加結點 cltype claddend cltype head,data nodedata els...

mysql增刪改查鍊錶 鍊錶的增刪改查

include include 先定義鍊錶裡面的元素。typedef struct nodemynode 定義整個鍊錶。typedef struct linkmylink int isempty to mylink mylink mylink 判斷鍊錶是否為空。int push to mylinki...

C 鍊錶,增刪改查

main.c homework linklist created by jiumiao on 15 7 23.年 include include typedef struct npcnpc typedef struct nodenode node head null 定義乙個頭節點 新增節點 voi...