Skip to content
Permalink
Browse files

feature: added return of removed element when queue is full

  • Loading branch information
hbarcelos committed Feb 5, 2020
1 parent ea68789 commit 96d968044141094cbd0e7be6f551b1a4cb68a13c
Showing with 24 additions and 0 deletions.
  1. +5 −0 round-linked-queue.js
  2. +19 −0 round-linked-queue.test.js
@@ -30,6 +30,8 @@ class RoundLinkedQueue {
next: null,
};

let removedElement;

if (this.length < this.maxLength) {
if (!this._first) {
this._first = node;
@@ -38,11 +40,14 @@ class RoundLinkedQueue {

this._length += 1;
} else {
removedElement = this._first.data;
this._first = this._first.next;
}

this._last.next = node;
this._last = node;

return removedElement;
}
}

@@ -70,5 +70,24 @@ describe("Round-Queue", () => {
// Length should still be the same
expect(queue.length).to.equal(3, "length not properly set");
});

it("Should return the removed element from a full queue", () => {
const queue = new RoundQueue(3);
queue.add(1);
queue.add(2);
queue.add(3);

const result = queue.add(4);

expect(result).to.equal(1, "removed wrong element");
});

it("Should return undefined when the queue is not full", () => {
const queue = new RoundQueue(3);

const result = queue.add(1);

expect(result).to.equal(undefined, "should not return an element");
});
});
});

0 comments on commit 96d9680

Please sign in to comment.
You can’t perform that action at this time.