C語(yǔ)言中單鏈表(不帶頭結(jié)點(diǎn))基本操作的實(shí)現(xiàn)詳解
通過(guò)對(duì)順序表的學(xué)習(xí),我們可以發(fā)現(xiàn)順序表有以下幾點(diǎn)缺陷:
1.空間不夠時(shí)需要擴(kuò)容,擴(kuò)容尤其是用realloc進(jìn)行異地?cái)U(kuò)容時(shí),是有一定代價(jià)的,其次還可能存在一定空間浪費(fèi)。
2.頭部或者中間插入刪除,需要挪動(dòng)數(shù)據(jù),效率低下。
那我們可以對(duì)其所具有的缺陷進(jìn)行優(yōu)化:可以按需申請(qǐng)空間同時(shí)插入刪除時(shí)不挪動(dòng)數(shù)據(jù)。而單鏈表就符合這些優(yōu)化所具有特點(diǎn)。但是我們要注意的是單鏈表具有這些優(yōu)點(diǎn)并不代表鏈表就可以完全替代順序表。
一、單鏈表的概念
鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過(guò)鏈表中的指針鏈接次序?qū)崿F(xiàn)的。

從上圖可以看出,鏈?zhǔn)浇Y(jié)構(gòu)在邏輯上是連續(xù)的,但是在物理上不一定連續(xù),物理結(jié)構(gòu)中指針域存儲(chǔ)的是下一個(gè)結(jié)點(diǎn)的地址;而且鏈?zhǔn)浇Y(jié)構(gòu)的結(jié)點(diǎn)一般都是從堆上申請(qǐng)出來(lái)的;從堆上申請(qǐng)的空間,是按照一定的策略來(lái)分配的,兩次申請(qǐng)的空間可能連續(xù),也可能不連續(xù)。鏈表存儲(chǔ)數(shù)據(jù)的區(qū)域可分為數(shù)據(jù)域和指針域:

