File tree 3 files changed +157
-0
lines changed
solution/0200-0299/0225.Implement Stack using Queues
3 files changed +157
-0
lines changed Original file line number Diff line number Diff line change @@ -204,6 +204,60 @@ private:
204
204
* /
205
205
```
206
206
207
+ ### **Rust**
208
+
209
+ ```rust
210
+ use std::collections::VecDeque;
211
+
212
+ struct MyStack {
213
+ /// There could only be two status at all time
214
+ /// 1. One contains N elements, the other is empty
215
+ /// 2. One contains N - 1 elements, the other contains exactly 1 element
216
+ q_1: VecDeque<i32>,
217
+ q_2: VecDeque<i32>,
218
+ // Either 1 or 2, originally begins from 1
219
+ index: i32,
220
+ }
221
+
222
+ impl MyStack {
223
+ fn new() -> Self {
224
+ Self {
225
+ q_1: VecDeque::new(),
226
+ q_2: VecDeque::new(),
227
+ index: 1,
228
+ }
229
+ }
230
+
231
+ fn move_data(&mut self) {
232
+ // Always move from q1 to q2
233
+ assert!(self.q_2.len() == 1);
234
+ while !self.q_1.is_empty() {
235
+ self.q_2.push_back(self.q_1.pop_front().unwrap());
236
+ }
237
+ let tmp = self.q_1.clone();
238
+ self.q_1 = self.q_2.clone();
239
+ self.q_2 = tmp;
240
+ }
241
+
242
+ fn push(&mut self, x: i32) {
243
+ self.q_2.push_back(x);
244
+ self.move_data();
245
+ }
246
+
247
+ fn pop(&mut self) -> i32 {
248
+ self.q_1.pop_front().unwrap()
249
+ }
250
+
251
+ fn top(&mut self) -> i32 {
252
+ *self.q_1.front().unwrap()
253
+ }
254
+
255
+ fn empty(&self) -> bool {
256
+ self.q_1.is_empty()
257
+ }
258
+ }
259
+ ```
260
+
207
261
### ** Go**
208
262
209
263
``` go
Original file line number Diff line number Diff line change @@ -180,6 +180,60 @@ private:
180
180
* /
181
181
```
182
182
183
+ ### **Rust**
184
+
185
+ ```rust
186
+ use std::collections::VecDeque;
187
+
188
+ struct MyStack {
189
+ /// There could only be two status at all time
190
+ /// 1. One contains N elements, the other is empty
191
+ /// 2. One contains N - 1 elements, the other contains exactly 1 element
192
+ q_1: VecDeque<i32>,
193
+ q_2: VecDeque<i32>,
194
+ // Either 1 or 2, originally begins from 1
195
+ index: i32,
196
+ }
197
+
198
+ impl MyStack {
199
+ fn new() -> Self {
200
+ Self {
201
+ q_1: VecDeque::new(),
202
+ q_2: VecDeque::new(),
203
+ index: 1,
204
+ }
205
+ }
206
+
207
+ fn move_data(&mut self) {
208
+ // Always move from q1 to q2
209
+ assert!(self.q_2.len() == 1);
210
+ while !self.q_1.is_empty() {
211
+ self.q_2.push_back(self.q_1.pop_front().unwrap());
212
+ }
213
+ let tmp = self.q_1.clone();
214
+ self.q_1 = self.q_2.clone();
215
+ self.q_2 = tmp;
216
+ }
217
+
218
+ fn push(&mut self, x: i32) {
219
+ self.q_2.push_back(x);
220
+ self.move_data();
221
+ }
222
+
223
+ fn pop(&mut self) -> i32 {
224
+ self.q_1.pop_front().unwrap()
225
+ }
226
+
227
+ fn top(&mut self) -> i32 {
228
+ *self.q_1.front().unwrap()
229
+ }
230
+
231
+ fn empty(&self) -> bool {
232
+ self.q_1.is_empty()
233
+ }
234
+ }
235
+ ```
236
+
183
237
### ** Go**
184
238
185
239
``` go
Original file line number Diff line number Diff line change
1
+ use std:: collections:: VecDeque ;
2
+
3
+ struct MyStack {
4
+ /// There could only be two status at all time
5
+ /// 1. One contains N elements, the other is empty
6
+ /// 2. One contains N - 1 elements, the other contains exactly 1 element
7
+ q_1 : VecDeque < i32 > ,
8
+ q_2 : VecDeque < i32 > ,
9
+ // Either 1 or 2, originally begins from 1
10
+ index : i32 ,
11
+ }
12
+
13
+ impl MyStack {
14
+ fn new ( ) -> Self {
15
+ Self {
16
+ q_1 : VecDeque :: new ( ) ,
17
+ q_2 : VecDeque :: new ( ) ,
18
+ index : 1 ,
19
+ }
20
+ }
21
+
22
+ fn move_data ( & mut self ) {
23
+ // Always move from q1 to q2
24
+ assert ! ( self . q_2. len( ) == 1 ) ;
25
+ while !self . q_1 . is_empty ( ) {
26
+ self . q_2 . push_back ( self . q_1 . pop_front ( ) . unwrap ( ) ) ;
27
+ }
28
+ let tmp = self . q_1 . clone ( ) ;
29
+ self . q_1 = self . q_2 . clone ( ) ;
30
+ self . q_2 = tmp;
31
+ }
32
+
33
+ fn push ( & mut self , x : i32 ) {
34
+ self . q_2 . push_back ( x) ;
35
+ self . move_data ( ) ;
36
+ }
37
+
38
+ fn pop ( & mut self ) -> i32 {
39
+ self . q_1 . pop_front ( ) . unwrap ( )
40
+ }
41
+
42
+ fn top ( & mut self ) -> i32 {
43
+ * self . q_1 . front ( ) . unwrap ( )
44
+ }
45
+
46
+ fn empty ( & self ) -> bool {
47
+ self . q_1 . is_empty ( )
48
+ }
49
+ }
You can’t perform that action at this time.
0 commit comments