链表入门题 题解
2023-12-16 17:58:04
发布于:北京
36阅读
0回复
0点赞
#include<iostream>
using namespace std;
struct node{
int data;
node *next;
};
int main(){
int n,a,b;
node *h,*r,*p,*s;
h=new node;
r=h;
r->next=NULL;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a;
p=new node;
p->data=a;
r->next=p;
r=p;
r->next=NULL;
}
cin>>a>>b;
s=new node;
s->data=b;
p=h;
while(p->next->data!=a) p=p->next;
s->next=p->next->next;
p->next->next=s;
p=h;
while(p->next!=NULL){
cout<<p->next->data<<' ';
p=p->next;
}
return 0;
}
这里空空如也
有帮助,赞一个