Skip to content

Commit 6b4d8e8

Browse files
committed
--update: aded Stack
1 parent 47210da commit 6b4d8e8

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

src/_DataStructures_/Queue/Queue.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe('Data Structure : Queue', () => {
2020
it('Should remove() an element from the queue', () => {
2121
queue.add(2);
2222
queue.add(3);
23-
queue.remove();
2423

24+
expect(queue.remove()).toEqual(2);
2525
expect(queue.data).toEqual([3]);
2626
});
2727

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const Stack = require('.');
2+
3+
describe('Data Structure : Stack', () => {
4+
it('Should be class', () => {
5+
expect(typeof Stack.prototype.constructor).toEqual('function');
6+
});
7+
8+
describe('Stack API', () => {
9+
let stack = null;
10+
11+
beforeEach(() => {
12+
stack = new Stack();
13+
});
14+
15+
it('Should add() element to a stack', () => {
16+
stack.push(5);
17+
expect(stack.data).toEqual([5]);
18+
});
19+
20+
it('Should remove() an element from the stack', () => {
21+
stack.push(2);
22+
stack.push(3);
23+
24+
expect(stack.pop()).toEqual(3);
25+
expect(stack.data).toEqual([2]);
26+
});
27+
28+
describe('peek()', () => {
29+
beforeEach(() => {
30+
stack.push(2);
31+
stack.push(5);
32+
});
33+
34+
it('Should return the elemet to be removed using peek()', () => {
35+
expect(stack.peek()).toEqual(5);
36+
});
37+
38+
it('Should not remove the element', () => {
39+
expect(stack.peek()).toEqual(5);
40+
expect(stack.pop()).toEqual(5);
41+
});
42+
});
43+
44+
it('Should maintain the FILO order of elements', () => {
45+
// first in last out
46+
stack.push(2);
47+
stack.push(1);
48+
stack.push(4);
49+
stack.push(3);
50+
51+
expect(stack.pop()).toEqual(3);
52+
expect(stack.pop()).toEqual(4);
53+
expect(stack.pop()).toEqual(1);
54+
expect(stack.pop()).toEqual(2);
55+
});
56+
});
57+
});

src/_DataStructures_/Stack/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Stack {
2+
constructor() {
3+
this.data = [];
4+
}
5+
6+
push(element) {
7+
// add element to the last
8+
this.data.push(element);
9+
}
10+
11+
pop() {
12+
return this.data.pop();
13+
}
14+
15+
peek() {
16+
return this.data[this.data.length - 1];
17+
}
18+
}
19+
20+
module.exports = Stack;

src/_Problems_/remove-duplicates/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ function removeDuplicatesUsingHashTable(str) {
99
}
1010
}
1111

12+
// eslint-disable-next-line array-callback-return
1213
Object.keys(charHash).map((char) => {
1314
result += char;
1415
});

0 commit comments

Comments
 (0)