Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e858fc

Browse files
committed
fix: maintaining rules of stack - O(1) insertion
1 parent 3694b78 commit 9e858fc

File tree

1 file changed

+22
-86
lines changed
  • src/_DataStructures_/Stack/2-stacks-using1-array

1 file changed

+22
-86
lines changed

‎src/_DataStructures_/Stack/2-stacks-using1-array/index.js‎

Lines changed: 22 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,117 +6,53 @@
66
*/
77

88
class TwoStacks {
9-
constructor() {
9+
constructor(capacity) {
1010
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;
1514
}
1615

1716
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;
3421
}
22+
throw new Error('Overflow');
3523
}
3624

3725
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;
5530
}
31+
throw new Error('Overflow');
5632
}
5733

58-
pop1() {
59-
if (!this.top1) {
60-
return null;
61-
}
62-
63-
const indexOfTop1 = this.data.indexOf(this.top1);
64-
const arr = [];
34+
pop1() {}
6535

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() {}
7937
}
8038

8139
module.exports = TwoStacks;
8240

8341
/** Test cases */
8442

85-
/*
86-
87-
const s = new TwoStacks();
43+
const s = new TwoStacks(3);
8844

45+
s.push1('a');
8946
console.log(s.data);
9047

91-
s.push1(5);
92-
s.push1(4);
48+
s.push2('a2');
9349
console.log(s.data);
9450

95-
96-
s.push2(2)
97-
s.push2(1);
51+
s.push1('b');
9852
console.log(s.data);
9953

100-
s.push1(14);
54+
s.push2('b2');
10155
console.log(s.data);
102-
console.log(s.top1);
10356

104-
console.log(s.pop1())
57+
s.push2('b3');
10558
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /