mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-07 01:44:52 +08:00
Add comments to Queue class.
This commit is contained in:
@ -2,6 +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
|
||||||
|
// 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.
|
||||||
this.linkedList = new LinkedList();
|
this.linkedList = new LinkedList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -9,6 +13,7 @@ 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.tail;
|
return !this.linkedList.tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -17,9 +22,11 @@ export default class Queue {
|
|||||||
*/
|
*/
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +34,9 @@ export default class Queue {
|
|||||||
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,6 +44,8 @@ export default class Queue {
|
|||||||
* @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;
|
||||||
}
|
}
|
||||||
@ -43,6 +55,7 @@ export default class Queue {
|
|||||||
* @return {string}
|
* @return {string}
|
||||||
*/
|
*/
|
||||||
toString(callback) {
|
toString(callback) {
|
||||||
|
// Return string representation of the queue's linked list.
|
||||||
return this.linkedList.toString(callback);
|
return this.linkedList.toString(callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user