C語言單向鍊錶的建立 插入 刪除 查詢 遍歷

2021-10-07 09:04:59 字數 3840 閱讀 9657

#include

#include

//定義資料型別,假設為int

typedef

int elemtype;

//定義自引用結構體(結點)

struct node

;//typedef為了簡寫struct node

typedef

struct node node;

/*** 鍊錶各種函式的宣告

*///建立鍊錶函式宣告

node *

creatlist()

;//遍歷鍊錶函式宣告

void

displist

(node *head)

;//得到鍊錶總長度函式宣告

intgetlistlength

(const node *head)

;//找到鍊錶中x位置上的元素

node *

findnodeatx

(node *head,

int x)

;//找到鍊錶中x元素

intfindx

(elemtype x, node *head)

;//在第x個位置上插入元素x

node *

insertnode

(node *head,

int x, elemtype element)

;//從鍊錶種刪除指定位置上的元素

node *

deleteelematx

(node *head,

int x)

;/**

* main函式用於測試

*/int

main

(int argc,

char

const

*ar**)

else

elemtype element;

printf

("請輸入要查詢的元素\n");

scanf

("%d"

,&element)

; location =

findx

(element,head);if

(location ==-1

)else

printf

("請輸入要插入的位置\n");

scanf

("%d"

,&location)

;printf

("請輸入要插入的元素\n");

scanf

("%d"

,&element)

;insertnode

(head,location,element)

;printf

("插入後的鍊錶是\n");

displist

(head)

;printf

("請輸入要刪除的結點位置\n");

scanf

("%d"

,&location)

; node *mynode;

mynode =

deleteelematx

(head, location)

;printf

("刪除後的鍊錶是\n");

displist

(head)

;return0;

}/**

* 刪除鍊錶中指定位置上的元素

* @node *head, 要刪除元素的鍊錶

* @int x,要刪除的位置

*/node *

deleteelematx

(node *head,

int x)

//找到需要刪除結點的之前的乙個結點

previous =

findnodeatx

(head,x-1)

;if(previous ==

null

)else

if(previous->next ==

null

)else

return head;

}/**

* 向鍊錶中的第x個位置上插入元素

* @node *head, 要插入元素的鍊錶

* @int x 要插入的位置從1開始。

* @elemtype element 要插入的元素

* @return node* 返回插入元素後鍊錶的頭指標

*/node *

insertnode

(node *head,

int x, elemtype element)

//第二種情況如果找不到該結點,比如鍊錶只有2個結點,插入到第4個就不可以,只能插入到第三個

if(previous ==

null

)else

return head;

}/**

* 求鍊錶的長度

* @const node *head 需要返回長度的煉表頭指標

* @return int 鍊錶的長度

*/int

getlistlength

(const node *head)

return j;

}/**

* 查詢鍊錶**現的第乙個出現的x元素

* @elemtype x 要查詢的元素的元素型別

* @node *head 要查詢的鍊錶

* @return int 返回找到的元素位置,如果沒找到則該返回-1;

*/int

findx

(elemtype x, node *head)

else

break;}

if(p ==

null

)return-1

;return j;

}/**

* 查詢第x個位置上的元素

* @ node *head 要查詢的鍊錶

* @ int x 鍊錶中第x位置上的元素

* @return node* 返回查詢到的結點位址,如果沒有查詢到則返回null

*/node *

findnodeatx

(node *head,

int x)

if(j!=x)

return findnode;

}/**

* 遍歷鍊錶輸出資料

* @node *head 需要遍歷的鍊錶的頭指標

*/void

displist

(node *head)

}/**

* 建立鍊錶

* @return node* 返回鍊錶的頭指標,使用者遍歷鍊錶。

*/node *

creatlist()

else

printf

("請輸入乙個數字\n");

scanf

("%d"

,¤t->data)

; previous = current;

}//返回頭指標用於遍歷鍊錶

return head;

}

請輸入結點個數

3請輸入乙個數字

1請輸入乙個數字

2請輸入乙個數字

3鍊錶中的資料如下:12

3鍊錶的長度是3

請輸入要查詢的結點位置

1所查詢的元素的資料是1

請輸入要查詢的元素

2查詢的元素在鍊錶的第2個位置上

請輸入要插入的位置

4請輸入要插入的元素

4插入後的鍊錶是

鍊錶中的資料如下:12

34請輸入要刪除的結點位置

4刪除後的鍊錶是

鍊錶中的資料如下:12

3

單向鍊錶 建立 插入 刪除 遍歷

include include include using namespace std struct list create 新建鍊錶 struct list insert struct list head,struct list temp 插入 struct list deletes struct...

c語言單向鍊錶建立

typedef struct nodenode,ptrnode 1 結構體的大小 參考鏈結 char 1 int 4 64位系統中,不管什麼型別的指標的大小都是8 cpu一次讀取8位元組,在結構體中會對不滿足8位元組的型別補全為8位元組 乙個實體結構體的大小為補全後所有變數大小的和。sizeof s...

單向鍊錶的建立(C語言)

貌似有段時間沒有做原創文章了,聒噪的很,開始正式學習資料結構啦哈哈,今天先做單向鍊錶的建立,希望和大家一起分享 陣列作為存放同類資料的集合,給我們在程式設計時帶來很多的方便,增加了靈活性。但陣列也同樣存在一些弊病。如陣列的大小在定義時要事先規定,不能在程式中進行調整,這樣一來,在程式設計中針對不同問...