C語(yǔ)言 單向鏈表的增刪查改快速掌握
前言
鏈表是線性表的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu),它可以以O(shè)(1)的時(shí)間復(fù)雜度進(jìn)行插入或者刪除,同時(shí)由于是鏈?zhǔn)浇Y(jié)構(gòu)相比順序表而言,不會(huì)存在空間浪費(fèi)的情況。而鏈表又分為帶頭單向鏈表,不帶頭單向鏈表,帶頭循環(huán)鏈表,不帶頭循環(huán)鏈表,帶頭雙向循環(huán)鏈表,不帶頭雙向循環(huán)鏈表,帶頭雙向鏈表,不帶頭雙向鏈表,總共有八種,其中結(jié)構(gòu)最簡(jiǎn)單的是不帶頭單向鏈表,也是實(shí)現(xiàn)起來(lái)最容易出錯(cuò)的。并且我們?cè)诰W(wǎng)上進(jìn)行鏈表的oj時(shí),題目基本也是不帶頭的單向鏈表,而且也是互聯(lián)網(wǎng)大廠面試中最容易考的。
一、創(chuàng)建
typedef int SLTDadaType;//存放的數(shù)據(jù)類(lèi)型
struct SListNode
{
SLTDadaType _data;//存放的數(shù)據(jù)
struct SListNode* _next;//指向下一個(gè)節(jié)點(diǎn)的指針
};
typedef struct SListNode SListNode;
二、單向鏈表的函數(shù)聲明
SListNode* BuyListNode(SLTDadaType x);//創(chuàng)建一個(gè)節(jié)點(diǎn) SListNode* SListPushBack(SListNode* head, SLTDadaType x);//尾插 SListNode* SListPopBack(SListNode* head);//頭插 SListNode* SListPushFornt(SListNode* head, SLTDadaType x);//尾刪 SListNode* SListPopFornt(SListNode* head);//頭刪 SListNode* SListFind(SListNode* head, SLTDadaType x);//查找一個(gè)節(jié)點(diǎn) void SListModify(SListNode* head, SLTDadaType x,SLTDadaType y);//x修改
三、函數(shù)實(shí)現(xiàn)
1.創(chuàng)建節(jié)點(diǎn)
SListNode* BuyListNode(SLTDadaType x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
newnode->_data = x;
newnode->_next = NULL;
return newnode;
}
2.尾插節(jié)點(diǎn)
SListNode* SListPushBack(SListNode* head, SLTDadaType x)
{
SListNode* newnode = BuyListNode(x);//無(wú)論節(jié)點(diǎn)是否為空,都先進(jìn)行創(chuàng)建一個(gè)節(jié)點(diǎn)
if (head == NULL) //頭節(jié)點(diǎn)為空
{
head = newnode;
return head;
}
else //頭節(jié)點(diǎn)不為空,直接遍歷到鏈表結(jié)尾進(jìn)行尾插
{
SListNode* tail = head;
while (tail->_next != NULL)
{
tail = tail->_next;
}
tail->_next = newnode;
return head;
}
}
3.頭插
SListNode* SListPushFornt(SListNode* head, SLTDadaType x)
{
SListNode* newnode = BuyListNode(x);
newnode->_next = head;
head = newnode;
return head;
}
4.尾刪
SListNode* SListPopBack(SListNode* head)
{
//1.空
//2.只有一個(gè)節(jié)點(diǎn)
//3.有多個(gè)節(jié)點(diǎn)
if (head == NULL)
{
return head;
}
else if (head->_next== NULL)
{
free(head);
head = NULL;
return head;
}
else
{
SListNode* prev = NULL;
SListNode* tail = head;
while (tail->_next != NULL) //利用前指針來(lái)保存要?jiǎng)h除的節(jié)點(diǎn)的前一個(gè)節(jié)點(diǎn)
{
prev = tail;
tail = tail->_next;
}
free(tail);
if (prev != NULL)
prev->_next = NULL;
return head;
}
}
5.頭刪
SListNode* SListPopFornt(SListNode* head)
{
if (head == NULL)
{
return head;
}
else
{
SListNode* cur = head->_next;
free(head);
head = cur;
return head;
}
}
6.查找節(jié)點(diǎn)
SListNode* SListFind(SListNode* head, SLTDadaType x)
{
SListNode* cur = head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
else
{
cur = cur->_next;
}
}
return NULL;
}
7.修改
void SListModify(SListNode* head, SLTDadaType x, SLTDadaType y)//x修改
{
SListNode* find = SListFind(head, x);
if (find)
{
find->_data = y;
}
else
{
printf("對(duì)不起,您要修改的值不存在\n");
}
}
總結(jié)
本篇文章主要是針對(duì)單向鏈表一些基本操作的代碼實(shí)現(xiàn),若有寫(xiě)的錯(cuò)誤或值得改進(jìn)的地方,請(qǐng)大家多多留言指出。
最后,也請(qǐng)大家多多支持,求關(guān)注?。?!
到此這篇關(guān)于C語(yǔ)言 單向鏈表的增刪查改快速掌握的文章就介紹到這了,更多相關(guān)C語(yǔ)言 單向鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)超市計(jì)價(jià)收款系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)超市計(jì)價(jià)收款系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
C語(yǔ)言代碼實(shí)現(xiàn)通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言代碼實(shí)現(xiàn)通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
解析內(nèi)存對(duì)齊 Data alignment: Straighten up and fly right的詳解
對(duì)于所有直接操作內(nèi)存的程序員來(lái)說(shuō),數(shù)據(jù)對(duì)齊都是很重要的問(wèn)題.數(shù)據(jù)對(duì)齊對(duì)你的程序的表現(xiàn)甚至能否正常運(yùn)行都會(huì)產(chǎn)生影響2013-05-05
C語(yǔ)言程序如何求學(xué)生總成績(jī)和平均成績(jī)
這篇文章主要介紹了C語(yǔ)言程序如何求學(xué)生總成績(jī)和平均成績(jī),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
學(xué)習(xí)C和C++的9點(diǎn)經(jīng)驗(yàn)總結(jié)
本文給大家總結(jié)了一下我們?cè)趯W(xué)習(xí)C和C++的時(shí)候的一些經(jīng)驗(yàn)和需要注意的事項(xiàng),希望能給大家一些幫助,少走些彎路2015-12-12

