Skip to content

Commit f7147e7

Browse files
authored
Merge pull request #1 from shadowcodex/stacks
Stacks
2 parents bd82ad6 + 92b6eb5 commit f7147e7

File tree

11 files changed

+538
-0
lines changed

11 files changed

+538
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License
2+
3+
Copyright (c) 2010-2018 Google, Inc. http://angularjs.org
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
22+

benchmark/queues.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
let { queues } = require('./../index');
2+
let ptime = require('quick-ptime');
3+
4+
5+
// setup stacks
6+
let queue = new queues.Queue();
7+
let arrayQueue = new queues.ArrayQueue();
8+
9+
// enqueue 100,000 items into the queue
10+
ptime.setTime('queue100000');
11+
for(let i = 0; i < 100000; i++) {
12+
queue.enqueue('value');
13+
}
14+
console.log("Enqueue 100,000 Queue timing:", ptime.elapsedTime('queue100000').formatted);
15+
16+
ptime.setTime('arrayqueue100000');
17+
for(let i = 0; i < 100000; i++) {
18+
arrayQueue.enqueue('value');
19+
}
20+
console.log("Enqueue 100,000 Array Queue timing:", ptime.elapsedTime('arrayqueue100000').formatted);
21+
22+
// dequeue 100,000 items into the queue
23+
ptime.setTime('dqueue100000');
24+
for(let i = 0; i < 100000; i++) {
25+
queue.dequeue();
26+
}
27+
console.log("Dequeue 100,000 Queue timing:", ptime.elapsedTime('dqueue100000').formatted);
28+
29+
ptime.setTime('darrayqueue100000');
30+
for(let i = 0; i < 100000; i++) {
31+
arrayQueue.dequeue();
32+
}
33+
console.log("Dequeue 100,000 Array Queue timing:", ptime.elapsedTime('darrayqueue100000').formatted);

benchmark/stacks.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
let { stacks } = require('./../index');
2+
let ptime = require('quick-ptime');
3+
4+
5+
// setup stacks
6+
let stack = new stacks.Stack();
7+
let arrayStack = new stacks.ArrayStack();
8+
9+
// push 100,000 items onto the stack
10+
ptime.setTime('stack100000');
11+
for(let i = 0; i < 100000; i++) {
12+
stack.push('value');
13+
}
14+
console.log("Push 100,000 Stack timing:", ptime.elapsedTime('stack100000').formatted);
15+
16+
ptime.setTime('arraystack100000');
17+
for(let i = 0; i < 100000; i++) {
18+
arrayStack.push('value');
19+
}
20+
console.log("Push 100,000 Array Stack timing:", ptime.elapsedTime('arraystack100000').formatted);
21+
22+
23+
// pop 100,000 items off the stack
24+
ptime.setTime('pstack100000');
25+
for(let i = 0; i < 100000; i++) {
26+
stack.pop()
27+
}
28+
console.log("Pop 100,000 Stack timing:", ptime.elapsedTime('pstack100000').formatted);
29+
30+
ptime.setTime('parraystack100000');
31+
for(let i = 0; i < 100000; i++) {
32+
stack.pop()
33+
}
34+
console.log("Pop Array 100,000 Stack timing:", ptime.elapsedTime('parraystack100000').formatted);

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const stacks = require('./lib/stacks');
2+
const queues = require('./lib/queues');
3+
4+
module.exports = {
5+
stacks,
6+
queues
7+
}

lib/linkedLists.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class LinkedList {
2+
constructor () {
3+
this.current = {};
4+
}
5+
6+
add(value) {
7+
8+
}
9+
10+
remove() {
11+
12+
}
13+
14+
peek() {
15+
return this.current;
16+
}
17+
}
18+
19+
class DoublyLinkedList {
20+
constructor () {
21+
22+
}
23+
}
24+
25+
module.exports = {
26+
LinkedList,
27+
DoublyLinkedList
28+
}

lib/queues.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
class Queue {
2+
constructor () {
3+
this.first = {};
4+
this.last = {};
5+
this.length = 0;
6+
}
7+
8+
enqueue(data) {
9+
let node = {value: data};
10+
if(this.length > 0) {
11+
this.last.prev = node;
12+
this.last = node;
13+
this.length++;
14+
return this.length;
15+
} else {
16+
this.first = node;
17+
this.last = node;
18+
this.length++;
19+
return this.length;
20+
}
21+
}
22+
23+
dequeue() {
24+
if(this.length === 1) {
25+
const value = this.first.value;
26+
this.first = {};
27+
this.last = {};
28+
this.length--;
29+
return value;
30+
} if(this.length > 1) {
31+
const value = this.first.value;
32+
this.first = this.first.prev;
33+
this.length--;
34+
return value;
35+
} else {
36+
return undefined;
37+
}
38+
}
39+
40+
peek() {
41+
return this.first.value;
42+
}
43+
44+
length() {
45+
return this.length;
46+
}
47+
48+
isEmpty() {
49+
return this.length === 0;
50+
}
51+
}
52+
53+
class ArrayQueue {
54+
constructor () {
55+
this.queue = [];
56+
}
57+
58+
enqueue(data) {
59+
return this.queue.unshift(data);
60+
}
61+
62+
dequeue() {
63+
return this.queue.pop();
64+
}
65+
66+
peek() {
67+
return this.queue[this.queue.length - 1];
68+
}
69+
70+
length() {
71+
return this.queue.length;
72+
}
73+
74+
isEmpty() {
75+
return this.queue.length === 0;
76+
}
77+
}
78+
79+
module.exports = {
80+
Queue,
81+
ArrayQueue
82+
}

