clarify comments (#193)

This commit is contained in:
Kevin Brewer 2018-08-31 04:14:29 -05:00 committed by Oleksii Trekhleb
parent 002d32a8cd
commit 6b0bacd993

View File

@ -2,10 +2,10 @@ import LinkedList from '../linked-list/LinkedList';
export default class Queue { export default class Queue {
constructor() { constructor() {
// We're going to implement Queue based on LinkedList since this // We're going to implement Queue based on LinkedList since the two
// structures a quite similar. Namely they both operates mostly with // structures are quite similar. Namely, they both operate mostly on
// with theirs beginning and the end. Compare enqueue/de-queue // the elements at the beginning and the end. Compare enqueue/dequeue
// operations of the Queue with append/prepend operations of LinkedList. // operations of Queue with append/deleteHead operations of LinkedList.
this.linkedList = new LinkedList(); this.linkedList = new LinkedList();
} }
@ -13,39 +13,36 @@ export default class Queue {
* @return {boolean} * @return {boolean}
*/ */
isEmpty() { isEmpty() {
// The queue is empty in case if its linked list don't have tail. return !this.linkedList.head;
return !this.linkedList.tail;
} }
/** /**
* Read the element at the front of the queue without removing it.
* @return {*} * @return {*}
*/ */
peek() { peek() {
if (!this.linkedList.head) { if (!this.linkedList.head) {
// If linked list is empty then there is nothing to peek from.
return null; return null;
} }
// Just read the value from the end of linked list without deleting it.
return this.linkedList.head.value; return this.linkedList.head.value;
} }
/** /**
* Add a new element to the end of the queue (the tail of the linked list).
* This element will be processed after all elements ahead of it.
* @param {*} value * @param {*} value
*/ */
enqueue(value) { enqueue(value) {
// Enqueueing means to stand in the line. Therefore let's just add
// new value at the beginning of the linked list. It will need to wait
// until all previous nodes will be processed.
this.linkedList.append(value); this.linkedList.append(value);
} }
/** /**
* Remove the element at the front of the queue (the head of the linked list).
* If the queue is empty, return null.
* @return {*} * @return {*}
*/ */
dequeue() { dequeue() {
// Let's try to delete the last node from linked list (the tail).
// If there is no tail in linked list (it is empty) just return null.
const removedHead = this.linkedList.deleteHead(); const removedHead = this.linkedList.deleteHead();
return removedHead ? removedHead.value : null; return removedHead ? removedHead.value : null;
} }