|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | class TwoStacks {
|
9 |
| - constructor() { |
| 9 | + constructor(capacity) { |
10 | 10 | this.data = [];
|
11 |
| - this.top1 = null; |
12 |
| - this.top2 = null; |
13 |
| - this.stack1Count = 0; |
14 |
| - this.stack2Count = 0; |
| 11 | + this.capacity = capacity; |
| 12 | + this.top1 = -1; |
| 13 | + this.top2 = capacity; |
15 | 14 | }
|
16 | 15 |
|
17 | 16 | push1(value) {
|
18 |
| - if (!this.data.length) { |
19 |
| - this.data.push(value); |
20 |
| - this.top1 = value; |
21 |
| - this.stack1Count += 1; |
22 |
| - } else { |
23 |
| - const arr = []; |
24 |
| - for (let i = 0; i < this.stack1Count; i += 1) { |
25 |
| - arr.push(this.data[i]); |
26 |
| - } |
27 |
| - arr.push(value); |
28 |
| - for (let i = this.stack1Count; i < this.data.length; i += 1) { |
29 |
| - arr.push(this.data[i]); |
30 |
| - } |
31 |
| - this.data = arr; |
32 |
| - this.top1 = value; |
33 |
| - this.stack1Count += 1; |
| 17 | + if (this.top1 === -1 || this.top1 < this.top2 - 1) { |
| 18 | + this.top1 += 1; |
| 19 | + this.data[this.top1] = value; |
| 20 | + return; |
34 | 21 | }
|
| 22 | + throw new Error('Overflow'); |
35 | 23 | }
|
36 | 24 |
|
37 | 25 | push2(value) {
|
38 |
| - if (!this.data.length) { |
39 |
| - this.data.push(value); |
40 |
| - this.top2 = value; |
41 |
| - this.stack2Count += 1; |
42 |
| - } else { |
43 |
| - const arr = []; |
44 |
| - for (let i = 0; i < this.stack1Count; i += 1) { |
45 |
| - arr.push(this.data[i]); |
46 |
| - } |
47 |
| - arr.push(value); |
48 |
| - |
49 |
| - for (let i = this.stack1Count; i < this.data.length; i += 1) { |
50 |
| - arr.push(this.data[i]); |
51 |
| - } |
52 |
| - this.data = arr; |
53 |
| - this.top2 = value; |
54 |
| - this.stack2Count += 1; |
| 26 | + if (this.top2 <= this.capacity && this.top2 > this.top1) { |
| 27 | + this.data[this.top2] = value; |
| 28 | + this.top2 -= 1; |
| 29 | + return; |
55 | 30 | }
|
| 31 | + throw new Error('Overflow'); |
56 | 32 | }
|
57 | 33 |
|
58 |
| - pop1() { |
59 |
| - if (!this.top1) { |
60 |
| - return null; |
61 |
| - } |
62 |
| - |
63 |
| - const indexOfTop1 = this.data.indexOf(this.top1); |
64 |
| - const arr = []; |
| 34 | + pop1() {} |
65 | 35 |
|
66 |
| - delete this.data[indexOfTop1]; |
67 |
| - |
68 |
| - this.data.forEach(el => arr.push(el)); |
69 |
| - |
70 |
| - const oldTop = this.top1; |
71 |
| - this.top1 = this.data[indexOfTop1 - 1]; |
72 |
| - this.data = arr; |
73 |
| - return oldTop; |
74 |
| - } |
75 |
| - |
76 |
| - pop2() { |
77 |
| - return this.data.pop(); |
78 |
| - } |
| 36 | + pop2() {} |
79 | 37 | }
|
80 | 38 |
|
81 | 39 | module.exports = TwoStacks;
|
82 | 40 |
|
83 | 41 | /** Test cases */
|
84 | 42 |
|
85 |
| -/* |
86 |
| -
|
87 |
| -const s = new TwoStacks(); |
| 43 | +const s = new TwoStacks(3); |
88 | 44 |
|
| 45 | +s.push1('a'); |
89 | 46 | console.log(s.data);
|
90 | 47 |
|
91 |
| -s.push1(5); |
92 |
| -s.push1(4); |
| 48 | +s.push2('a2'); |
93 | 49 | console.log(s.data);
|
94 | 50 |
|
95 |
| -
|
96 |
| -s.push2(2) |
97 |
| -s.push2(1); |
| 51 | +s.push1('b'); |
98 | 52 | console.log(s.data);
|
99 | 53 |
|
100 |
| -s.push1(14); |
| 54 | +s.push2('b2'); |
101 | 55 | console.log(s.data);
|
102 |
| -console.log(s.top1); |
103 | 56 |
|
104 |
| -console.log(s.pop1()) |
| 57 | +s.push2('b3'); |
105 | 58 | console.log(s.data);
|
106 |
| -
|
107 |
| -console.log(s.pop1()) |
108 |
| -console.log(s.data); |
109 |
| -
|
110 |
| -console.log(s.pop2()) |
111 |
| -console.log(s.data); |
112 |
| -
|
113 |
| -console.log(s.pop2()) |
114 |
| -console.log(s.data); |
115 |
| -
|
116 |
| -console.log(s.pop2()) |
117 |
| -console.log(s.data); |
118 |
| -
|
119 |
| -console.log(s.pop1()) |
120 |
| -console.log(s.data); |
121 |
| -
|
122 |
| -*/ |
0 commit comments