1
- /* The queue data strcture is sequential collection of elements that
2
- follows the priciple of FIFO */
3
- /*here we are using a singly linkedlist so we are only going to traverse to the next node */
1
+ /**
2
+ * This is an linkedlist-based implementation of a Queue.
3
+ * A Queue is a data structure that follows the FIFO (First In First Out) principle.
4
+ * It means that the first element that was added to the queue will be the first one to be removed.
5
+ * The time complexity of the operations is O(n).
6
+
7
+ */
8
+
9
+ /** USAGE
4
10
5
- /*USAGE
6
11
*Printers
7
12
*CPU task scheduling
8
13
*Callback queue in javascript
@@ -15,49 +20,52 @@ type Node<T> = {
15
20
}
16
21
17
22
18
- export class LinkedlistQueue < T > {
23
+ export class LinkedQueue < T > {
19
24
20
25
public length : number ;
21
26
public head ?: Node < T > ;
22
27
private tail ?: Node < T > ;
23
28
24
29
constructor ( ) {
25
- this . head = this . tail = undefined ; //when a new linkedlist is created the head becomes equal to the tail .
30
+ this . head = this . tail = undefined ;
26
31
this . length = 0 ;
27
32
}
28
33
29
- // adds elements to the rear/tail of the collection.
34
+ // adds elements to the rear/tail of the Queue
30
35
enqueue ( item : T ) : void {
31
- const node = { value : item } as Node < T > ; //creates a new node
32
- this . length ++ //increase the length of the linkedlist
36
+ const node = { value : item } as Node < T > ; // Creates a new node
37
+ this . length ++ // Increase the length of the Queue
33
38
34
39
35
40
if ( ! this . tail ) {
36
41
this . tail = this . head = node ;
37
42
return ;
38
43
}
39
- this . tail . next = node ; //updates the next tail to the node created
40
- this . tail = node ; //the tail of the linkedlist then becomes the node created!!
44
+ this . tail . next = node ; // Updates the next tail to the node created
45
+ this . tail = node ; // The tail of the Queue then becomes the node created!!
41
46
42
47
}
43
- //remove elements to the front/head of the collection
48
+ // Remove elements to the front/head of the Queue
44
49
deque ( ) : T | undefined {
50
+
51
+ // If there is no head return undefined
45
52
if ( ! this . head ) {
46
- return undefined ; //if there is no head return undefined
53
+ return undefined ;
47
54
}
55
+
48
56
this . length -- ;
49
- let head = this . head ; //we store the head in order not to lose track of it
50
- this . head = this . head . next ; //update the the head to the next node
51
- return head . value ; // return the value of the head
57
+ let head = this . head ; // We store the head in order not to lose track of it
58
+ this . head = this . head . next ; // Update the the head to the next node
59
+ return head . value ; // Return the value of the head
52
60
}
53
61
54
62
55
- // returns the value of the head
63
+ // Returns the value of the head
56
64
peek ( ) : T | undefined {
57
65
return this . head ?. value ;
58
66
}
59
67
60
- // checks if the the linkedlist is empty
68
+ // Returns true if the Queue is empty
61
69
isEmpty ( ) : boolean {
62
70
return this . length == 0
63
71
}
0 commit comments