Skip to content

Commit 93bfaff

Browse files
committed
fixed array stack to be both bounded and unbounded
1 parent 75a072e commit 93bfaff

File tree

3 files changed

+158
-60
lines changed

3 files changed

+158
-60
lines changed

benchmark/stacks.js

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,45 @@ let ptime = require('quick-ptime');
33

44

55
// setup stacks
6-
let stack = new stacks.Stack();
7-
let arrayStack = new stacks.ArrayStack();
6+
let linkedStack = new stacks.LinkedStack();
7+
let uArrayStack = new stacks.UArrayStack();
8+
let bArrayStack = new stacks.BArrayStack(100000);
89

910
// push 100,000 items onto the stack
1011
ptime.setTime('stack100000');
1112
for(let i = 0; i < 100000; i++) {
12-
stack.push('value');
13+
linkedStack.push('value');
1314
}
14-
console.log("Push 100,000 Stack timing:", ptime.elapsedTime('stack100000').formatted);
15+
console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);
1516

16-
ptime.setTime('arraystack100000');
17+
ptime.setTime('uarraystack100000');
1718
for(let i = 0; i < 100000; i++) {
18-
arrayStack.push('value');
19+
uArrayStack.push('value');
1920
}
20-
console.log("Push 100,000 Array Stack timing:", ptime.elapsedTime('arraystack100000').formatted);
21+
console.log("Push 100,000 Unbounded Array Stack timing:", ptime.elapsedTime('uarraystack100000').formatted);
22+
23+
ptime.setTime('barraystack100000');
24+
for(let i = 0; i < 100000; i++) {
25+
bArrayStack.push('value');
26+
}
27+
console.log("Push 100,000 Bounded Array Stack timing:", ptime.elapsedTime('barraystack100000').formatted);
2128

2229

2330
// pop 100,000 items off the stack
2431
ptime.setTime('pstack100000');
2532
for(let i = 0; i < 100000; i++) {
26-
stack.pop()
33+
linkedStack.pop()
2734
}
2835
console.log("Pop 100,000 Stack timing:", ptime.elapsedTime('pstack100000').formatted);
2936

30-
ptime.setTime('parraystack100000');
37+
ptime.setTime('puarraystack100000');
38+
for(let i = 0; i < 100000; i++) {
39+
uArrayStack.pop()
40+
}
41+
console.log("Pop 100,000 Unbounded Array Stack timing:", ptime.elapsedTime('puarraystack100000').formatted);
42+
43+
ptime.setTime('pbarraystack100000');
3144
for(let i = 0; i < 100000; i++) {
32-
stack.pop()
45+
bArrayStack.pop()
3346
}
34-
console.log("Pop 100,000 Array Stack timing:", ptime.elapsedTime('parraystack100000').formatted);
47+
console.log("Pop 100,000 Bounded Array Stack timing:", ptime.elapsedTime('pbarraystack100000').formatted);

