Skip to content
Permalink
Browse files

feature: handled removing an element from an empty queue

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

let removedElement;
let removedNode = {};

if (this.length < this.maxLength) {
if (!this._first) {
@@ -40,23 +40,26 @@ class RoundLinkedQueue {

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

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

return removedElement;
return removedNode.data;
}

remove() {
const removedElement = this.first;
const removedNode = this._first;
if (!removedNode) {
throw new Error("Cannot remove element from an empty queue");
}

this._first = this._first.next;
this._length -= 1;

return removedElement;
return removedNode.data;
}
}

@@ -108,5 +108,11 @@ describe("Round-Queue", () => {
expect(queue.first).to.equal(2, "should shift the second element to the head of the queue");
expect(queue.last).to.equal(3, "should not change the last element");
});

it("Should throw an error when the queue is empty", () => {
const queue = new RoundQueue(3);

expect(() => queue.remove()).to.throw("Cannot remove element from an empty queue");
});
});
});

0 comments on commit f4f8c9a

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