@@ -2,50 +2,47 @@ import LinkedList from '../linked-list/LinkedList';
22
33export 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.
5+ // We're going to implement Queue based on LinkedList since the two
6+ // structures are quite similar. Namely, they both operate mostly on
7+ // the elements at the beginning and the end. Compare enqueue/dequeue
8+ // operations of Queue with append/deleteHead operations of LinkedList.
99 this . linkedList = new LinkedList ( ) ;
1010 }
1111
1212 /**
1313 * @return {boolean }
1414 */
1515 isEmpty ( ) {
16- // The queue is empty in case if its linked list don't have tail.
17- return ! this . linkedList . tail ;
16+ return ! this . linkedList . head ;
1817 }
1918
2019 /**
20+ * Read the element at the front of the queue without removing it.
2121 * @return {* }
2222 */
2323 peek ( ) {
2424 if ( ! this . linkedList . head ) {
25- // If linked list is empty then there is nothing to peek from.
2625 return null ;
2726 }
2827
29- // Just read the value from the end of linked list without deleting it.
3028 return this . linkedList . head . value ;
3129 }
3230
3331 /**
32+ * Add a new element to the end of the queue (the tail of the linked list).
33+ * This element will be processed after all elements ahead of it.
3434 * @param {* } value
3535 */
3636 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.
4037 this . linkedList . append ( value ) ;
4138 }
4239
4340 /**
41+ * Remove the element at the front of the queue (the head of the linked list).
42+ * If the queue is empty, return null.
4443 * @return {* }
4544 */
4645 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.
4946 const removedHead = this . linkedList . deleteHead ( ) ;
5047 return removedHead ? removedHead . value : null ;
5148 }
0 commit comments