Skip to content
Permalink
Browse files

feature: handled adding elements when queue is full

  • Loading branch information
hbarcelos committed Feb 5, 2020
1 parent a1b4a3e commit 52205dbaa54c50381b70efaa1bbc53450f9f83cc
Showing with 32 additions and 9 deletions.
  1. +16 −9 round-linked-queue.js
  2. +16 −0 round-linked-queue.test.js
@@ -31,18 +31,25 @@ class RoundLinkedQueue {
next: null,
};

if (!this._root) {
this._root = node;
this._first = node;
this._last = node;
if (this.length < this.maxLength) {
if (!this._root) {
this._root = node;
this._first = node;
this._last = node;
} else {
const previousLast = this._last;
previousLast.next = node;

this._last = node;
}

this._length += 1;
} else {
const previousLast = this._last;
previousLast.next = node;

this._root = this._root.next;
this._last.next = node;
this._first = this._root;
this._last = node;
}

this._length += 1;
}
}

@@ -54,5 +54,21 @@ describe("Round-Queue", () => {
// Length should've been increased by 2
expect(queue.length).to.equal(2, "length not properly set");
});

it("Should remove the first element and add the new element to the end of a full queue", () => {
const queue = new RoundQueue(3);
queue.add(1);
queue.add(2);
queue.add(3);

queue.add(4);

// Element should've been added to the end of the queue
expect(queue.last).to.equal(4, "last not properly set");
// The second element should've been shifted to the first position
expect(queue.first).to.equal(2, "first not properly set");
// Length should still be the same
expect(queue.length).to.equal(3, "length not properly set");
});
});
});

0 comments on commit 52205db

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