最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)實(shí)例講解單鏈表的實(shí)現(xiàn)

 更新時(shí)間:2022年03月25日 10:45:19   作者:七憶歲和  
單鏈表是后面要學(xué)的雙鏈表以及循環(huán)鏈表的基礎(chǔ),要想繼續(xù)深入了解數(shù)據(jù)結(jié)構(gòu)以及C++,我們就要奠定好這塊基石!接下來(lái)就和我一起學(xué)習(xí)吧

這里我們來(lái)簡(jiǎn)單實(shí)現(xiàn)單鏈表的增刪查找。

1、單鏈表

概念:鏈表是一種物理存儲(chǔ)結(jié)構(gòu)上非連續(xù)、非順序的存儲(chǔ)結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過(guò)鏈表中的指針鏈接次序?qū)崿F(xiàn)的 。

 (鏈表和我們生活中最接近的就是火車了。)

2、單鏈表的實(shí)現(xiàn)

接下來(lái)我們來(lái)實(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)完成后還沒找到就說(shuō)明鏈表中沒有該值
	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),無(wú)論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語(yǔ)言數(shù)據(jù)結(jié)構(gòu)實(shí)例講解單鏈表的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C語(yǔ)言 單鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C語(yǔ)言怎么獲得進(jìn)程的PE文件信息

    C語(yǔ)言怎么獲得進(jìn)程的PE文件信息

    這篇文章主要介紹了C語(yǔ)言怎么獲得進(jìn)程的PE文件信息的相關(guān)代碼,需要的朋友可以參考下
    2016-01-01
  • C++基于遞歸和非遞歸算法求二叉樹鏡像的方法

    C++基于遞歸和非遞歸算法求二叉樹鏡像的方法

    這篇文章主要介紹了C++基于遞歸和非遞歸算法求二叉樹鏡像的方法,針對(duì)二叉樹遍歷結(jié)合實(shí)例形式分析了遞歸與非遞歸算法的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2017-05-05
  • C++20中的協(xié)程(Coroutine)的實(shí)現(xiàn)

    C++20中的協(xié)程(Coroutine)的實(shí)現(xiàn)

    這篇文章主要介紹了C++20中的協(xié)程(Coroutine)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲完整代碼

    C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲完整代碼

    大家好,本篇文章主要講的是C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲完整代碼,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01
  • C語(yǔ)言實(shí)現(xiàn)順序表的插入刪除

    C語(yǔ)言實(shí)現(xiàn)順序表的插入刪除

    這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)順序表的插入刪除,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • C++自動(dòng)生成迷宮游戲

    C++自動(dòng)生成迷宮游戲

    這篇文章主要為大家詳細(xì)介紹了C++自動(dòng)生成迷宮游戲,運(yùn)用并查集自動(dòng)生成迷宮地圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • C++構(gòu)造和解析Json的使用示例

    C++構(gòu)造和解析Json的使用示例

    今天小編就為大家分享一篇關(guān)于C++構(gòu)造和解析Json的使用示例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • C++中const char*、char const*、char * const三者的區(qū)別

    C++中const char*、char const*、char * const三者的區(qū)別

    這篇文章主要介紹了C++中const char*、char const*、char * const三者的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • CString,字符串,整數(shù)等相互轉(zhuǎn)換方法(推薦)

    CString,字符串,整數(shù)等相互轉(zhuǎn)換方法(推薦)

    下面小編就為大家?guī)?lái)一篇CString,字符串,整數(shù)等相互轉(zhuǎn)換方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • C++ Boost.Range與Adapters庫(kù)使用詳解

    C++ Boost.Range與Adapters庫(kù)使用詳解

    這篇文章主要介紹了C++ Boost.Range與Adapters庫(kù)使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-11-11

最新評(píng)論

和田县| 偃师市| 贵州省| 出国| 玉屏| 甘洛县| 余姚市| 洪江市| 沽源县| 太谷县| 封开县| 库车县| 法库县| 巴林右旗| 乐都县| 合山市| 沿河| 宁陕县| 祁门县| 湖口县| 乌拉特后旗| 尼木县| 安平县| 延边| 康保县| 江孜县| 西充县| 嘉定区| 农安县| 浮梁县| 长子县| 宜章县| 桐城市| 宜兰县| 平乐县| 闽侯县| 加查县| 琼中| 沛县| 元谋县| 常熟市|