链表方法
2024-12-15 14:13:14
发布于:江苏
17阅读
0回复
0点赞
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
};
int main(){
node *head,*r,*p;
head=new node;
head->next=NULL;
r=head;
int n,t;
cin>>n;
for(int i=1;i<=n;i++){
cin>>t;
p=new node;
p->data=t;
p->next=NULL;
r->next=p;
r=p;
}
int m,q;
cin>>m>>q;
p=head;
while(p->next!=NULL){
if(p->next->data==m){
node *t=new node;
t->data=q;
t->next=p->next->next;
p->next->next=t;
}
p=p->next;
}
p=head;
while(p->next!=NULL){
cout<<p->next->data<<' ';
p=p->next;
}
return 0;
}
这里空空如也
有帮助,赞一个