Skip to content
Permalink
Browse files

feature: handled adding elements when queue is not empty

  • Loading branch information
hbarcelos committed Feb 5, 2020
1 parent 1d1bddc commit a1b4a3e194e188a01be5027fceb903bb55e1cd05
Showing with 34 additions and 5 deletions.
  1. +17 −5 round-linked-queue.js
  2. +17 −0 round-linked-queue.test.js
@@ -18,17 +18,29 @@ class RoundLinkedQueue {
}

get first() {
return this._first;
return this._first.data;
}

get last() {
return this._last;
return this._last.data;
}

add(element) {
this._root = element;
this._first = element;
this._last = element;
const node = {
data: element,
next: null,
};

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;
}
@@ -37,5 +37,22 @@ describe("Round-Queue", () => {
// Length should've been increased by 1
expect(queue.length).to.equal(originalLength + 1);
});

it("Should add an element to the end of a non-empty queue", () => {
const queue = new RoundQueue(3);
const previousElement = 1;
const elementToAdd = 2;
// Make the queue non-empty
queue.add(previousElement);

queue.add(elementToAdd);

// Element should've been added to the end of the queue
expect(queue.last).to.equal(elementToAdd, "last not properly set");
// But the first pointer must remain the first element added
expect(queue.first).to.equal(previousElement, "first not properly set");
// Length should've been increased by 2
expect(queue.length).to.equal(2, "length not properly set");
});
});
});

0 comments on commit a1b4a3e

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