網(wǎng)站首頁 編程語言 正文
1、插入操作
(1)創(chuàng)建一個(gè)新的要插入的結(jié)點(diǎn)
(2)將新結(jié)點(diǎn)的 next 指針指向插入位置后的結(jié)點(diǎn)
(3)將插入位置前的節(jié)點(diǎn)指針 next 指向新的結(jié)點(diǎn)
?注意:步驟(2)(3)的順序不能顛倒,否則會(huì)導(dǎo)致插入位置后的部分鏈表丟失。
插入位置一共分三種,分別是頭部插入、中間插入和尾部插入。
如圖:
代碼:
link* insertElem(link* p,int elem,int pos){
link* temp = p;//創(chuàng)建臨時(shí)結(jié)點(diǎn)temp
//首先找到插入位置的上一個(gè)結(jié)點(diǎn)
for(int i=1;i<pos;i++){
temp=temp->next;
if(temp==NULL){
printf("插入位置無效\n");
return p;
}
}
//創(chuàng)建插入結(jié)點(diǎn)c
link *c=(link*)malloc(sizeof(link));
c->elem=elem;
//向鏈表中插入結(jié)點(diǎn)
c->next = temp->next;
temp->next=c;
return p;
}
2、刪除操作
(1)將結(jié)點(diǎn)從鏈表中摘下
temp->next=temp->next->next
(2)手動(dòng)釋放掉結(jié)點(diǎn),回收被結(jié)點(diǎn)占用的存儲(chǔ)空間
如圖:
?代碼:
link*delElem(link*p,int pos){//p是原鏈表,pos是要?jiǎng)h除的元素值
link* temp=p;
//遍歷到被刪除結(jié)點(diǎn)的上一個(gè)結(jié)點(diǎn)
for(int i =1;i<pos;i++){
temp=temp->next;
if(temp->next==NULL){
printf("沒有該結(jié)點(diǎn)\n");
return p;
}
}
link*del=temp->next;//單獨(dú)開辟一塊空間存放被刪結(jié)點(diǎn)
temp->next=temp->next->next;//從鏈表中摘除被刪結(jié)點(diǎn)
free(del);//手動(dòng)釋放該結(jié)點(diǎn)防止內(nèi)存泄漏
return p;
}
3、查找操作
從頭遍歷鏈表。
int searchElem(link* p,int elem){
//新建一個(gè)指針t,初始化為頭指針 p
link*t=p;
int i=1;
//由于頭節(jié)點(diǎn)的存在,因此while中的判斷為t->next
while(t->next){
t=t->next;
if(t->elem==elem){
return i;
}
i++;
}
//程序執(zhí)行至此處,表示查找失敗
return -1;
}
4、修改操作
先找到目標(biāo)位置再更改結(jié)點(diǎn)的數(shù)據(jù)域。
link* changeElem(link* p,int pos,int newElem){
link* temp=p;
temp=temp->next;//在遍歷之前,temp指向首元結(jié)點(diǎn)
//遍歷到待更新結(jié)點(diǎn)
for(int i=1;i<pos;i++){
temp=temp->next;
}
temp->elem=newElem;
return p;
}
5、完整代碼
#include<stdio.h>
#include<stdlib.h>
typedef struct Link{
int elem;//數(shù)據(jù)域
struct Link *next;//指針域,用來連接后繼元素
}link;//link為節(jié)點(diǎn)名,每個(gè)結(jié)點(diǎn)都是一個(gè)link結(jié)構(gòu)體
link* initLink(){
link *p=(link*)malloc(sizeof(link));//創(chuàng)建頭結(jié)點(diǎn)
link*temp = p;//聲明一個(gè)指針temp指向頭結(jié)點(diǎn),也就是頭結(jié)點(diǎn)的地址賦值給指針變量(注意這不是頭指針而是用來連接數(shù)組的臨時(shí)指針變量)
//生成鏈表
for(int i=1;i<5;i++)
{
link *a=(link*)malloc(sizeof(link));//生成一個(gè)結(jié)點(diǎn)
a->elem=i;//給結(jié)點(diǎn)的數(shù)據(jù)域賦值
a->next=NULL;//指針域設(shè)置為空
temp->next=a;//上一個(gè)結(jié)點(diǎn)的指針指向新增結(jié)點(diǎn)
temp=temp->next;//臨時(shí)指針向后移動(dòng)也可寫成temp=a
}
//返回頭結(jié)點(diǎn),通過頭節(jié)點(diǎn)的指針即可找到整個(gè)鏈表
return p;
}
link* insertElem(link* p,int elem,int pos){
link* temp = p;//創(chuàng)建臨時(shí)結(jié)點(diǎn)temp
//首先找到插入位置的上一個(gè)結(jié)點(diǎn)
for(int i=1;i<pos;i++){
temp=temp->next;
if(temp==NULL){
printf("插入位置無效\n");
return p;
}
}
//創(chuàng)建插入結(jié)點(diǎn)c
link *c=(link*)malloc(sizeof(link));
c->elem=elem;
//向鏈表中插入結(jié)點(diǎn)
c->next = temp->next;
temp->next=c;
return p;
}
link* delElem(link*p,int pos){//p是原鏈表,pos是要?jiǎng)h除的元素值
link* temp=p;
//遍歷到被刪除結(jié)點(diǎn)的上一個(gè)結(jié)點(diǎn)
for(int i =1;i<pos;i++){
temp=temp->next;
if(temp->next==NULL){
printf("沒有該結(jié)點(diǎn)\n");
return p;
}
}
link*del=temp->next;//單獨(dú)開辟一塊空間存放被刪結(jié)點(diǎn)
temp->next=temp->next->next;//從鏈表中摘除被刪結(jié)點(diǎn)
free(del);//手動(dòng)釋放該結(jié)點(diǎn)防止內(nèi)存泄漏
return p;
}
link* changeElem(link* p,int pos,int newElem){
link* temp=p;
temp=temp->next;//在遍歷之前,temp指向首元結(jié)點(diǎn)
//遍歷到待更新結(jié)點(diǎn)
for(int i=1;i<pos;i++){
temp=temp->next;
}
temp->elem=newElem;
return p;
}
int searchElem(link* p,int elem){
//新建一個(gè)指針t,初始化為頭指針 p
link*t=p;
int i=1;
//由于頭節(jié)點(diǎn)的存在,因此while中的判斷為t->next
while(t->next){
t=t->next;
if(t->elem==elem){
return i;
}
i++;
}
//程序執(zhí)行至此處,表示查找失敗
return -1;
}
void display(link *p){
link*temp=p;//將temp指向頭結(jié)點(diǎn)
printf("鏈表中的數(shù)據(jù)為 : ");
//只要temp指針指向的結(jié)點(diǎn)的next不是Null,就執(zhí)行輸出語句。
while(temp->next){
temp=temp->next;
printf("%d ",temp->elem);
}
printf("\n");
}
int main()
{
//初始化鏈表
link *p = initLink();
display(p);
printf("在第4的位置插入元素5:\n");
p = insertElem(p, 5, 4);
display(p);
printf("刪除元素3:\n");
p = delElem(p, 3);
display(p);
printf("查找元素2的位置為:\n");
int address = searchElem(p, 2);
if (address == -1) {
printf("沒有該元素");
}
else {
printf("元素2的位置為:%d\n", address);
}
printf("更改第3的位置上的數(shù)據(jù)為7:\n");
p = changeElem(p, 3, 7);
display(p);
return 0;
}
輸出結(jié)果:
原文鏈接:https://blog.csdn.net/qq_51701007/article/details/126009653
相關(guān)推薦
- 2022-11-02 python中l(wèi)ist列表刪除元素的四種方法實(shí)例_python
- 2022-12-05 python?os.stat()如何獲取相關(guān)文件的系統(tǒng)狀態(tài)信息_python
- 2023-02-28 css字體10px方法
- 2022-12-02 C語言實(shí)現(xiàn)三子棋小游戲的示例代碼_C 語言
- 2022-06-13 C語言strlen函數(shù)實(shí)現(xiàn)讀取字符串長度詳解_C 語言
- 2022-09-15 C++關(guān)于/2和>>1的區(qū)別說明_C 語言
- 2022-11-17 WPF利用DrawingContext實(shí)現(xiàn)繪制溫度計(jì)_C#教程
- 2022-06-28 使用?Docker?Compose?構(gòu)建復(fù)雜的多容器?App的方法_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支