Skip to content

Commit 96d9680

Browse files
committed
feature: added return of removed element when queue is full
1 parent ea68789 commit 96d9680

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

round-linked-queue.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class RoundLinkedQueue {
3030
next: null,
3131
};
3232

33+
let removedElement;
34+
3335
if (this.length < this.maxLength) {
3436
if (!this._first) {
3537
this._first = node;
@@ -38,11 +40,14 @@ class RoundLinkedQueue {
3840

3941
this._length += 1;
4042
} else {
43+
removedElement = this._first.data;
4144
this._first = this._first.next;
4245
}
4346

4447
this._last.next = node;
4548
this._last = node;
49+
50+
return removedElement;
4651
}
4752
}
4853

round-linked-queue.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,24 @@ describe("Round-Queue", () => {
7070
// Length should still be the same
7171
expect(queue.length).to.equal(3, "length not properly set");
7272
});
73+
74+
it("Should return the removed element from a full queue", () => {
75+
const queue = new RoundQueue(3);
76+
queue.add(1);
77+
queue.add(2);
78+
queue.add(3);
79+
80+
const result = queue.add(4);
81+
82+
expect(result).to.equal(1, "removed wrong element");
83+
});
84+
85+
it("Should return undefined when the queue is not full", () => {
86+
const queue = new RoundQueue(3);
87+
88+
const result = queue.add(1);
89+
90+
expect(result).to.equal(undefined, "should not return an element");
91+
});
7392
});
7493
});

0 commit comments

Comments
 (0)