Skip to content

Commit 6fb4bfc

Browse files
committed
feat: updated the Queue array to linkedlist DS
1 parent b30f149 commit 6fb4bfc

2 files changed

Lines changed: 124 additions & 40 deletions

File tree

Data-Structures/Queue/Queue.js

Lines changed: 94 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,121 @@
11
/* Queue
22
* 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.
67
*/
78

8-
// Functions: enqueue, dequeue, peek, view, length, empty
99
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
1427
}
1528

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
2050
}
2151

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+
*/
2356
dequeue () {
24-
if (this.empty()) {
57+
if (this.isEmpty()) {
2558
throw new Error('Queue is Empty')
2659
}
2760

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
2970
}
3071

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
3482
}
3583

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()) {
3990
throw new Error('Queue is Empty')
4091
}
4192

42-
return this.queue[0]
93+
return this.tail.data
4394
}
4495

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
48110
}
49111

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
53118
}
54119
}
55120

56-
export { Queue }
121+
export default Queue
Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
import { Queue } from '../Queue'
1+
import Queue from '../Queue'
2+
3+
describe('Testing the Queue DS', () => {
4+
it('Check Capacity', () => {
5+
const queue = new Queue(3)
6+
queue.enqueue(1)
7+
queue.enqueue(2)
8+
queue.enqueue(8)
9+
10+
expect(() => queue.enqueue(9)).toThrow()
11+
})
212

3-
describe('Queue', () => {
413
it('Check enqueue/dequeue', () => {
5-
const queue = new Queue()
14+
const queue = new Queue(10)
615
queue.enqueue(1)
716
queue.enqueue(2)
817
queue.enqueue(8)
@@ -13,36 +22,46 @@ describe('Queue', () => {
1322
})
1423

1524
it('Check length', () => {
16-
const queue = new Queue()
25+
const queue = new Queue(5)
1726

1827
queue.enqueue(1)
1928
queue.enqueue(2)
2029
queue.enqueue(8)
2130
queue.enqueue(9)
2231

23-
expect(queue.length()).toBe(4)
32+
expect(queue.toArray()).toEqual([1, 2, 8, 9])
2433
})
2534

26-
it('Check peek', () => {
27-
const queue = new Queue()
35+
it('Check peekFirst & peekLast', () => {
36+
const queue = new Queue(4)
2837

2938
queue.enqueue(1)
3039
queue.enqueue(2)
3140
queue.enqueue(8)
3241
queue.enqueue(9)
3342

34-
expect(queue.peek()).toBe(1)
43+
expect(queue.peekFirst()).toBe(1)
44+
expect(queue.peekLast()).toBe(9)
3545
})
3646

37-
it('Check empty', () => {
47+
it('Check toArray', () => {
3848
const queue = new Queue()
39-
expect(queue.empty()).toBeTruthy()
49+
50+
queue.enqueue(1)
51+
queue.enqueue(2)
52+
queue.enqueue(8)
53+
queue.enqueue(9)
54+
})
55+
56+
it('Check empty', () => {
57+
const queue = new Queue(5)
58+
expect(queue.isEmpty()).toBeTruthy()
4059

4160
queue.enqueue(1)
4261
queue.enqueue(2)
4362
queue.enqueue(8)
4463
queue.enqueue(9)
4564

46-
expect(queue.empty()).toBeFalsy()
65+
expect(queue.isEmpty()).toBeFalsy()
4766
})
4867
})

0 commit comments

Comments
 (0)