網站首頁 編程語言 正文
1、插入操作
(1)創(chuàng)建一個新的要插入的結點
(2)將新結點的 next 指針指向插入位置后的結點
(3)將插入位置前的節(jié)點指針 next 指向新的結點
?注意:步驟(2)(3)的順序不能顛倒,否則會導致插入位置后的部分鏈表丟失。
插入位置一共分三種,分別是頭部插入、中間插入和尾部插入。
如圖:
代碼:
link* insertElem(link* p,int elem,int pos){
link* temp = p;//創(chuàng)建臨時結點temp
//首先找到插入位置的上一個結點
for(int i=1;i<pos;i++){
temp=temp->next;
if(temp==NULL){
printf("插入位置無效\n");
return p;
}
}
//創(chuàng)建插入結點c
link *c=(link*)malloc(sizeof(link));
c->elem=elem;
//向鏈表中插入結點
c->next = temp->next;
temp->next=c;
return p;
}
2、刪除操作
(1)將結點從鏈表中摘下
temp->next=temp->next->next
(2)手動釋放掉結點,回收被結點占用的存儲空間
如圖:
?代碼:
link*delElem(link*p,int pos){//p是原鏈表,pos是要刪除的元素值
link* temp=p;
//遍歷到被刪除結點的上一個結點
for(int i =1;i<pos;i++){
temp=temp->next;
if(temp->next==NULL){
printf("沒有該結點\n");
return p;
}
}
link*del=temp->next;//單獨開辟一塊空間存放被刪結點
temp->next=temp->next->next;//從鏈表中摘除被刪結點
free(del);//手動釋放該結點防止內存泄漏
return p;
}
3、查找操作
從頭遍歷鏈表。
int searchElem(link* p,int elem){
//新建一個指針t,初始化為頭指針 p
link*t=p;
int i=1;
//由于頭節(jié)點的存在,因此while中的判斷為t->next
while(t->next){
t=t->next;
if(t->elem==elem){
return i;
}
i++;
}
//程序執(zhí)行至此處,表示查找失敗
return -1;
}
4、修改操作
先找到目標位置再更改結點的數據域。
link* changeElem(link* p,int pos,int newElem){
link* temp=p;
temp=temp->next;//在遍歷之前,temp指向首元結點
//遍歷到待更新結點
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;//數據域
struct Link *next;//指針域,用來連接后繼元素
}link;//link為節(jié)點名,每個結點都是一個link結構體
link* initLink(){
link *p=(link*)malloc(sizeof(link));//創(chuàng)建頭結點
link*temp = p;//聲明一個指針temp指向頭結點,也就是頭結點的地址賦值給指針變量(注意這不是頭指針而是用來連接數組的臨時指針變量)
//生成鏈表
for(int i=1;i<5;i++)
{
link *a=(link*)malloc(sizeof(link));//生成一個結點
a->elem=i;//給結點的數據域賦值
a->next=NULL;//指針域設置為空
temp->next=a;//上一個結點的指針指向新增結點
temp=temp->next;//臨時指針向后移動也可寫成temp=a
}
//返回頭結點,通過頭節(jié)點的指針即可找到整個鏈表
return p;
}
link* insertElem(link* p,int elem,int pos){
link* temp = p;//創(chuàng)建臨時結點temp
//首先找到插入位置的上一個結點
for(int i=1;i<pos;i++){
temp=temp->next;
if(temp==NULL){
printf("插入位置無效\n");
return p;
}
}
//創(chuàng)建插入結點c
link *c=(link*)malloc(sizeof(link));
c->elem=elem;
//向鏈表中插入結點
c->next = temp->next;
temp->next=c;
return p;
}
link* delElem(link*p,int pos){//p是原鏈表,pos是要刪除的元素值
link* temp=p;
//遍歷到被刪除結點的上一個結點
for(int i =1;i<pos;i++){
temp=temp->next;
if(temp->next==NULL){
printf("沒有該結點\n");
return p;
}
}
link*del=temp->next;//單獨開辟一塊空間存放被刪結點
temp->next=temp->next->next;//從鏈表中摘除被刪結點
free(del);//手動釋放該結點防止內存泄漏
return p;
}
link* changeElem(link* p,int pos,int newElem){
link* temp=p;
temp=temp->next;//在遍歷之前,temp指向首元結點
//遍歷到待更新結點
for(int i=1;i<pos;i++){
temp=temp->next;
}
temp->elem=newElem;
return p;
}
int searchElem(link* p,int elem){
//新建一個指針t,初始化為頭指針 p
link*t=p;
int i=1;
//由于頭節(jié)點的存在,因此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指向頭結點
printf("鏈表中的數據為 : ");
//只要temp指針指向的結點的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的位置上的數據為7:\n");
p = changeElem(p, 3, 7);
display(p);
return 0;
}
輸出結果:
原文鏈接:https://blog.csdn.net/qq_51701007/article/details/126009653
相關推薦
- 2022-04-25 利用Redis實現訪問次數限流的方法詳解_Redis
- 2023-12-20 UML類圖中各箭頭表示總結
- 2022-09-05 Hbase之查看RowKey所在Region
- 2022-03-30 解決Microsoft?Visual?C++?2010?Express?運行及調試問題_C 語言
- 2024-03-25 SpringBoot項目的基本依賴以及設置
- 2022-02-23 去除type=“number“輸入框聚焦時的上下箭頭
- 2022-05-17 Mybatis中報錯:attempted to return null from a method
- 2022-05-05 C語言的三種條件判斷語句你都了解嗎_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支