-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathE.cpp
113 lines (111 loc) · 2.63 KB
/
E.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <fstream>
#include <string>
#include <list>
#include <stdlib.h>
#include <vector>
std::ofstream out("formation.out");
std::ifstream in("formation.in");
struct queue_cell
{
queue_cell() : next(NULL),prev(NULL),data(1) {};
int data;
queue_cell *next;
queue_cell *prev;
};
queue_cell *first = new queue_cell();
struct queue
{
queue() :head(first){};
queue_cell *head;
void left(int i, int j, std::vector <queue_cell*>& path){
queue_cell *srch = path[j];
queue_cell *p = new queue_cell();
p->data = i;
if (srch->prev){
srch->prev->next = p;
p->prev = srch->prev;
p->next = srch;
srch->prev = p;
}
else{
srch->prev = p;
p->next = srch;
this->head = p;
}
path[i] = p;
}
void right(int i, int j, std::vector <queue_cell*>& path){
queue_cell *srch = path[j];
queue_cell *p = new queue_cell();
p->data = i;
if (srch->next){
srch->next->prev = p;
p->prev = srch;
p->next = srch->next;
srch->next = p;
}
else{
srch->next = p;
p->prev = srch;
}
path[i] = p;
}
void leave(int i, std::vector <queue_cell*>&path){
queue_cell *p = path[i];
if (p == this->head){
this->head = p->next;
this->head->prev = NULL;
}
else if (!p->next){
p->prev->next = NULL;
}
else{
p->next->prev = p->prev;
p->prev->next = p->next;
}
}
void name(int i, std::vector <queue_cell*>& path){
queue_cell *p = path[i];
if (p->prev) out << p->prev->data;
else out << 0;
out << " ";
if (p->next) out << p->next->data;
else out << 0;
out << "\n";
}
};
int main()
{
queue a;
int n,m;
in >> n >> m;
std::string s;
std::vector <queue_cell*> path(n+1);
path[1] = first;
for (int i = 0; i < m; i++){
in >> s;
if (s == "left"){
int x,y;
in >> x >> y;
a.left(x,y,path);
}
if (s=="right"){
int x,y;
in >> x >> y;
a.right(x,y,path);
}
if (s=="name"){
int x;
in >> x;
a.name(x,path);
}
if (s=="leave"){
int x;
in >> x;
a.leave(x,path);
}
}
in.close();
out.close();
return 0;
}