lib/stacks.js

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
class Stack {
1+
class LinkedStack {
22
constructor () {
3-
this.top = {};
3+
this.topNode = {};
44
this.length = 0;
55
}
66

77
push(data) {
88
let node = {
99
value: data,
10-
prev: this.top
10+
prev: this.topNode
1111
};
12-
this.top = node;
12+
this.topNode = node;
1313
this.length++;
1414
return this.length;
1515
}
@@ -19,14 +19,12 @@ class Stack {
1919
return undefined;
2020
}
2121

22-
let value = this.top.value;
23-
this.top = this.top.prev;
24-
this.length--;
25-
return value;
22+
this.topNode = this.topNode.prev;
23+
this.length--;
2624
}
2725

28-
peek() {
29-
return this.top.value;
26+
top() {
27+
return this.topNode.value;
3028
}
3129

3230
height() {
@@ -38,7 +36,7 @@ class Stack {
3836
}
3937
}
4038

41-
class ArrayStack {
39+
class UArrayStack {
4240
constructor() {
4341
this.stack = [];
4442
}
@@ -48,10 +46,10 @@ class ArrayStack {
4846
}
4947

5048
pop() {
51-
return this.stack.pop();
49+
this.stack.pop();
5250
}
5351

54-
peek() {
52+
top() {
5553
return this.stack[this.stack.length - 1];
5654
}
5755

@@ -64,8 +62,60 @@ class ArrayStack {
6462
}
6563
}
6664

65+
class BArrayStack {
66+
constructor(size) {
67+
const DEFAULT_SIZE = 100;
68+
let use = !!size ? size : DEFAULT_SIZE;
69+
this.stack = [];
70+
for(let i = 0; i < use; i++) {
71+
this.stack[i] = null;
72+
}
73+
this.topIndex = -1;
74+
}
75+
76+
height() {
77+
return this.topIndex + 1;
78+
}
79+
80+
isEmpty() {
81+
return this.topIndex === -1 ? true : false;
82+
}
83+
84+
isFull() {
85+
return this.topIndex === this.stack.length - 1 ? true : false;
86+
}
87+
88+
push(item) {
89+
if(!this.isFull()) {
90+
this.topIndex++;
91+
this.stack[this.topIndex] = item;
92+
} else {
93+
throw new Error('Stack too deep');
94+
}
95+
}
96+
97+
pop() {
98+
if(!this.isEmpty()) {
99+
this.stack[this.topIndex] = null;
100+
this.topIndex--;
101+
} else {
102+
throw new Error('Stack already empty');
103+
}
104+
}
105+
106+
top() {
107+
if(!this.isEmpty()) {
108+
return this.stack[this.topIndex];
109+
} else {
110+
return undefined;
111+
}
112+
}
113+
114+
}
115+
67116

68117
module.exports = {
69-
Stack,
70-
ArrayStack
118+
LinkedStack,
119+
UArrayStack,
120+
BArrayStack
71121
}

test/stacks.js

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,111 @@
11
var assert = require('assert');
22
const { stacks } = require('../index');
33

4-
describe('Stack', function() {
4+
describe('LinkedStack', function() {
55
it('should make a new stack', function() {
6-
const stack = new stacks.Stack();
6+
const stack = new stacks.LinkedStack();
77
assert.equal(stack.height(), 0);
88
});
99

1010
it('should push an item on to the stack', function() {
11-
let stack = new stacks.Stack();
11+
let stack = new stacks.LinkedStack();
12+
stack.push('item');
13+
14+
assert.equal(stack.top(), 'item');
15+
});
16+
17+
it('should pop an item off the stack', function() {
18+
let stack = new stacks.LinkedStack();
19+
stack.push('item');
20+
stack.pop();
21+
22+
assert.equal(stack.top(), undefined);
23+
});
24+
25+
it('should push three items on and pop tone off leaving 2 items', function() {
26+
let stack = new stacks.LinkedStack();
27+
stack.push('item1');
28+
stack.push('item2');
29+
stack.push('item3');
30+
31+
stack.pop();
32+
assert.equal(stack.height(), 2);
33+
assert.equal(stack.top(), 'item2');
34+
});
35+
});
36+
37+
38+
39+
40+
41+
42+
describe('Unbounded Array Stack', function() {
43+
it('should make a new stack', function() {
44+
const stack = new stacks.UArrayStack();
45+
assert.equal(stack.height(), 0);
46+
});
47+
48+
it('should push an item on to the stack', function() {
49+
let stack = new stacks.UArrayStack();
1250
stack.push('item');
1351

14-
assert.equal(stack.peek(), 'item');
52+
assert.equal(stack.top(), 'item');
1553
});
1654

1755
it('should pop an item off the stack', function() {
18-
let stack = new stacks.Stack();
56+
let stack = new stacks.UArrayStack();
1957
stack.push('item');
20-
let value = stack.pop();
21-
assert.equal(value, 'item');
58+
stack.pop();
59+
60+
assert.equal(stack.top(), undefined);
2261
});
2362

24-
it('should push three items on and pop them off in order', function() {
25-
let stack = new stacks.Stack();
63+
it('should push three items on and pop tone off leaving 2 items', function() {
64+
let stack = new stacks.UArrayStack();
2665
stack.push('item1');
2766
stack.push('item2');
2867
stack.push('item3');
2968

30-
let value = stack.pop();
31-
assert.equal(value, 'item3');
32-
33-
value = stack.pop();
34-
assert.equal(value, 'item2');
35-
36-
value = stack.pop();
37-
assert.equal(value, 'item1');
69+
stack.pop();
70+
assert.equal(stack.height(), 2);
71+
assert.equal(stack.top(), 'item2');
3872
});
3973
});
4074

41-
describe('Array Stack', function() {
75+
76+
77+
78+
79+
80+
describe('Bounded Array Stack', function() {
4281
it('should make a new stack', function() {
43-
const stack = new stacks.ArrayStack();
82+
const stack = new stacks.BArrayStack();
4483
assert.equal(stack.height(), 0);
4584
});
4685

4786
it('should push an item on to the stack', function() {
48-
let stack = new stacks.ArrayStack();
87+
let stack = new stacks.BArrayStack();
4988
stack.push('item');
5089

51-
assert.equal(stack.peek(), 'item');
90+
assert.equal(stack.top(), 'item');
5291
});
5392

5493
it('should pop an item off the stack', function() {
55-
let stack = new stacks.ArrayStack();
94+
let stack = new stacks.BArrayStack();
5695
stack.push('item');
57-
let value = stack.pop();
58-
assert.equal(value, 'item');
96+
stack.pop();
97+
98+
assert.equal(stack.top(), undefined);
5999
});
60100

61-
it('should push three items on and pop them off in order', function() {
62-
let stack = new stacks.ArrayStack();
101+
it('should push three items on and pop tone off leaving 2 items', function() {
102+
let stack = new stacks.BArrayStack();
63103
stack.push('item1');
64104
stack.push('item2');
65105
stack.push('item3');
66106

67-
let value = stack.pop();
68-
assert.equal(value, 'item3');
69-
70-
value = stack.pop();
71-
assert.equal(value, 'item2');
72-
73-
value = stack.pop();
74-
assert.equal(value, 'item1');
107+
stack.pop();
108+
assert.equal(stack.height(), 2);
109+
assert.equal(stack.top(), 'item2');
75110
});
76111
});

0 commit comments

Comments
 (0)