Skip to content

Commit

Permalink
clarify comments (trekhleb#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
appleJax authored and trekhleb committed Aug 31, 2018
1 parent 002d32a commit 6b0bacd
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions src/data-structures/queue/Queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,47 @@ import LinkedList from '../linked-list/LinkedList';

export default class Queue {
constructor() {
// We're going to implement Queue based on LinkedList since this
// structures a quite similar. Namely they both operates mostly with
// with theirs beginning and the end. Compare enqueue/de-queue
// operations of the Queue with append/prepend operations of LinkedList.
// We're going to implement Queue based on LinkedList since the two
// structures are quite similar. Namely, they both operate mostly on
// the elements at the beginning and the end. Compare enqueue/dequeue
// operations of Queue with append/deleteHead operations of LinkedList.
this.linkedList = new LinkedList();
}

/**
* @return {boolean}
*/
isEmpty() {
// The queue is empty in case if its linked list don't have tail.
return !this.linkedList.tail;
return !this.linkedList.head;
}

/**
* Read the element at the front of the queue without removing it.
* @return {*}
*/
peek() {
if (!this.linkedList.head) {
// If linked list is empty then there is nothing to peek from.
return null;
}

// Just read the value from the end of linked list without deleting it.
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
*/
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);
}

/**
* Remove the element at the front of the queue (the head of the linked list).
* If the queue is empty, return null.
* @return {*}
*/
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();
return removedHead ? removedHead.value : null;
}
Expand Down

0 comments on commit 6b0bacd

Please sign in to comment.