網站首頁 編程語言 正文
單鏈表刪除指定結點
在單鏈表中刪除指定的結點。這里單鏈表是用尾插法建立的,因為尾插法輸出的順序與輸入的順序是相同的。
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}no;
int main()
{
no *head,*tail,*p,*r,*q;
head=new no;
head->next=NULL;
tail=head;
int n,k;
printf("一共要輸入的數: ");
scanf("%d\n",&n);
//尾插法建立單鏈表
for(int i=0;i<n;i++)
{
cin>>k;
p=new no;
p->data=k;
p->next=NULL;
tail->next=p;
tail=p;
}
//接下來是刪除操作
int m;
printf("輸入要刪除的數: ");
scanf("%d",&m);
p=head;//讓p指針從頭結點開始遍歷,要注意的是,頭結點是沒有數值的哦!
while(p->data!=m&&p->next!=NULL)//循環查找要刪除的結點
{
r=p;
p=p->next;//把p的下一個結點給p,所以p就不是原來的p了,原來的p變成了r
if(p->data==m)//因為頭結點沒有數值,所以一開始就讓p=p->next是對的
{
r->next=p->next;//將要刪除結點的前一個結點指向它的下一個結點(原本是要指它的,現在指向它的下一個結點了)(r是要刪除結點的前一個結點)
delete(p);
}//注意,這里的p->next已經和第38行的p->next不一樣了,它是38行的下一個結點了
}
q=head->next;
for(int i=0;i<n-1;i++)
{
printf("%d ",q->data);
q=q->next;
}
return 0;
}
測試一:一共要輸入的數:5
? ? ? ? ? ? ? 1 2 3 4 5
? ? ? ? ? ? ? 要刪除的數:5
? ? ? ? ? ? ? 輸出:1 2 3 4
測試二:一共要輸入的數:5
? ? ? ? ? ? ? 1 2 3 4 5
? ? ? ? ? ? ? 要刪除的數:1
? ? ? ? ? ? ? 輸出: 2 3 4 5
測試三:一共要輸入的數:5
? ? ? ? ? ? ? 1 2 3 4 5
? ? ? ? ? ? ? 要刪除的數:2
? ? ? ? ? ? ? 輸出:1 3 4 5
鏈表的刪除結點(各種方法)
先建立鏈表(代碼在最后)
鏈表中刪除第i個結點
int main()
{
? ? int i;
? ? Node *p,*head,*k;
? ? head=setlink();
? ? scanf("%d",&i);
? ? int v=1;
? ? for(p=head->next;p!=NULL;k=p,p=p->next) ?
? ? {
?? ??? ?if(v==i)break;
?? ??? ?else{
?? ??? ??? ?v++;
?? ??? ?}
?? ??? ? ??
? ? }
?? ??? ?k->next=p->next;
?? ?
?? ? delete(p);
?? ? ? ?for(p=head->next;p!=NULL;p=p->next)
?? ??? ? ? printf("%d ",p->id);
?? ? return 0 ;
}
刪除與鏈表中與a相同的結點
int main()
{
?? ?int a;
?? ?Node *p,*q,*heada,*k;
?? ?heada=setlink();
?? ?scanf("%d",&a);
?? ?for(p=heada->next;p!=NULL;k=p,p=p->next) ?
?? ?{
?? ??? ?if(p->id==a)
?? ??? ?{
?? ??? ??? ?q=p;
?? ??? ??? ?k->next=p->next;
?? ??? ??? ?p=k->next;
?? ??? ??? ?delete(q);
?? ??? ?}
?
?? ?}
?? ?for(p=heada->next;p!=NULL;p=p->next)
?? ??? ?printf("%d ",p->id);
?? ?return 0 ;
}
刪除鏈表中重復元素
int main()
{
?? ?Node *p,*q,*heada,*k,*ptr;
?? ?heada=setlink();
?? ?for(p=heada->next;p!=NULL;p=p->next) ?
?? ?{
?? ??? ?k=p;
?? ??? ?for(q=p->next;q!=NULL;k=q,q=q->next)
?? ??? ?{
?? ??? ?if(p->id==q->id)
?? ??? ?{
?? ??? ??? ?ptr=q;
?? ??? ??? ?k->next=q->next;
?? ??? ??? ?q=k;
?? ??? ??? ?free(ptr);
?? ??? ?}
?? ??? ?}
?? ?}
?? ?for(p=heada->next;p!=NULL;p=p->next)
?? ??? ?printf("%d ",p->id);
?? ?return 0 ;
}
原文鏈接:https://blog.csdn.net/nefu_lmy/article/details/119081408
相關推薦
- 2022-08-07 C++從文件中提取英文單詞的實現方法_C 語言
- 2022-11-13 kvm?透傳顯卡至win10虛擬機的方法_Kvm
- 2022-06-12 python數據處理詳情_python
- 2022-11-20 spring?boot集成redis基礎入門實例詳解_Redis
- 2023-01-12 python如何批量讀取.mat文件并保存成.npy_python
- 2022-03-21 詳解c++優先隊列priority_queue的用法_C 語言
- 2022-10-11 tidb-gc長時間不變動引起的慢查詢
- 2022-07-12 如何利用python實現kmeans聚類_python
- 最近更新
-
- 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同步修改后的遠程分支