關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作
關(guān)于節(jié)點(diǎn)數(shù)據(jù)添加:
尾添加
最核心的是定義一個頭指針和一個尾指針(尾指針可以不定義但是會增加代碼的重復(fù)性,增加程序運(yùn)行時間);
關(guān)于尾添加:(注意區(qū)分有節(jié)點(diǎn)和無節(jié)點(diǎn)的情況)
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
endadd(&phead,&pend,4);
......
return 0;
}
void endadd(struct Mystruct **phead,struct Mystruct **pend, int adddata)
{
struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct));
if(NULL == pt)
return;
pt->data = adddata;
pt->pnext = NULL;
if(NULL == *phead)
{
*phead = pt;
}
else
{
(*pend)->pnext = pt;
}
*pend= pt;
}頭添加
關(guān)于代碼思路與尾添加基本一致,注意區(qū)分節(jié)點(diǎn)的鏈接:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
head_add(&phead,&pend,4);
......
return 0;
}
void head_add(struct Mystruct **phead,struct Mystruct **pend, int adddata)
{
struct Mystruct *pt = (struct Mystruct *)malloc(sizeof(struct Mystruct));
if(NULL == pt)
return;
pt->data = adddata;
pt->pnext = NULL;
if(NULL == *phead)
{
*pend = pt;
}
else
{
pt->pnext = (*phead);
}
*phead= pt;
}一次性添加n個x數(shù)據(jù)節(jié)點(diǎn):
利用循壞,直接調(diào)用頭添加或者尾添加:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int adddata);
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
circulate_add(&phead,&pend,4,5);
......
return 0;
}
void circulate_add(struct Mystruct **phead,struct Mystruct **pend, int count, int adddata);
{
for(int i = 0;i<count;i++)
{
endadd(phead, pend, adddata);
}
}關(guān)于查找:
根據(jù)指定數(shù)據(jù):
核心就是通過頭指針一個一個往下走找到指定節(jié)點(diǎn)的數(shù)據(jù)與所找數(shù)據(jù)是否匹配,最重要的是要使用中間變量記錄頭指針,否則就無法找到頭指針了(因為是單項鏈表):
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
void data_find(struct Mystruct *phead, int designated_data);
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
middle_data_find(phead,4);
......
return 0;
}
void data_find(struct Mystruct* phead, int designated_data)
{
if (NULL == phead)
return;
struct Mystruct* ptemp = phead;
while (ptemp != NULL)
{
if (ptemp->data == designated_data)
{
printf("找到了");
break;
}
ptemp = ptemp->pnext;
}
return;
}根據(jù)下標(biāo)查找:
思路基本不變;區(qū)別傳入指定下標(biāo);內(nèi)部定義一個計數(shù)器,當(dāng)下標(biāo)和計數(shù)器數(shù)值相等;表示鏈表存在這個節(jié)點(diǎn);可以選擇傳出或者提醒;大家思考一下,動手實(shí)踐一下。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
struct Mystruct *index_find(struct Mystruct *phead, int index);
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
middle_data_find(phead,4);
......
return 0;
}
struct Mystruct* index_find(struct Mystruct* phead, int index)
{
if (NULL == phead||index<0)
return NULL;
struct Mystruct* ptemp = phead;
int i = 0;
for (i = 0; i < index; i++)
{
ptemp = ptemp->pnext;
}
return ptemp;
}刪除頭節(jié)點(diǎn):
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
void deleat_head(struct Mystruct **phead,struct Mystruct **pend);
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
deleat_head(&phead)
......
return 0;
}
void deleat_head(struct Mystruct** phead, struct Mystruct** pend)
{
if (NULL == *phead)
return;
struct Mystruct* pt = *phead;
if ((*phead)->pnext == NULL)
{
free(pt);
*phead = NULL;
*pend = NULL;
}
else
{
*phead = (*phead)->pnext;
free(pt);
}
}
void deleat_end(struct My刪除尾節(jié)點(diǎn):
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
void deleat_end(struct Mystruct**phead,struct Mystruct**pend);
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
deleat_head(&phead)
......
return 0;
}
void deleat_end(struct Mystruct** phead, struct Mystruct** pend)
{
if (NULL == *phead)
return;
struct Mystruct* pt = *phead;
if (pt->pnext == NULL)
{
free(pt);
*phead = NULL;
*pend = NULL;
}
else
{
while (pt->pnext != (*pend))
{
if (pt->pnext == (*pend))
{
free(*pend);
*pend = pt;
pt->pnext = NULL;
pt = pt->pnext;
}
}
}
}刪除中間節(jié)點(diǎn):
這里思路改變一下:根據(jù)數(shù)據(jù)或者下標(biāo)找到前一個節(jié)點(diǎn),改變前一個節(jié)點(diǎn)的pnext指針的指向,直接指向下一個節(jié)點(diǎn),也就是這個節(jié)點(diǎn)的pnext;簡單示范一下刪除中間指定數(shù)據(jù):
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata);
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
deleat_head(&phead)
......
return 0;
}
void deleat_middlledata(struct Mystruct**phead,struct Mystruct**pend,int deleatdata)
{
if (NULL == *phead)
return;
struct Mystruct* pt = *phead;
if (pt->pnext == NULL)
{
free(pt);
*phead = NULL;
*pend = NULL;
}
}刪除全部節(jié)點(diǎn):
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Mystruct
{
int data;
struct Mystruct *pnext;
};
void deleat_all(struct Mystruct** phead, struct Mystruct** pend)
int main(void)
{
struct Mystruct *phead = NULL;
struct Mystruct *pend= NULL;
deleat_all(&phead,&pend)
......
return 0;
}
void deleat_all(struct Mystruct** phead, struct Mystruct** pend)
{
while (*phead!= NULL)
{
struct Mystruct* pt = *phead;
*phead = (*phead)->pnext;
free(pt);
}
*phead = NULL;
*pend = NULL;
}到此這篇關(guān)于關(guān)于數(shù)據(jù)結(jié)構(gòu)單向鏈表的各種操作的文章就介紹到這了,更多相關(guān)數(shù)據(jù)結(jié)構(gòu)單向鏈表操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C語言數(shù)據(jù)結(jié)構(gòu)之單向鏈表詳解
- C語言實(shí)例真題講解數(shù)據(jù)結(jié)構(gòu)中單向環(huán)形鏈表
- C語言數(shù)據(jù)結(jié)構(gòu)超詳細(xì)講解單向鏈表
- Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之單向鏈表
- C語言數(shù)據(jù)結(jié)構(gòu)之單向鏈表詳解分析
- Java數(shù)據(jù)結(jié)構(gòu)之鏈表實(shí)現(xiàn)(單向、雙向鏈表及鏈表反轉(zhuǎn))
- 數(shù)據(jù)結(jié)構(gòu)與算法:單向鏈表實(shí)現(xiàn)與封裝
- python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實(shí)例講解)
相關(guān)文章
C語言實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單的圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-03-03
C++實(shí)現(xiàn)簡單版通訊錄管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡單版通訊錄管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06
解讀C++編程中派生類的構(gòu)成和創(chuàng)建
這篇文章主要介紹了解讀C++編程中派生類的構(gòu)成和創(chuàng)建,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
學(xué)習(xí)二維動態(tài)數(shù)組指針做矩陣運(yùn)算的方法
這片文章介紹了如何利用二維動態(tài)數(shù)組指針做矩陣運(yùn)算,需要的朋友可以參考下2015-07-07
C++ 的 format 和 vformat 函數(shù)示例詳解
傳統(tǒng)C庫的printf系列函數(shù)存在安全問題,而C++推薦的基于流格式化輸入輸出雖然解決了安全性問題,但在易用性方面仍顯不足,C++11引入了新的C風(fēng)格字符串格式化函數(shù),但類型安全問題依舊存在,下面通過本文介紹C++ 的 format 和 vformat 函數(shù)示例,感興趣的朋友一起看看吧2025-02-02
vc++實(shí)現(xiàn)的tcp socket客戶端和服務(wù)端示例
這篇文章主要介紹了vc++實(shí)現(xiàn)的tcp socket客戶端和服務(wù)端示例,需要的朋友可以參考下2014-03-03

