社区讨论
链表30分求调
B3631单向链表参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @m685n1aq
- 此快照首次捕获于
- 2025/01/23 01:05 去年
- 此快照最后确认于
- 2025/11/04 10:59 4 个月前
CPP
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct Node{
int data;
Node *next;
};
Node *head,*p;
int n,x,y,k;
void Add(int x,int y){
p=head->next;
while(p!=NULL){
if(p->data==x){
Node *z;
z=new Node;
z->data=y;
z->next=p->next;
p->next=z;
return;
}
p=p->next;
}
}
void Question(int x){
p=head->next;
if(p->next==NULL){
printf("0\n");
}
while(p!=NULL){
if(p->data==x){
printf("%d\n",p->next->data);
return;
}
p=p->next;
}
}
void Delete(int x){
p=head->next;
while(p!=NULL){
if(p->data==x){
p->next=p->next->next;
return;
}
p=p->next;
}
}
int main(){
scanf("%d",&n);
head=new Node;
p=new Node;
head->next=p;
p->data=1;
p->next=NULL;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&k,&x);
switch(k){
case 1:
scanf("%d",&y);
Add(x,y);
continue;
case 2:
Question(x);
continue;
case 3:
Delete(x);
continue;
}
}
return 0;
}
回复
共 3 条回复,欢迎继续交流。
正在加载回复...