C語(yǔ)言實(shí)現(xiàn)單鏈表實(shí)現(xiàn)方法
C語(yǔ)言實(shí)現(xiàn)單鏈表實(shí)現(xiàn)方法
鏈表和我們之前實(shí)現(xiàn)過(guò)的順序表一樣,都是簡(jiǎn)單的數(shù)據(jù)結(jié)構(gòu),鏈表分為單向鏈表、雙向鏈表、循環(huán)鏈表。而單向鏈表又分為兩種實(shí)現(xiàn)方法,一種為帶頭節(jié)點(diǎn)的單鏈表,一種為不帶頭節(jié)點(diǎn)的單鏈表。我們來(lái)具體看看不帶頭節(jié)點(diǎn)的單鏈表的實(shí)現(xiàn)
單鏈表:它是一種鏈?zhǔn)酱鎯?chǔ)的線性表,用一組地址任意的存儲(chǔ)單元存放線性表的數(shù)據(jù)元素,稱(chēng)存儲(chǔ)單元為一個(gè)節(jié)點(diǎn)。
今天我們來(lái)實(shí)現(xiàn)一些單鏈表的簡(jiǎn)單接口
先看看單鏈表的結(jié)構(gòu): (為了通用性,我們將類(lèi)型重命名為DataType)
typedef int DataType; //鏈表 typedef struct Node { DataType *data; struct Node *next; }Node, *pNode, *pList;
接下來(lái)看看我們要實(shí)現(xiàn)的接口:
void InitLinkList(pList *pplist);//初始化鏈表 pNode BuyNode(DataType d);//創(chuàng)建鏈表節(jié)點(diǎn) void PushBack(pList *pplist, DataType d);//尾插 void PopBack(pList *pplist);//尾刪 void PushFront(pList *pplist, DataType d);//頭插 void PopFront(pList *pplist);//頭刪 void PrintList(pList plist);//打印鏈表 pNode Find(pList plist, DataType d);//查找指定元素 void Remove(pList *pplist, DataType d);//刪除指定的一個(gè)元素 void RemoveAll(pList *pplist, DataType d);//刪除指定的所有元素 void Insert(pList *pplist, pNode pos, DataType d);//指定位置的后面插入 void Erase(pList *pplist, pNode pos);//指定位置刪除 void DestroyList(pList *pplist);//銷(xiāo)毀鏈表
來(lái)看看每個(gè)接口的具體實(shí)現(xiàn):
pNode BuyNode(DataType d) { pNode newNode = (pNode)malloc(sizeof(Node)); if (newNode == NULL) { perror("malloc"); exit(EXIT_FAILURE); } newNode->data = d; newNode->next = NULL; return newNode; } void InitLinkList(pList *pplist) { assert(pplist); *pplist = NULL; } void PushBack(pList *pplist, DataType d) { assert(pplist); pNode newNode = BuyNode(d); pNode cur = *pplist; //鏈表沒(méi)有節(jié)點(diǎn) if (*pplist == NULL) { *pplist = newNode; return; } //鏈表有節(jié)點(diǎn) while (cur->next != NULL) { cur = cur->next; } cur->next = newNode; } void PopBack(pList *pplist) { pNode cur = *pplist; pNode prev = NULL; assert(pplist); //鏈表沒(méi)有節(jié)點(diǎn) if (*pplist == NULL) { return; } //鏈表有一個(gè)節(jié)點(diǎn) if (cur->next == NULL) { free(*pplist); *pplist = NULL; return; } //鏈表有兩個(gè)及兩個(gè)以上節(jié)點(diǎn) while (cur->next != NULL) { prev = cur;//prev中保存的是cur之前的那個(gè)節(jié)點(diǎn) cur = cur->next; } prev->next = NULL; free(cur); } void PushFront(pList *pplist, DataType d) { pNode newNode = BuyNode(d); //pNode cur = *pplist; assert(pplist); ////鏈表沒(méi)有節(jié)點(diǎn) //if (*pplist == NULL) //{ // *pplist = newNode; //} ////鏈表有節(jié)點(diǎn) newNode->next = *pplist; *pplist = newNode; } void PopFront(pList *pplist) { pNode cur = *pplist; assert(pplist); //鏈表為空 if (*pplist == NULL) { return; } *pplist = cur->next; free(cur); cur = NULL; } void PrintList(pList plist) { pNode cur = plist; while (cur) { printf("%d-->", cur->data); cur = cur->next; } printf("NULL\n"); } pNode Find(pList plist, DataType d) { pNode cur = plist; while (cur) { if (cur->data == d) { return cur; } cur = cur->next; } return NULL; } void Remove(pList *pplist, DataType d) { pNode cur = *pplist; pNode prev = NULL; assert(pplist); if (cur == NULL) { return; } while (cur) { if (cur->data == d) { pNode del = cur; if (cur == *pplist) { *pplist = cur->next; } prev->next = cur->next; free(del); del = NULL; return; } else { prev = cur; cur = cur->next; } } } void RemoveAll(pList *pplist, DataType d) { pNode cur = *pplist; pNode prev = NULL; assert(pplist); if (*pplist == NULL) { return; } while (cur) { if (cur->data == d) { pNode del = cur; if (cur == *pplist) { *pplist = cur->next; } else { prev->next = cur->next; } cur = cur->next; free(del); del = NULL; } else { prev = cur; cur = cur->next; } } } //在pos后面插入一個(gè)元素 void Insert(pList *pplist, pNode pos, DataType d) { pNode newNode = BuyNode(d); assert(pplist); assert(pos); if (*pplist == NULL) { PushFront(pplist, d); return; } newNode->next = pos->next; pos->next = newNode; } void Erase(pList *pplist, pNode pos) { assert(pplist); assert(pos); //要?jiǎng)h除的是尾節(jié)點(diǎn) if (pos->next == NULL) { PopBack(pplist); } //刪除的是非尾節(jié)點(diǎn) else { pNode del = pos->next; pos->data = pos->next->data; pos->next = pos->next->next; free(del); del = NULL; } } void DestroyList(pList *pplist) { assert(pplist); pNode cur = *pplist; while (cur) { pNode del = cur; cur = cur->next; printf("del:%d\n", del->data); free(del); del = NULL; } }
由于這些接口都較為簡(jiǎn)單,所以不進(jìn)行具體的測(cè)試展示,讀者可以自行測(cè)試
以上就是C語(yǔ)言實(shí)現(xiàn)單鏈表實(shí)現(xiàn)方法,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:使用Libmicrohttpd搭建內(nèi)嵌(本地)服務(wù)器的方法
欄 目:C語(yǔ)言
本文標(biāo)題:C語(yǔ)言實(shí)現(xiàn)單鏈表實(shí)現(xiàn)方法
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1264.html
您可能感興趣的文章
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用函數(shù)刪除字符
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)式函數(shù)庫(kù)
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)數(shù)怎么表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段函數(shù)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排序法函數(shù)
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段函數(shù)
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎么打出三角函數(shù)的值
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求階乘


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法