-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathF.cpp
121 lines (116 loc) · 2.54 KB
/
F.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
114
115
116
117
118
119
120
121
#include <fstream>
#include <string>
#include <list>
#include <stdlib.h>
#include <vector>
std::ofstream out("hospital.out");
std::ifstream in("hospital.in");
struct queue_cell
{
queue_cell() : next(NULL),prev(NULL),data(1) {};
int data;
queue_cell *next;
queue_cell *prev;
};
queue_cell *middle = new queue_cell;
struct queue
{
queue() :head(NULL), tail(NULL), size(0) {};
queue_cell *head;
queue_cell *tail;
int size;
void push(int x){
this->size += 1;
queue_cell *p = new queue_cell;
p->next = tail;
if (this->tail)
{
this->tail->prev = p;
}
this->tail = p;
p->data = x;
if (this->head == NULL)
{
this->head = p;
middle = p;
}
if (middle->prev){
if(this->size % 2 != 0){
middle = middle->prev;
}
}
}
int pop(){
int ans = this->head->data;
if (this->head->prev)
{
this->head->prev->next = NULL;
queue_cell *p = this->head;
this->head = this->head->prev;
delete p;
this->size -= 1;
if (size > 0 && middle->prev){
if(this->size % 2 != 0){
middle = middle->prev;
}
}
return ans;
}
else
{
this->head = NULL;
this->size = 0;
middle = NULL;
return ans;
}
}
void push_mid(int x){
if (this->size <= 1){
this->push(x);
return;
}
queue_cell *p = new queue_cell();
p->data = x;
this->size++;
if (middle->prev){
middle->prev->next = p;
p->next = middle;
p->prev = middle->prev;
middle->prev = p;
}
else{
middle->prev = p;
p->next = middle;
}
if (this->size % 2 != 0)
{
middle = p;
}
}
};
int main()
{
queue a;
std::string s;
int n;
in >> n;
for (int i = 0; i < n; i++){
in >> s;
if (s == "+"){
int x;
in >> x;
a.push(x);
}
else if (s == "*"){
int x;
in >> x;
a.push_mid(x);
}
else{
out << a.pop() << "\n";
}
}
in.close();
out.close();
return 0;
}