Skip to content

Commit d220bf6

Browse files
committed
--update: change in repo structure and added code for Queue
1 parent 494f4c2 commit d220bf6

File tree

18 files changed

+57
-0
lines changed

18 files changed

+57
-0
lines changed
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const Queue = require('.');
2+
3+
describe('Data Structure : Queue', () => {
4+
it('Should be class', () => {
5+
expect(typeof Queue.prototype.constructor).toEqual('function');
6+
});
7+
8+
describe('Queue API', () => {
9+
let queue = null;
10+
11+
beforeEach(() => {
12+
queue = new Queue();
13+
});
14+
15+
it('Should add element to a queue', () => {
16+
queue.add(5);
17+
expect(queue.data).toEqual([5]);
18+
});
19+
20+
it('Should remove an element from the queue', () => {
21+
queue.add(2);
22+
queue.add(3);
23+
queue.remove();
24+
25+
expect(queue.data).toEqual([3]);
26+
});
27+
28+
it('Should maintain the order of elements', () => {
29+
// first in first out
30+
queue.add(2);
31+
queue.add(1);
32+
queue.add(4);
33+
queue.add(3);
34+
35+
expect(queue.remove()).toEqual(2);
36+
expect(queue.remove()).toEqual(1);
37+
expect(queue.remove()).toEqual(4);
38+
expect(queue.remove()).toEqual(3);
39+
});
40+
});
41+
});

src/_DataStructures_/Queue/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Queue {
2+
constructor() {
3+
this.data = [];
4+
}
5+
6+
add(element) {
7+
// add element to the start of the data
8+
return this.data.unshift(element);
9+
}
10+
11+
remove() {
12+
return this.data.pop();
13+
}
14+
}
15+
16+
module.exports = Queue;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)