C語言數(shù)據(jù)結(jié)構(gòu)實(shí)例講解單鏈表的實(shí)現(xiàn)
這里我們來簡(jiǎn)單實(shí)現(xiàn)單鏈表的增刪查找。
1、單鏈表
概念:鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的 。


(鏈表和我們生活中最接近的就是火車了。)
2、單鏈表的實(shí)現(xiàn)
接下來我們來實(shí)現(xiàn)單鏈表的增刪查改
頭文件
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
typedef int SLDataType;
//鏈表的創(chuàng)建
typedef struct SListNode
{
SLDataType data;//val
struct SListNode* next;//存儲(chǔ)下一個(gè)結(jié)點(diǎn)的地址
}SListNode,SLN;
//打印鏈表
void SListPrint(SListNode* phead);
//尾插
void SListPushBack(SListNode** pphead, SLDataType x);
//頭插
void SListPushFront(SListNode** pphead, SLDataType x);
//尾刪
void SListPopBack(SListNode** pphead);
//頭刪
void SListPopFront(SListNode** pphead);
//查找
SListNode* SListFind(SListNode* phead, SLDataType x);
//在pos位置之前插入
void SListInsert(SListNode** pphead, SListNode* pos, SLDataType x);
//刪除pos位置
void SListErase(SListNode** pphead, SListNode* pos);
//在pos位置之后插入
void SlistInserAfter(SListNode* pos, SLDataType x);
//刪除pos后的值
void SlistEraseAfter(SListNode* pos);
//用完銷毀
void SListDestroy(SListNode** pphead);函數(shù)的實(shí)現(xiàn)
(1)打印鏈表
void SListPrint(SListNode* phead)
{
assert(phead);
SListNode* cur = phead;
if (cur == NULL)
{
printf("SList is NULL\n");
}
while (cur != NULL)
{
printf("%d->", cur->data);
cur = cur->next;
}
printf("NULL\n");
}(2)動(dòng)態(tài)申請(qǐng)結(jié)點(diǎn)
將一個(gè)data x動(dòng)態(tài)申請(qǐng)結(jié)點(diǎn)。

SListNode* BuySList(SLDataType x)
{
SListNode* newnode = (SListNode*)malloc(sizeof(SListNode));
if (newnode == NULL)
{
printf("malloc fail\n");
exit(-1);
}
else
{
newnode->data = x;
newnode->next = NULL;
}
return newnode;
}(3)尾插


void SListPushBack(SListNode** pphead, SLDataType x)
{
assert(pphead);
SListNode* newnode = BuySList(x);
if (*pphead == NULL)
{
*pphead = newnode;
}
else
{
//找尾
SListNode* tail = *pphead;
while (tail->next != NULL)
{
tail = tail->next;
}
//走完循環(huán)找到尾
tail->next = newnode;
}
}(4)頭插

void SListPushFront(SListNode** pphead, SLDataType x)
{
assert(pphead);
SListNode* newnode = BuySList(x);
newnode->next = *pphead;
*pphead = newnode;
}(5)尾刪

