社区讨论

https://www.luogu.com.cn/problem/B3631求助

灌水区参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@m0hr3kc8
此快照首次捕获于
2024/08/31 14:17
2 年前
此快照最后确认于
2025/11/04 21:57
4 个月前
查看原帖
CPP
#include<bits/stdc++.h>
using namespace std;
typedef struct LNode{
	int data;
	struct LNode * next;
}LNode;
LNode *phead=NULL;
LNode* findNode(int x,LNode *p){
    LNode *t=phead;
    while(t!=NULL&&t->data!=x){
        t=t->next;
    }
    return t;
}
void insert(int x,int y){
	if(phead==NULL) return;
    LNode *p=findNode(x,phead);
    LNode *pTemp=(LNode *)malloc(sizeof(LNode));
    pTemp->next=p->next;
    pTemp->data=y;
    p->next=pTemp;
}
void detele(int x){//delete是关键字
    if(phead==NULL) return;
    LNode *p=findNode(x,phead);
    if(p==NULL||p->next==NULL) return;
    LNode *q=p->next;
    p->next=q->next;
    free(q);
    return;
}
void search(int x){
    if(phead==NULL) return;
    LNode *p=findNode(x,phead);
    if(p->next==NULL) cout << 0 << endl;
    else cout << p->next->data << endl;
}
int main(){
    int k;
    cin >> k;
    phead = (LNode*)malloc(sizeof(LNode));
    phead->data = 1;
    phead->next = NULL;
    while(k--){
        int opt;
        cin >> opt;
        if(opt==1){
            int x,y;
            cin >> x >> y;
            insert(x,y);
        }else if(opt==2){
            int x;
            cin >> x;
            search(x);
        }else{
            int x;
            cin >> x;
            detele(x);
        }
    }
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...