EEOJ/OJ2/linkedList.cpp

97 lines
1.7 KiB
C++

#include<iostream>
#include<map>
using namespace std;
struct node {
int no;
struct node* pnext;
};
struct linkedList {
map<int, node*> index;
struct node head;
struct node tail;
linkedList();
void insertAfter(int no, int nextno);
int reportNext(int no);
void deleteNode(int no);
void printAll();
};
linkedList::linkedList() {
head.no = 0;
head.pnext = &tail;
tail.no = 0;
tail.pnext = nullptr;
}
void linkedList::insertAfter(int no, int nextno) {
node* next = new node;
next->no = nextno;
node* now = &head;
while (now->pnext != nullptr) {
if (now->no == no) {
next->pnext = now->pnext;
now->pnext = next;
index[nextno] = next;
return;
}
now = now->pnext;
}
}
int linkedList::reportNext(int no) {
if (no == 0) {
return head.pnext->no;
}
else {
return index[no]->pnext->no;
}
}
void linkedList::deleteNode(int no) {
node* now = &head;
node* tmpptr;
while (now->pnext != nullptr) {
if (now->pnext->no == no) {
tmpptr = now->pnext->pnext;
delete now->pnext;
now->pnext = tmpptr;
return;
}
now = now->pnext;
}
}
void linkedList::printAll() {
node* now = &head;
node* tmpptr;
while (now->pnext != nullptr) {
if (now->no != 0) {
cout << now->no << endl;
}
now = now->pnext;
}
}
int main() {
std::ios::sync_with_stdio(false);
linkedList list;
int choice;
int total;
int x;
int y;
cin >> total;
list.insertAfter(0, 1);
for (int i = 0; i < total; i++) {
cin >> choice;
if (choice == 1) {
cin >> x;
cin >> y;
list.insertAfter(x, y);
}
else if (choice == 2) {
cin >> x;
cout << list.reportNext(x) << endl;
}
else if (choice == 3) {
cin >> x;
list.deleteNode(x);
}
}
list.printAll();
}