void SListPopBack(SListNode** pphead)
{
assert(pphead);
//當(dāng)鏈表只有一個(gè)結(jié)點(diǎn)時(shí)
if (*pphead == NULL)
{
printf("SListNode is NULL\n");
return;
}
//當(dāng)鏈表只有一個(gè)結(jié)點(diǎn)時(shí)
else if ((*pphead)->next == NULL)
{
free(*pphead);
*pphead = NULL;
}
//當(dāng)鏈表有多個(gè)結(jié)點(diǎn)時(shí)
else
{
SListNode* tail = *pphead;
while (tail->next->next != NULL)
{
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
}
}(6)頭刪

void SListPopFront(SListNode** pphead)
{
assert(pphead);
if (*pphead == NULL)
{
printf("SList is NULL\n");
return;
}
else
{
SListNode* next = (*pphead)->next;
free(*pphead);
*pphead = next;
}
}(7)查找
SListNode* SListFind(SListNode* phead, SLDataType x)
{
assert(phead);
SListNode* cur = phead;
while (cur != NULL)
{
if (cur->data == x)
{
return cur;
}
//如果沒找到就往下走
cur = cur->next;
}
//循環(huán)完成后還沒找到就說明鏈表中沒有該值
return NULL;
}(8)在pos之前插入


void SListInsert(SListNode** pphead, SListNode* pos, SLDataType x)
{
assert(pphead);
assert(pos);
//pos是第一個(gè)位置
if (pos == *pphead)
{
SListPushFront(pphead, x);
}
//pos不是第一個(gè)位置
else
{
SListNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
SListNode* newnode = BuySList(x);
prev->next = newnode;
newnode->next = pos;
}
}(9)刪除pos

void SListErase(SListNode** pphead, SListNode* pos)
{
assert(pphead);
assert(pos);
//1、頭結(jié)點(diǎn)為空
if (*pphead == NULL)
{
printf("SList is NULL\n");
return;
}
//2、刪除第一個(gè)結(jié)點(diǎn)
else if (pos == *pphead)
{
SListPopFront(pphead);
}
//3、其他結(jié)點(diǎn)
else
{
SListNode* prev = *pphead;
while (prev->next != pos)
{
prev = prev->next;
}
prev->next = pos->next;
free(pos);
pos = NULL;
}
}(10)在pos之后插入
相對(duì)于在pos之前插入,在pos后插入可以不用傳頭結(jié)點(diǎn),無論pos在哪個(gè)位置都適用。

void SListInsertAfter(SListNode* pos, SLDataType x)
{
assert(pos);
SListNode* newnode = BuySList(x);
SListNode* next = pos->next;
pos->next = newnode;
newnode->next = next;
//下面這種方式也可以
/*SListNode* newnode = BuySList(x);
newnode->next = pos->next;
pos->next = newnode;*/
}(11)在pos后刪除

void SListEraseAfter(SListNode* pos)
{
assert(pos);
SListNode* next = pos->next;
if (next)
{
pos->next = next->next;
free(next);
next = NULL;
}
}(12)最后用完記得銷毀
void SListDestroy(SListNode** pphead)
{
assert(pphead);
SListNode* cur = *pphead;
while (cur)
{
SListNode* next = cur->next;
free(cur);
cur = next;
}
*pphead = NULL;
}3、各功能的測(cè)試
#include "SList.h"
void test1()
{
SListNode* slist = NULL;
//測(cè)試尾插
SListPushBack(&slist, 1);
SListPushBack(&slist, 2);
SListPushBack(&slist, 3);
SListPushFront(&slist, 5);
SListPushFront(&slist, 4);
SListPrint(slist);
//測(cè)試頭插
SListPushFront(&slist, 5);
SListPushFront(&slist, 4);
SListPrint(slist);
//測(cè)試尾刪
SListPopBack(&slist);
SListPopBack(&slist);
SListPrint(slist);
//測(cè)試頭刪
SListPopFront(&slist);
SListPopFront(&slist);
SListPopFront(&slist);
SListPrint(slist);
//測(cè)試查找
SListNode* ret1 = SListFind(slist, 5);
printf("%d\n", ret1->data);
/*SListNode* ret2 = SListFind(slist, 2);
printf("%d\n", ret2->data);*/
//pos前插測(cè)試
SListNode* pos = SListFind(slist, 1);
if (pos)
{
SListInsert(&slist,pos,3);
}
SListPrint(slist);
pos = SListFind(slist, 1);
if (pos)
{
SListInsert(&slist, pos, 10);
}
SListPrint(slist);
//刪除pos測(cè)試
pos = SListFind(slist, 10);
if (pos)
{
SListErase(&slist, pos);
}
SListPrint(slist);
//測(cè)試在pos后插入
pos = SListFind(slist, 3);
if (pos)
{
SListInsertAfter(pos, 6);
}
SListPrint(slist);
pos = SListFind(slist, 1);
if (pos)
{
SListInsertAfter(pos, 8);
}
SListPrint(slist);
//測(cè)試刪除pos后的值
pos = SListFind(slist, 1);
if (pos)
{
SListEraseAfter(pos);
}
SListPrint(slist);
}
int main()
{
test1();
return 0;
}運(yùn)行結(jié)果:

單鏈表的實(shí)現(xiàn)到此結(jié)束,如果你還想更進(jìn)一步,請(qǐng)關(guān)注后續(xù)----單鏈表OJ,讓你從此不再迷茫!
到此這篇關(guān)于C語言數(shù)據(jù)結(jié)構(gòu)實(shí)例講解單鏈表的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C語言 單鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言 字符串首字母轉(zhuǎn)換成大寫簡(jiǎn)單實(shí)例
這篇文章主要介紹了C語言 字符串首字母轉(zhuǎn)換成大寫簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
C語言修煉之路一朝函數(shù)思習(xí)得?模塊思維世間生上篇
函數(shù)是一組一起執(zhí)行一個(gè)任務(wù)的語句。每個(gè)?C?程序都至少有一個(gè)函數(shù),即主函數(shù)?main()?,所有簡(jiǎn)單的程序都可以定義其他額外的函數(shù)2022-03-03
vscode 配置 C/C++ 編譯環(huán)境的詳細(xì)圖文教程
這篇文章主要介紹了vscode 配置 C/C++ 編譯環(huán)境的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
C語言fprintf()函數(shù)和fscanf()函數(shù)的具體使用
本文主要介紹了C語言fprintf()函數(shù)和fscanf()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11