lib/stacks.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Stack {
2+
constructor () {
3+
this.top = {};
4+
this.length = 0;
5+
}
6+
7+
push(data) {
8+
let node = {
9+
value: data,
10+
prev: this.top
11+
};
12+
this.top = node;
13+
this.length++;
14+
return this.length;
15+
}
16+
17+
pop() {
18+
if(this.length === 0) {
19+
return undefined;
20+
}
21+
22+
let value = this.top.value;
23+
this.top = this.top.prev;
24+
this.length--;
25+
return value;
26+
}
27+
28+
peek() {
29+
return this.top.value;
30+
}
31+
32+
height() {
33+
return this.length;
34+
}
35+
36+
isEmpty() {
37+
return this.length === 0;
38+
}
39+
}
40+
41+
class ArrayStack {
42+
constructor() {
43+
this.stack = [];
44+
}
45+
46+
push(data) {
47+
return this.stack.push(data);
48+
}
49+
50+
pop() {
51+
return this.stack.pop();
52+
}
53+
54+
peek() {
55+
return this.stack[this.stack.length - 1];
56+
}
57+
58+
height() {
59+
return this.stack.length;
60+
}
61+
62+
isEmpty() {
63+
return this.stack.length === 0;
64+
}
65+
}
66+
67+
68+
module.exports = {
69+
Stack,
70+
ArrayStack
71+
}

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "data-theory",
3+
"version": "0.0.1",
4+
"description": "A collection of data theory and structures in JavaScript.",
5+
"main": "index.js",
6+
"repository": "https://github.com/shadowcodex/data-theory.git",
7+
"author": "shadowcodex <[email protected]>",
8+
"license": "MIT",
9+
"private": false,
10+
"scripts": {
11+
"test": "npx mocha",
12+
"bench:stacks": "node benchmark/stacks.js",
13+
"bench:queues": "node benchmark/queues.js"
14+
},
15+
"devDependencies": {
16+
"mocha": "^5.2.0"
17+
},
18+
"dependencies": {
19+
"quick-ptime": "^1.0.0"
20+
}
21+
}

test/stacks.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var assert = require('assert');
2+
const { stacks } = require('../index');
3+
4+
describe('Stack', function() {
5+
it('should make a new stack', function() {
6+
const stack = new stacks.Stack();
7+
assert.equal(stack.height(), 0);
8+
});
9+
10+
it('should push an item on to the stack', function() {
11+
let stack = new stacks.Stack();
12+
stack.push('item');
13+
14+
assert.equal(stack.peek(), 'item');
15+
});
16+
17+
it('should pop an item off the stack', function() {
18+
let stack = new stacks.Stack();
19+
stack.push('item');
20+
let value = stack.pop();
21+
assert.equal(value, 'item');
22+
});
23+
24+
it('should push three items on and pop them off in order', function() {
25+
let stack = new stacks.Stack();
26+
stack.push('item1');
27+
stack.push('item2');
28+
stack.push('item3');
29+
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');
38+
});
39+
});
40+
41+
describe('Array Stack', function() {
42+
it('should make a new stack', function() {
43+
const stack = new stacks.ArrayStack();
44+
assert.equal(stack.height(), 0);
45+
});
46+
47+
it('should push an item on to the stack', function() {
48+
let stack = new stacks.ArrayStack();
49+
stack.push('item');
50+
51+
assert.equal(stack.peek(), 'item');
52+
});
53+
54+
it('should pop an item off the stack', function() {
55+
let stack = new stacks.ArrayStack();
56+
stack.push('item');
57+
let value = stack.pop();
58+
assert.equal(value, 'item');
59+
});
60+
61+
it('should push three items on and pop them off in order', function() {
62+
let stack = new stacks.ArrayStack();
63+
stack.push('item1');
64+
stack.push('item2');
65+
stack.push('item3');
66+
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');
75+
});
76+
});

0 commit comments

Comments
 (0)