用代碼表示鏈表中的數(shù)據(jù)域和指針域如下:
typedef int STLDataType;
typedef struct SListNode
{
SLTDataType data;
struct SListNode* next;
}STLNode;
思考:為什么在申請(qǐng)的時(shí)候需要申請(qǐng)堆上的空間?因?yàn)槎焉仙暾?qǐng)的空間存儲(chǔ)數(shù)據(jù)的時(shí)候不會(huì)因?yàn)楹瘮?shù)銷(xiāo)毀而銷(xiāo)毀,堆上的空間需要的時(shí)候就申請(qǐng),不需要的時(shí)候就釋放;函數(shù)棧幀上開(kāi)辟的空間存儲(chǔ)的數(shù)據(jù)會(huì)因?yàn)楹瘮?shù)棧幀的銷(xiāo)毀而釋放,后面在進(jìn)行尾插和頭插時(shí)候新的結(jié)點(diǎn)不能鏈接到表上。
二、單鏈表的基本操作
1.創(chuàng)建單個(gè)結(jié)點(diǎn)
SLTNode* BuySLTNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
perror("BuySLTNode malloc");
exit(-1);
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}
2.創(chuàng)建具有n個(gè)結(jié)點(diǎn)的鏈表
SLTNode* CreateSList(int n)
{
int i = 0;
SLTNode* ptail = NULL,*phead = NULL;
for (i = 0; i < n; i++)
{
SLTNode* newnode = BuySLTNode(i);
if (phead == NULL)
{
ptail = phead = newnode;
}
else
{
ptail->next = newnode;
ptail = newnode;
}
}
return phead;
}
//void CreateSList(SLTNode** pphead, int n)
//{
// int i = 0;
// SLTNode* ptail = NULL;
// for (i = 0; i < n; i++)
// {
// SLTNode* newnode = BuySLTNode(i);
// if (*pphead == NULL)
// {
// ptail = *pphead = newnode;
// }
// else
// {
// ptail->next= newnode;
// ptail = newnode;
// }
// }
//}上述代碼中提供了兩種實(shí)現(xiàn)方式,沒(méi)有注釋的是返回頭指針的,注釋內(nèi)容是沒(méi)有返回值,形參使用的是二級(jí)指針改變頭指針指向的地址。
3.打印單鏈表
void SLTPrint(SLTNode* phead)
{
SLTNode* tail = phead;
while (tail)
{
printf("%d ", tail->val);
tail = tail->next;
}
printf("\n");
}
4.尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x)//尾插
{
SLTNode* tail = *pphead;
SLTNode* newnode = BuySLTNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
}尾插時(shí),要注意當(dāng)單鏈表的頭結(jié)點(diǎn)為空的時(shí)候,要先將新結(jié)點(diǎn)作為頭結(jié)點(diǎn)。因?yàn)楫?dāng)頭結(jié)點(diǎn)為空時(shí),需要改變頭指針,所以傳過(guò)來(lái)的為二級(jí)指針(也可以使用一級(jí)指針,不過(guò)此時(shí)要有返回值),要想改變SLTNode*的值,就要傳遞它的地址即SLTNode**類(lèi)型。
5.尾刪
void SLTPopBack(SLTNode** pphead)//尾刪
{
assert(*pphead);
if ((*pphead)->next==NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLTNode* tail = *pphead;
SLTNode* prev = NULL;
while (tail->next)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}尾刪時(shí)也要注意區(qū)分是否為頭結(jié)點(diǎn);同時(shí)注意刪除后,要將其置空prev->next = NULL。
6.頭插
void SLTPushFront(SLTNode** pphead, SLTDataType x)//頭插
{
SLTNode* newnode = BuySLTNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
7.頭刪
void SLTPopFront(SLTNode** pphead)//頭刪
{
assert(*pphead);
SLTNode* nextnode = (*pphead)->next;
free(*pphead);
*pphead = nextnode;
}
頭插和頭刪是單鏈表的優(yōu)勢(shì),尾插和尾刪是順序表的優(yōu)勢(shì)。同時(shí)在尾刪時(shí)注意將*pphead加上括號(hào)用來(lái)區(qū)分優(yōu)先級(jí),否則會(huì)報(bào)錯(cuò)。
8.查找某個(gè)結(jié)點(diǎn)
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)//查找某個(gè)數(shù)并返回所在位置
{
SLTNode* tail = phead;
while (tail)
{
if (tail->val == x)
{
return tail;
}
tail = tail->next;
}
return NULL;
}
注意循環(huán)條件,當(dāng)循環(huán)條件寫(xiě)為while(tail->next)時(shí),會(huì)有空指針的風(fēng)險(xiǎn),同時(shí)還會(huì)漏掉最后一個(gè)結(jié)點(diǎn)。
9.在某個(gè)結(jié)點(diǎn)后面插入
void SLInsertAfter(SLTNode* pos, SLTDataType x)//在某個(gè)結(jié)點(diǎn)的后面插入某個(gè)數(shù)
{
assert(pos);
SLTNode* newnode = BuySLTNode(x);
SLTNode* tail = pos->next;
pos->next = newnode;
newnode->next = tail;
}
10.在某個(gè)結(jié)點(diǎn)前面插入
void SLTInsert(SLTNode** pphead,SLTNode* pos, SLTDataType x)//在某個(gè)結(jié)點(diǎn)的前面插入某個(gè)數(shù)
{
assert(pos);
SLTNode* newnode = BuySLTNode(x);
if (pos == *pphead)
{
/*newnode->next = *pphead;
*pphead = newnode;*/
SLTPushBack(pphead, x);
}
else
{
SLTNode* tail = *pphead;
while (tail->next != pos)
{
tail = tail->next;
}
tail->next = newnode;
newnode->next = pos;
}
}11.刪除某個(gè)位置后面的結(jié)點(diǎn)
void SLTEraseAfter(SLTNode* pos)//刪除pos位置之后的那個(gè)位置
{
assert(pos);
if (pos->next == NULL)
{
return;
}
else
{
SLTNode* tail = pos->next;
pos->next = tail->next;
free(tail);
}
}
12.刪除某個(gè)結(jié)點(diǎn)
void SLTErase(SLTNode** pphead, SLTNode* pos)//刪除pos位置
{
assert(pos);
SLTNode* tail = *pphead;
if (pos == *pphead)
{
SLTNode* nextNode = (*pphead)->next;
free(*pphead);
*pphead = nextNode;
}
else
{
while (tail->next != pos)
{
tail = tail->next;
}
tail->next = pos->next;
free(pos);
}
}13.銷(xiāo)毀單鏈表
void SLTDestroy(SLTNode** pphead)//銷(xiāo)毀
{
assert(*pphead);
SLTNode* tail = *pphead;
while (tail)
{
SLTNode* next = tail->next;
free(tail);
tail = next;
}
*pphead = NULL;
}
注意最后要將頭結(jié)點(diǎn)置空。
三、測(cè)試代碼
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int SLTDataType;
typedef struct SList {
struct Slist* next;
SLTDataType val;
}SLTNode;
SLTNode* BuySLTNode(SLTDataType x)
{
SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
if (newnode == NULL)
{
perror("BuySLTNode malloc");
exit(-1);
}
newnode->val = x;
newnode->next = NULL;
return newnode;
}
SLTNode* CreateSList(int n)
{
int i = 0;
SLTNode* ptail = NULL, * phead = NULL;
for (i = 0; i < n; i++)
{
SLTNode* newnode = BuySLTNode(i);
if (phead == NULL)
{
ptail = phead = newnode;
}
else
{
ptail->next = newnode;
ptail = newnode;
}
}
return phead;
}
//void CreateSList(SLTNode** pphead, int n)
//{
// int i = 0;
// SLTNode* ptail = NULL;
// for (i = 0; i < n; i++)
// {
// SLTNode* newnode = BuySLTNode(i);
// if (*pphead == NULL)
// {
// ptail = *pphead = newnode;
// }
// else
// {
// ptail->next= newnode;
// ptail = newnode;
// }
// }
//}
void SLTPrint(SLTNode* phead)
{
SLTNode* tail = phead;
while (tail)
{
printf("%d ", tail->val);
tail = tail->next;
}
printf("\n");
}
void SLTPushBack(SLTNode** pphead, SLTDataType x)//尾插
{
SLTNode* tail = *pphead;
SLTNode* newnode = BuySLTNode(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
while (tail->next)
{
tail = tail->next;
}
tail->next = newnode;
}
}
void SLTPopBack(SLTNode** pphead)//尾刪
{
assert(*pphead);
if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
else
{
SLTNode* tail = *pphead;
SLTNode* prev = NULL;
while (tail->next)
{
prev = tail;
tail = tail->next;
}
free(tail);
prev->next = NULL;
}
}
void SLTPushFront(SLTNode** pphead, SLTDataType x)//頭插
{
SLTNode* newnode = BuySLTNode(x);
newnode->next = *pphead;
*pphead = newnode;
}
void SLTPopFront(SLTNode** pphead)//頭刪
{
assert(*pphead);
SLTNode* nextnode = (*pphead)->next;
free(*pphead);
*pphead = nextnode;
}
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)//查找某個(gè)數(shù)并返回所在位置
{
SLTNode* tail = phead;
while (tail)
{
if (tail->val == x)
{
return tail;
}
tail = tail->next;
}
return NULL;
}
void SLInsertAfter(SLTNode* pos, SLTDataType x)//在某個(gè)結(jié)點(diǎn)的后面插入某個(gè)數(shù)
{
assert(pos);
SLTNode* newnode = BuySLTNode(x);
SLTNode* tail = pos->next;
pos->next = newnode;
newnode->next = tail;
}
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)//在某個(gè)結(jié)點(diǎn)的前面插入某個(gè)數(shù)
{
assert(pos);
SLTNode* newnode = BuySLTNode(x);
if (pos == *pphead)
{
/*newnode->next = *pphead;
*pphead = newnode;*/
SLTPushBack(pphead, x);
}
else
{
SLTNode* tail = *pphead;
while (tail->next != pos)
{
tail = tail->next;
}
tail->next = newnode;
newnode->next = pos;
}
}
void SLTEraseAfter(SLTNode* pos)//刪除pos位置之后的那個(gè)位置
{
assert(pos);
if (pos->next == NULL)
{
return;
}
else
{
SLTNode* tail = pos->next;
pos->next = tail->next;
free(tail);
}
}
void SLTErase(SLTNode** pphead, SLTNode* pos)//刪除pos位置
{
assert(pos);
SLTNode* tail = *pphead;
if (pos == *pphead)
{
SLTNode* nextNode = (*pphead)->next;
free(*pphead);
*pphead = nextNode;
}
else
{
while (tail->next != pos)
{
tail = tail->next;
}
tail->next = pos->next;
free(pos);
}
}
void SLTDestroy(SLTNode** pphead)//銷(xiāo)毀
{
assert(*pphead);
SLTNode* tail = *pphead;
while (tail)
{
SLTNode* next = tail->next;
free(tail);
tail = next;
}
*pphead = NULL;
}
void TestSL()
{
SLTNode* plist = CreateSList(2);
/*SLTNode* plist = NULL;
CreateSList(&plist,10);//沒(méi)有返回值
SLTPrint(plist);*/
SLTPushBack(&plist, 600);
SLTPushBack(&plist, 200);
SLTPushBack(&plist, 300);
SLTPushBack(&plist, 400);
SLTPrint(plist);
SLTPopBack(&plist);
SLTPopBack(&plist);
SLTPopBack(&plist);
SLTPrint(plist);
SLTPushFront(&plist, 100);
SLTPushFront(&plist, 200);
SLTPushFront(&plist, 300);
SLTPushFront(&plist, 400);
SLTPrint(plist);
SLTPopFront(&plist);
SLTPopFront(&plist);
SLTPrint(plist);
SLTNode* pos = SLTFind(plist, 600);
if (pos)
{
SLInsertAfter(pos, 700);
SLTInsert(&plist, pos, 500);
SLTEraseAfter(pos);
//SLTErase(&plist, pos);
}
SLTPrint(plist);
SLTDestroy(&plist);
SLTPrint(plist);
}
int main()
{
TestSL();
return 0;
}
以上就是C語(yǔ)言中單鏈表(不帶頭結(jié)點(diǎn))基本操作的實(shí)現(xiàn)詳解的詳細(xì)內(nèi)容,更多關(guān)于C語(yǔ)言單鏈表的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之判斷循環(huán)鏈表空與滿(mǎn)
- C語(yǔ)言如何建立動(dòng)態(tài)鏈表問(wèn)題
- C語(yǔ)言利用鏈表實(shí)現(xiàn)學(xué)生成績(jī)管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)手寫(xiě)Map(數(shù)組+鏈表+紅黑樹(shù))的示例代碼
- C語(yǔ)言中如何實(shí)現(xiàn)單鏈表刪除指定結(jié)點(diǎn)
- C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之單鏈表的實(shí)現(xiàn)
- C語(yǔ)言刷題判斷鏈表中是否有環(huán)題解
相關(guān)文章
C++如何實(shí)現(xiàn)簡(jiǎn)易掃雷游戲
這篇文章主要為大家詳細(xì)介紹了C++如何實(shí)現(xiàn)簡(jiǎn)易掃雷游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
C++實(shí)現(xiàn)LeetCode(95.獨(dú)一無(wú)二的二叉搜索樹(shù)之二)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(95.獨(dú)一無(wú)二的二叉搜索樹(shù)之二),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
詳解如何從Matlab中導(dǎo)出清晰的結(jié)果圖片
寫(xiě)文章的時(shí)候有時(shí)需要matlab導(dǎo)出清晰的圖片,如果直接用figure里面的保存的話(huà)不夠清晰,下面這篇文章主要給大家介紹了關(guān)于如何從Matlab中導(dǎo)出清晰的結(jié)果圖片的相關(guān)資料,需要的朋友可以參考下2022-06-06
OpenGL實(shí)現(xiàn)不規(guī)則區(qū)域填充算法
這篇文章主要為大家詳細(xì)介紹了OpenGL實(shí)現(xiàn)不規(guī)則區(qū)域填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
詳解C語(yǔ)言中的符號(hào)常量、變量與算術(shù)表達(dá)式
這篇文章主要介紹了C語(yǔ)言中的符號(hào)常量、變量與算術(shù)表達(dá)式,是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11
探討C語(yǔ)言中關(guān)鍵字volatile的含義
本篇文章是對(duì)C語(yǔ)言中關(guān)鍵字volatile的含義進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語(yǔ)言在輸入輸出時(shí)遇到的常見(jiàn)問(wèn)題總結(jié)

