题解 | #【模板】链表#

【模板】链表

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

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct Lnode {
    int data;
    struct Lnode* next;
};


int main() {
    Lnode* head = new Lnode;
    head->next = nullptr;
    int op_num;
    cin >> op_num;
    while (op_num) {
        op_num--;
        string order;
        Lnode* h = head;
        cin >> order;
        if (order == "insert") {
            int a, b;
            cin >> a >> b;
            Lnode* temp = new Lnode;
            temp->data = b;
            temp->next = nullptr;
            while (h->next && h->next->data != a) {
                h = h->next;
            }
            if (!h->next) {
                h->next = temp;
            } else {
                Lnode* rear = h->next; //值为a的结点指针
                h->next = temp;
                temp->next = rear;
            }
        } else {
            int a;
            cin >> a;
            while (h->next && h->next->data != a) {
                h = h->next;
            }
            if (!h->next) {
                continue;
            } else {
                Lnode* rear = h->next; //值为a的结点指针
                Lnode* temp = rear->next;
                h->next = temp;
                delete rear;
            }
        }
    }
    if(!head->next){cout<<"NULL"<<endl;}
    else{    
        while(head->next){
            cout<<head->next->data<<' ';
            head = head->next;
        }
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

没啥好说的

全部评论

相关推荐

06-17 21:57
门头沟学院 Java
白友:噗嗤,我发现有些人事就爱发这些,明明已读不回就行了,就是要恶心人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务