C++
#include <cstddef>
#include <cstdlib>
#include <iostream>
using namespace std;
using list = struct list {
int data;
list *next;
};
list *initList() {
list *L = new list;
L->next = nullptr; // 空头结点
return L;
}
list* insertList(list *L, int x, int y) {
list *p = new list;
list *q = L;
while (q->next != nullptr && q->next->data != x) {
q = q->next;
}
p->data = y;
p->next = q->next;
q->next = p;
return L;
}
list* deleteList(list *L, int x) {
list *q = L;
while (q->next != nullptr) {
if (q->next->data == x) {
list *p = q->next;
q->next = p->next;
delete p;
return L;
}
q = q->next;
}
return L;
}
bool isEmptyList(list *L) {
return L->next == nullptr;
}
void printList(list *L) {
list *q = L->next;
while (q != nullptr) {
cout << q->data << ' ';
q = q->next;
}
}
int main() {
int n;
cin >> n;
string op;
int x, y;
list *L = initList();
while (n--) {
cin >> op;
if (op=="insert") {
cin >> x >> y;
insertList(L, x, y);
}
else if (op=="delete") {
cin >> x;
deleteList(L, x);
}
}
if (isEmptyList(L)) cout << "NULL";
else printList(L);
}
// 64 位输出请用 printf("%lld")



京公网安备 11010502036488号