Skip to content

Commit

Permalink
feature: added return of removed element when queue is full
Browse files Browse the repository at this point in the history
  • Loading branch information
hbarcelos committed Feb 5, 2020
1 parent ea68789 commit 96d9680
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions round-linked-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class RoundLinkedQueue {
next: null,
};

let removedElement;

if (this.length < this.maxLength) {
if (!this._first) {
this._first = node;
Expand All @@ -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;
}
}

Expand Down
19 changes: 19 additions & 0 deletions round-linked-queue.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.