-
Notifications
You must be signed in to change notification settings - Fork 1
/
ADAQUEUE - Ada and Queue.cc
134 lines (132 loc) · 3.22 KB
/
ADAQUEUE - Ada and Queue.cc
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <bits/stdc++.h>
using namespace std;
struct E {
int v;
E* prev;
E* next;
E() {
prev = next = NULL;
}
};
int main() {
int N;
int v;
bool reversed = false;
char q[10];
E* root1 = NULL;
E* root2 = NULL;
E* e;
E* temp;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%s", q);
if (strcmp(q, "toFront") == 0) {
scanf("%d", &v);
e = new E;
e->v = v;
if (root1 == NULL)
root1 = root2 = e;
else {
if (!reversed) {
temp = root1;
root1 = e;
root1->next = temp;
temp->prev = root1;
}//if
else {
temp = root2;
root2 = e;
temp->next = root2;
root2->prev = temp;
}//else
}//else
e = root1;
}//if
else if (strcmp(q, "push_back") == 0) {
scanf("%d", &v);
e = new E;
e->v = v;
if (root1 == NULL)
root1 = root2 = e;
else {
if (!reversed) {
temp = root2;
root2 = e;
temp->next = root2;
root2->prev = temp;
}//if
else {
temp = root1;
root1 = e;
root1->next = temp;
temp->prev = root1;
}//else
}//else
e = root1;
}//else if
else if (strcmp(q, "front") == 0) {
if (!reversed) {
if (!root1)
printf("No job for Ada?\n");
else {
printf("%d\n", root1->v);
temp = root1;
root1 = root1->next;
if (!root1)
root2 = NULL;
else
root1->prev = NULL;
delete temp;
}//else
}//if
else {
if (!root2)
printf("No job for Ada?\n");
else {
printf("%d\n", root2->v);
temp = root2;
root2 = root2->prev;
if (!root2)
root1 = NULL;
else
root2->next = NULL;
delete temp;
}//else
}//else
}//else if
else if (strcmp(q, "back") == 0) {
if (!reversed) {
if (!root2)
printf("No job for Ada?\n");
else {
printf("%d\n", root2->v);
temp = root2;
root2 = root2->prev;
if (!root2)
root1 = NULL;
else
root2->next = NULL;
delete temp;
}//else
}//if
else {
if (!root1)
printf("No job for Ada?\n");
else {
printf("%d\n", root1->v);
temp = root1;
root1 = root1->next;
if (!root1)
root2 = NULL;
else
root1->prev = NULL;
delete temp;
}//else
}//else
}//else if
else if (strcmp(q, "reverse") == 0) {
reversed = !reversed;
}//else if
}//for
return 0;
}