题解 | #【模板】链表#
【模板】链表
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; } } }