@@ -2,13 +2,18 @@ import LinkedList from '../linked-list/LinkedList';
2
2
3
3
export default class Queue {
4
4
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.
5
9
this . linkedList = new LinkedList ( ) ;
6
10
}
7
11
8
12
/**
9
13
* @return {boolean }
10
14
*/
11
15
isEmpty ( ) {
16
+ // The queue is empty in case if its linked list don't have tail.
12
17
return ! this . linkedList . tail ;
13
18
}
14
19
@@ -17,23 +22,30 @@ export default class Queue {
17
22
*/
18
23
peek ( ) {
19
24
if ( ! this . linkedList . head ) {
25
+ // If linked list is empty then there is nothing to peek from.
20
26
return null ;
21
27
}
22
28
29
+ // Just read the value from the end of linked list without deleting it.
23
30
return this . linkedList . head . value ;
24
31
}
25
32
26
33
/**
27
34
* @param {* } value
28
35
*/
29
36
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.
30
40
this . linkedList . append ( value ) ;
31
41
}
32
42
33
43
/**
34
44
* @return {* }
35
45
*/
36
46
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.
37
49
const removedHead = this . linkedList . deleteHead ( ) ;
38
50
return removedHead ? removedHead . value : null ;
39
51
}
@@ -43,6 +55,7 @@ export default class Queue {
43
55
* @return {string }
44
56
*/
45
57
toString ( callback ) {
58
+ // Return string representation of the queue's linked list.
46
59
return this . linkedList . toString ( callback ) ;
47
60
}
48
61
}
0 commit comments