链表
#include<bits/stdc++.h>
using namespace std;
struct node {
int val;
node* next;
};
typedef node* LinkList;
int n, x, y;
node* ad;
string s;
LinkList initLinkList() {
LinkList head = (LinkList)malloc(sizeof(node));
if (head != NULL) {
head->next = NULL;
} else {
cout << "init fail" << endl;
}
return head;
}
void displyLink(LinkList head) {
if (head == NULL || head->next == NULL) {
cout << "NULL" << endl;
return;
} else {
node* cur = head->next;
while (cur != NULL) {
cout << cur->val << " ";
cur = cur->next;
}
cout << endl;
}
}
void insertLink(LinkList head, int x, int y) {
if (head == NULL) return;
node* cur = head->next, *pre = head;
ad = (node*)malloc(sizeof(node));
ad->val = y;
ad->next = NULL;
while (cur != NULL) {
if (cur->val == x) {
ad->next = cur;
pre->next = ad;
break;
}
pre = pre->next;
cur = cur->next;
}
if (cur == NULL) {
pre->next = ad;
}
}
void deleteLink(LinkList head, int x) {
if (head == NULL || head->next == NULL) return;
node* cur = head->next, *pre = head;
while (cur != NULL) {
if (cur->val == x) {
node* t = cur->next;
pre->next = t;
free(cur);
break;
}
pre = pre->next;
cur = cur->next;
}
}
int main() {
cin >> n;
LinkList head = initLinkList();
while (n--) {
cin >> s;
if (s == "insert") {
cin >> x >> y;
insertLink(head, x, y);
} else if (s == "delete") {
cin >> x;
deleteLink(head, x);
}
}
displyLink(head);
return 0;
}