Skip to content

Commit 1d1bddc

Browse files
committed
feature: added initial implementation of .add() method
1 parent 791b62c commit 1d1bddc

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

round-linked-queue.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ class RoundLinkedQueue {
44
constructor(maxLength) {
55
this._maxLength = maxLength;
66
this._length = 0;
7+
this._root = 0;
8+
this._first = null;
9+
this._last = null;
710
}
811

912
get maxLength() {
@@ -13,6 +16,22 @@ class RoundLinkedQueue {
1316
get length() {
1417
return this._length;
1518
}
19+
20+
get first() {
21+
return this._first;
22+
}
23+
24+
get last() {
25+
return this._last;
26+
}
27+
28+
add(element) {
29+
this._root = element;
30+
this._first = element;
31+
this._last = element;
32+
33+
this._length += 1;
34+
}
1635
}
1736

1837
module.exports = RoundLinkedQueue;

round-linked-queue.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,21 @@ describe("Round-Queue", () => {
2121
expect(queue.length).to.equal(0);
2222
});
2323
});
24+
25+
describe("When adding elements", () => {
26+
it("Should add an element to an empty queue", () => {
27+
const queue = new RoundQueue(3);
28+
const originalLength = queue.length;
29+
const elementToAdd = 1;
30+
31+
queue.add(elementToAdd);
32+
33+
// Element should've been added to the end of the queue
34+
expect(queue.last).to.equal(elementToAdd);
35+
// But since it is now the only element, it should also be the at beginning as well
36+
expect(queue.first).to.equal(elementToAdd);
37+
// Length should've been increased by 1
38+
expect(queue.length).to.equal(originalLength + 1);
39+
});
40+
});
2441
});

0 commit comments

Comments
 (0)