C語言實現資料機構單鏈表(頭插法)

2021-10-24 16:54:39 字數 2244 閱讀 8813

#include

#include

typedef

struct lnode

linknode;

void

createlist

(linknode *

&l,int a,

int n)

}void

displist

(linknode *l)

else

}printf

(")\n");

} bool listempty

(linknode *l)

else

}int

listlength

(linknode *l)

printf

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

,len);}

intgetelem

(linknode *l,

int a,

int&e)

if(a<=

0|| p ==

null

)else

}int

locateelem

(linknode *l,

int a,

int&e)

if(p ==

null

)else

}bool listinsert

(linknode *l,

int a,

int c)

if(a<=

0||p==

null

)else

displist

(l);

listlength

(l);}

bool listdelete

(linknode *l,

int a)

if(a<=

0|| p==

null

)else

e = q->data;

p->next = q->next;

free

(q);

printf

("被刪除的元素為%d\n"

,e);

}displist

(l);

listlength

(l);

}void

destroylist

(linknode *l)

free

(pre)

;listlength

(l);

printf

("銷毀成功");

}int

main()

createlist

(l,u,m)

;displist

(l);

listempty

(l);

listlength

(l);

int a,e;

printf

("請輸入要搜尋的元素位置:");

scanf

("%d"

,&a)

;getelem

(l,a,e)

;int b,e_1;

printf

("請輸入要定位的元素:");

scanf

("%d"

,&b)

;locateelem

(l,b,e_1)

;int c,d;

printf

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

scanf

("%d"

,&c)

;printf

("請輸入要插入的元素位置:");

scanf

("%d"

,&d)

;listinsert

(l,d,c)

;int i;

printf

("請輸入要刪除的元素位置:");

scanf

("%d"

,&i)

;getchar()

;listdelete

(l,i)

;char c;

printf

("是否要置空該鍊錶(是/c)?\n");

單鏈表(頭插法)c語言實現

鍊錶 結構體變數與結構體變數聯絡在一起 動態建立鍊錶 動態記憶體申請 模組化設計 1.建立鍊錶 建立乙個表頭表示整個鍊錶 2.建立節點 3.插入節點 4.刪除節點 5.列印,遍歷鍊錶 可以用來做測試 include include struct listnode 建立鍊錶 struct listno...

單鏈表頭插法c語言

include include typedef int elemtype typedef struct nodelinknode,linklist p pointer q next character of p,another pointer linklist creat linklist 頭插入法...

單鏈表的頭插法和尾插法c語言實現

單鏈表的頭插法和尾插法c語言實現 include include include define size 100 簡單的定義乙個鍊錶節點的資料單元 typedef struct student t studentlist,pstudentlist 定義乙個全域性的靜態的煉表頭節點指標 static ...