题解 | #【模板】链表#

【模板】链表

https://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f

#include <iostream>
using namespace std;
typedef struct node{
    int data;
    struct node *next;
}node;
node *L = NULL;
void ins(int x, int y){
    node *p = L->next, *pre = L;
    node *t = new node;
    t->data = y;
    while(p){
        if(p->data == x){
            t->next = p;
            pre->next = t;
            return;
        }
        pre = p;
        p = p->next;
    }
    pre->next = t;
    t->next = NULL;
}
void del(int x){
    node *p = L->next, *pre = L;
    while(p){
        if(p->data == x){
            pre->next = p->next;
            delete p;
            return;
        }
        pre = p;
        p = p->next;
    }
}
int main() {
	L = new node;
	L->data = 0;
	L->next = NULL;
    ios::sync_with_stdio(false);//取消同步
    int n;
    cin>>n;
    for(int i=0; i<n; i++){
        string ope;
        int x, y;
 		cin>>ope;
        if(ope=="insert"){
        	cin>>x>>y;
            ins(x, y);
        }
        else{
			cin>>x;
            del(x);
        }
    }
    node *p = L->next;
    if(!p)
        cout<<"NULL";
    else{
        while(p){
            cout<<p->data<<" ";
            p = p->next;
        }
    }
}

全部评论

相关推荐

03-29 19:11
门头沟学院 Java
wyp_davis:是可以这样的,不过只要交钱就是假的
点赞 评论 收藏
分享
HaxyBT:那我提前下班总可以了吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务