Skip to content

Commit 5a57c5f

Browse files
committedAug 17, 2018
Add comments to Queue class.
1 parent 5eea378 commit 5a57c5f

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed
 

‎src/data-structures/queue/Queue.js

+13
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ import LinkedList from '../linked-list/LinkedList';
22

33
export default class Queue {
44
constructor() {
5+
// We're going to implement Queue based on LinkedList since this
6+
// structures a quite similar. Namely they both operates mostly with
7+
// with theirs beginning and the end. Compare enqueue/de-queue
8+
// operations of the Queue with append/prepend operations of LinkedList.
59
this.linkedList = new LinkedList();
610
}
711

812
/**
913
* @return {boolean}
1014
*/
1115
isEmpty() {
16+
// The queue is empty in case if its linked list don't have tail.
1217
return !this.linkedList.tail;
1318
}
1419

@@ -17,23 +22,30 @@ export default class Queue {
1722
*/
1823
peek() {
1924
if (!this.linkedList.head) {
25+
// If linked list is empty then there is nothing to peek from.
2026
return null;
2127
}
2228

29+
// Just read the value from the end of linked list without deleting it.
2330
return this.linkedList.head.value;
2431
}
2532

2633
/**
2734
* @param {*} value
2835
*/
2936
enqueue(value) {
37+
// Enqueueing means to stand in the line. Therefore let's just add
38+
// new value at the beginning of the linked list. It will need to wait
39+
// until all previous nodes will be processed.
3040
this.linkedList.append(value);
3141
}
3242

3343
/**
3444
* @return {*}
3545
*/
3646
dequeue() {
47+
// Let's try to delete the last node from linked list (the tail).
48+
// If there is no tail in linked list (it is empty) just return null.
3749
const removedHead = this.linkedList.deleteHead();
3850
return removedHead ? removedHead.value : null;
3951
}
@@ -43,6 +55,7 @@ export default class Queue {
4355
* @return {string}
4456
*/
4557
toString(callback) {
58+
// Return string representation of the queue's linked list.
4659
return this.linkedList.toString(callback);
4760
}
4861
}

0 commit comments

Comments
 (0)
Please sign in to comment.