-
Notifications
You must be signed in to change notification settings - Fork 1
/
2.front-middle-back-queue.cpp
171 lines (156 loc) · 3.66 KB
/
2.front-middle-back-queue.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*************************************************************************
> File Name: 2.front-middle-back-queue.cpp
> Author: rEn Chong
> Mail: [email protected]
> Created Time: 五 11/25 19:18:36 2022
************************************************************************/
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
// 下面用链表的思想实现一个dequeue
struct Node {
public :
int val;
Node *next, *pre;
Node(int val = 0, Node *pre = nullptr, Node *next = nullptr) : val(val), next(next), pre(pre) {}
void insert_pre(Node *p) {
p->pre = this->pre;
p->next = this;
if (this->pre) this->pre->next = p;
this->pre = p;
return ;
}
void insert_next(Node *p) {
p->pre = this;
p->next = this->next;
if (this->next) this->next->pre = p;
this->next = p;
return ;
}
void delete_pre() {
if (this->pre == nullptr) return ;
Node *p = this->pre;
this->pre = p->pre;
if (p->pre) p->pre->next = this;
delete p;
return ;
}
void delete_next() {
if (this->next == nullptr) return ;
Node *p = this->next;
this->next = p->next;
if (p->next) p->next->pre = this;
delete p;
return ;
}
};
class Queue {
public :
int cnt;
Node head, tail;
Queue() : cnt(0) {
head.next = &tail;
head.pre = nullptr;
tail.next = nullptr;
tail.pre = &head;
}
void push_back(int val) {
tail.insert_pre(new Node(val));
cnt += 1;
return ;
}
void push_front(int val) {
head.insert_next(new Node(val));
cnt += 1;
return ;
}
int pop_back() {
if (is_empty()) return -1;
int ret = tail.pre->val;
tail.delete_pre();
cnt -= 1;
return ret;
}
int pop_front() {
if (is_empty()) return -1;
int ret = head.next->val;
head.delete_next();
cnt -= 1;
return ret;
}
bool is_empty() {
return head.next == &tail;
}
int size() {
return cnt;
}
int front() {
return head.next->val;
}
int back() {
return tail.pre->val;
}
};
class FrontMiddleBackQueue {
public:
Queue q1, q2;
// 设计的时候考虑q1总是比q2元素要多一个或者相等
FrontMiddleBackQueue() {}
void pushFront(int val) {
q1.push_front(val);
update();
return ;
}
void pushMiddle(int val) {
if (q1.size() > q2.size()) {
q2.push_front(q1.back());
q1.pop_back();
}
q1.push_back(val);
update();
return ;
}
void pushBack(int val) {
q2.push_back(val);
update();
return ;
}
int popFront() {
if (is_empty()) return -1;
int ret = q1.pop_front();
update();
return ret;
}
int popMiddle() {
if (is_empty()) return -1;
int ret = q1.pop_back();
update();
return ret;
}
int popBack() {
if (is_empty()) return -1;
int ret;
if (q2.is_empty()) {
ret = q1.pop_back();
} else {
ret = q2.pop_back();
}
update();
return ret;
}
bool is_empty() {
return (q1.size() == 0);
}
void update() {
if (q1.size() < q2.size()) {
q1.push_back(q2.front());
q2.pop_front();
}
if (q1.size() == q2.size() + 2) {
q2.push_front(q1.back());
q1.pop_back();
}
return ;
}
};