题解 | #【模板】链表#
【模板】链表
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")
没啥好说的
查看17道真题和解析
