| Total Complexity | 12 |
| Complexity/F | 1.5 |
| Lines of Code | 58 |
| Function Count | 8 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | class Queue1 { |
||
| 2 | // put your code here to address problems |
||
| 3 | constructor(stack1, stack2) { |
||
| 4 | this.stack1 = stack1; |
||
| 5 | this.stack2 = stack2; |
||
| 6 | } |
||
| 7 | enqueue(record) { |
||
| 8 | // - Pop out all elements from Stack1, push to Stack2 |
||
| 9 | while (this.stack1.top()) { |
||
| 10 | this.stack2.push(this.stack1.pop()); |
||
| 11 | } |
||
| 12 | // - Push new record to Stack1 |
||
| 13 | this.stack1.push(record); |
||
| 14 | // - Pop out all elements from Stack2, push back to Stack1 |
||
| 15 | while (this.stack2.top()) { |
||
| 16 | this.stack1.push(this.stack2.pop()); |
||
| 17 | } |
||
| 18 | } |
||
| 19 | dequeue() { |
||
| 20 | return this.stack1.pop(); |
||
| 21 | } |
||
| 22 | peek() { |
||
| 23 | return this.stack1.data; |
||
| 24 | } |
||
| 25 | } |
||
| 26 | |||
| 27 | class Queue2 { |
||
| 28 | // put your code here to address problems |
||
| 29 | constructor(stack1, stack2) { |
||
| 30 | this.stack1 = stack1; |
||
| 31 | this.stack2 = stack2; |
||
| 32 | } |
||
| 33 | enqueue(record) { |
||
| 34 | this.stack1.push(record); |
||
| 35 | } |
||
| 36 | dequeue() { |
||
| 37 | let result; |
||
| 38 | // - Pop out all elements from Stack1, push to Stack2 |
||
| 39 | while (this.stack1.top()) { |
||
| 40 | this.stack2.push(this.stack1.pop()); |
||
| 41 | } |
||
| 42 | // - Pop from Stack2 |
||
| 43 | result = this.stack2.pop(); |
||
| 44 | // - Pop out all remaining elements from Stack2, push back to Stack1 |
||
| 45 | while (this.stack2.top()) { |
||
| 46 | this.stack1.push(this.stack2.pop()); |
||
| 47 | } |
||
| 48 | return result; |
||
| 49 | } |
||
| 50 | peek() { |
||
| 51 | return this.stack1.data; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | module.exports = { |
||
| 56 | Queue1, |
||
| 57 | Queue2 |
||
| 58 | }; |
||
| 59 |