網站首頁 編程語言 正文
單鏈表指定結點的插入
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}no;
int main()
{
no *head,*tail,*p,*q;
head=new no;
head->next=NULL;
tail=head;
int n;
printf("一共要輸入的數:");
cin>>n;
int k;
cin>>k;
for(int i=0;i<n;i++)
{
p=new no;
p->data=k;
p->next=NULL;
tail->next=p;//因為tail=head,所以tail沒有數值,tail->next才有數值
tail=p;
cin>>k;
}
printf("輸入要插入的數:");
int m;
cin>>m;
q=new no;//生成一個結點來存放這個數
q->data=m;
q->next=NULL;
printf("要插在哪個數和哪個數之間:");
int a,b;
cin>>a>>b;
p=head;
while(p->data!=m&&p->next!=NULL)
{
p=p->next;
if(p->data==a&&p->next->data==b)
{
q->next=p->next;//先處理后面的結點,保證后面的鏈表不斷開,
//q->next可以起到鏈接后面鏈表的作用
p->next=q;
}
}
p=head->next;
for(int j=0;j<n+1;j++)
{
printf("%d ",p->data);
p=p->next;
}
return 0;
}
測試一:
一共要輸入的數:5
1 2 3 4 5 0
輸入要插入的數:7
要插在哪個數和哪個數之間:1 2
1 7 2 3 4 5?
測試二:
一共要輸入的數:5
1 1 2 3 4 0
輸入要插入的數:2
要插在哪個數和哪個數之間:1 1
1 2 1 2 3 4
鏈表之在指定結點前面或后面插入新的結點
1.我們知道鏈表無非就是增刪改查這幾個操作,而在指定結點前方或后方插入結點就是增加的一種體現。
(1)在指定結點后方插入新結點
例子:1->2->3->4->5
當要在3和4之間插入新結點的時候(即在3的后方插入新結點)
步驟:
- 1.先找到3(point->data == data)
- 2.將3->next(即4的地址)賦給new->next,將新結點new與結點4建立了聯系
即:new->next = 3->next;
- 3.完成上面兩步驟后,再執行3->next = new;這樣就完成了在3的后面插入了新的結點
切記步驟2和3不可調換。
直接上代碼:
int inserFrontLinklist(struct Test* head,struct Test* new,int data1)
{
?? ?struct Test* point = head;
?? ?while(point != NULL)
?? ?{
?? ??? ?if(point->data == data1)
?? ??? ?{
?? ??? ??? ?//后插法的做法
?? ??? ??? ?new->next = point->next;
?? ??? ??? ?point->next = new;
?? ??? ??? ?return 1;
?? ??? ?}
?? ??? ?point = point->next;
?? ?}?? ?
?? ?return 0;
}
(2)在指定結點前方插入新結點
例子:1->2->3->4->5
在指定結點前方插入新結點要考慮兩種情況,一種是特殊情況,看是否插入的位置是在鏈表頭,另一種情況是正常插入,插入的地方不是鏈表頭,那么我們一起來看看你兩種情況怎么辦。
1.如果要插入的點剛好是在鏈表頭的話
?? ?struct Test* point = head;
?? ?if(point->data == data)
?? ?{
?? ??? ?new->next = head;
?? ??? ?return new;
?? ?}//如果要找的那個數剛好在頭結點的話那么直接插進去即可
以上兩個操作就完成了在鏈表頭插入新的結點的操作,但是記得return的是new,而不是head,因為現在的head已經發生了改變。
2.如果不是在鏈表頭,即正常插入
while(point->next != NULL)//這里跟后插法的區別,后插法是直接判斷point本身,而這里是判斷point->next
?? ?{
?? ??? ?if(point->next->data == data)//這里也一樣,后插法是判斷point->data,而這里是判斷point->next->data;
?? ??? ?{
?? ??? ??? ?new->next = point->next;//這里的算法跟后插法一樣
?? ??? ??? ?point->next = new;
?? ??? ??? ?
?? ??? ??? ?printf("Congratulations,you find the data success!\n");
?? ??? ??? ?return head;
?? ??? ?}
?? ??? ?point = point->next;
?? ?}
雖然這里的核心代碼跟上面的后插法是一樣的,但是兩者的判斷條件不一樣,后插法的判斷條件是while(point != NULL),而前插法的判斷條件是while(point->next != NULL),一定要切記這兩點
直接上代碼:
struct Test* insertBehindLinklist(struct Test *head,struct Test* new,int data)
{//前插法的做法
?? ?struct Test* point = head;
?? ?if(point->data == data)
?? ?{
?? ??? ?new->next = head;
?? ??? ?return new;
?? ?}//如果要找的那個數剛好在頭結點的話那么直接插進去即可。
?? ?
?? ?while(point->next != NULL)//這里跟后插法的區別,后插法是直接判斷point本身,而這里是判斷point->next
?? ?{
?? ??? ?if(point->next->data == data)//這里也一樣,后插法是判斷point->data,而這里是判斷point->next->data;
?? ??? ?{
?? ??? ??? ?new->next = point->next;//這里的算法跟后插法一樣
?? ??? ??? ?point->next = new;
?? ??? ??? ?
?? ??? ??? ?printf("Congratulations,you find the data success!\n");
?? ??? ??? ?return head;
?? ??? ?}
?? ??? ?point = point->next;
?? ?}
?? ?printf("Sorry, you find error!\n");
?? ?
?? ?return head;
}
切記一點:
如果鏈表頭發生了改變的話,一定要返回新的鏈表頭,如果鏈表頭沒發生變化的話,不用返回鏈表頭,在main里也能檢測到鏈表結點的變化(因為指針本身就是一個地址)
原文鏈接:https://blog.csdn.net/nefu_lmy/article/details/119108089
相關推薦
- 2022-06-18 Elasticsearch之文檔批量操作示例_其它綜合
- 2022-05-21 Python中with上下文管理協議的作用及用法_python
- 2022-09-07 Golang使用CGO與Plugin技術運行加載C動態庫_Golang
- 2022-04-25 Pycharm下載pyinstaller報錯:You?should?consider?upgradi
- 2022-11-17 Android使用元數據實現配置信息的傳遞方法詳細介紹_Android
- 2022-09-21 Flutter自定義底部導航欄的方法_Android
- 2022-08-26 C++超集C++/CLI模塊的基本語法_C 語言
- 2023-02-09 Rust?所有權機制原理深入剖析_Rust語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支