题解 | #【模板】链表#

【模板】链表

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

#include<bits/stdc++.h>

using namespace std;

int main(){
    int n,x,y;
    list<int> l1;
    cin>>n;
    string s;
    list<int>::iterator iter;

    for(int i=0;i<n;i++)
    {
        int f_insert=0;
        cin>>s;
        if(s=="insert")
        {
            cin>>x>>y;
            for(iter=l1.begin();iter!=l1.end();iter++)
            {
                if(*iter ==x)
                {
                    l1.insert(iter,y);
                    f_insert = 1;
                    break;
                }                   
            }
            if(!f_insert)
                l1.push_back(y);
        }
        else if(s=="delete")
        {
            cin>>x;
            for(iter=l1.begin();iter!=l1.end();iter++)
            {
                if(*iter ==x)
                {
                    l1.erase(iter);
                    break;
                }                   
            }
        }
    }
    if(l1.empty())
    {
        cout<<"NULL";
        return 0;
    }
    for(iter=l1.begin();iter!=l1.end();iter++)
    {
        cout<<*iter<<" ";                  
    }
    
    
    return 0;
}

全部评论
佩服
点赞 回复 分享
发布于 2022-10-22 19:11 陕西

相关推荐

牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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