|
1 | 1 | /* Queue |
2 | 2 | * A Queue is a data structure that allows you to add an element to the end of |
3 | | -* a list and remove the item at the front. A queue follows a "First In First Out" |
4 | | -* system, where the first item to enter the queue is the first to be removed. This |
5 | | -* implementation uses an array to store the queue. |
| 3 | +* a list and remove the item at the front. A queue follows a FIFO (First In First Out) |
| 4 | +* system, where the first item to enter the queue is the first to be removed, |
| 5 | +* all these operations complexity are O(1). |
| 6 | +* This implementation following the linked list structure. |
6 | 7 | */ |
7 | 8 |
|
8 | | -// Functions: enqueue, dequeue, peek, view, length, empty |
9 | 9 | class Queue { |
10 | | - // constructor |
11 | | - constructor () { |
12 | | - // This is the array representation of the queue |
13 | | - this.queue = [] |
| 10 | + #size |
| 11 | + |
| 12 | + constructor (capacity) { |
| 13 | + if (capacity < 1) { |
| 14 | + throw new Error('Capacity is not valid') |
| 15 | + } |
| 16 | + |
| 17 | + this.head = null |
| 18 | + this.tail = null |
| 19 | + this.capacity = capacity |
| 20 | + this.#size = 0 |
| 21 | + |
| 22 | + return Object.seal(this) |
| 23 | + } |
| 24 | + |
| 25 | + get length () { |
| 26 | + return this.#size |
14 | 27 | } |
15 | 28 |
|
16 | | - // methods |
17 | | - // Add a value to the end of the queue |
18 | | - enqueue (item) { |
19 | | - this.queue.push(item) |
| 29 | + /** |
| 30 | + * @description - Add a value to the end of the queue |
| 31 | + * @param {*} data |
| 32 | + * @returns {number} - The current size of queue |
| 33 | + */ |
| 34 | + enqueue (data) { |
| 35 | + if (this.capacity === this.#size) { |
| 36 | + throw new RangeError('Queue is full') |
| 37 | + } |
| 38 | + |
| 39 | + const node = { data, next: null } |
| 40 | + |
| 41 | + if (!this.head && !this.tail) { |
| 42 | + this.head = node |
| 43 | + this.tail = node |
| 44 | + } else { |
| 45 | + this.tail.next = node |
| 46 | + this.tail = node |
| 47 | + } |
| 48 | + |
| 49 | + return ++this.#size |
20 | 50 | } |
21 | 51 |
|
22 | | - // Removes the value at the front of the queue |
| 52 | + /** |
| 53 | + * @description - Removes the value at the front of the queue |
| 54 | + * @returns {*} - The first data of the queue |
| 55 | + */ |
23 | 56 | dequeue () { |
24 | | - if (this.empty()) { |
| 57 | + if (this.isEmpty()) { |
25 | 58 | throw new Error('Queue is Empty') |
26 | 59 | } |
27 | 60 |
|
28 | | - return this.queue.shift() // remove the item at position 0 from the array and return it |
| 61 | + const firstData = this.peekFirst() |
| 62 | + |
| 63 | + this.head = this.head.next |
| 64 | + |
| 65 | + if (!this.head) { |
| 66 | + this.tail = null |
| 67 | + } |
| 68 | + |
| 69 | + return firstData |
29 | 70 | } |
30 | 71 |
|
31 | | - // Return the length of the queue |
32 | | - length () { |
33 | | - return this.queue.length |
| 72 | + /** |
| 73 | + * @description - Return the item at the front of the queue |
| 74 | + * @returns {*} |
| 75 | + */ |
| 76 | + peekFirst () { |
| 77 | + if (this.isEmpty()) { |
| 78 | + throw new Error('Queue is Empty') |
| 79 | + } |
| 80 | + |
| 81 | + return this.head.data |
34 | 82 | } |
35 | 83 |
|
36 | | - // Return the item at the front of the queue |
37 | | - peek () { |
38 | | - if (this.empty()) { |
| 84 | + /** |
| 85 | + * @description - Return the item at the tail of the queue |
| 86 | + * @returns {*} |
| 87 | + */ |
| 88 | + peekLast () { |
| 89 | + if (this.isEmpty()) { |
39 | 90 | throw new Error('Queue is Empty') |
40 | 91 | } |
41 | 92 |
|
42 | | - return this.queue[0] |
| 93 | + return this.tail.data |
43 | 94 | } |
44 | 95 |
|
45 | | - // List all the items in the queue |
46 | | - view (output = value => console.log(value)) { |
47 | | - output(this.queue) |
| 96 | + /** |
| 97 | + * @description - Return the array of Queue |
| 98 | + * @returns {Array<*>} |
| 99 | + */ |
| 100 | + toArray () { |
| 101 | + const array = [] |
| 102 | + let node = this.head |
| 103 | + |
| 104 | + while (node) { |
| 105 | + array.push(node.data) |
| 106 | + node = node.next |
| 107 | + } |
| 108 | + |
| 109 | + return array |
48 | 110 | } |
49 | 111 |
|
50 | | - // Return Is queue empty ? |
51 | | - empty () { |
52 | | - return this.queue.length === 0 |
| 112 | + /** |
| 113 | + * @description - Return is queue empty or not |
| 114 | + * @returns {boolean} |
| 115 | + */ |
| 116 | + isEmpty () { |
| 117 | + return this.length === 0 |
53 | 118 | } |
54 | 119 | } |
55 | 120 |
|
56 | | -export { Queue } |
| 121 | +export default Queue |
0 commit comments