Skip to content

Commit e05b443

Browse files
authored
merge: Improved the complexity of dequeue O(n) to O(1) (TheAlgorithms#1005)
* feat: improved memoize function used Map instead of object & used the JSON.stringfy method for generate a valid string as a key * docs: modified documentation * style: format with standard * docs: modified stringify doc * refactor: remove two repetition implementation * feat: added validation, test codes * chore: remove useless words * feat: added types for jest * chore: added link box * feat: added new validation test casses & methods * style: formated with standard * feat: added parse method & test cases * docs: added js docs * chore: added default import export * feat: imporved algorithm via replace method * test: added two test cases * feat: added jest type for suggestions * feat: added `reduceRight` & `trim` method * chore: added helper variable * feat: added new rotation option * Revert "chore: added helper variable" This reverts commit 489544d. * remove: yarn lock * chore: fix grammer * feat: used replace method & added test case * feat: remove revert * chore: added new line * feat: updated the Queue array to linkedlist DS * chore: fixed grammer * resolve: removed capacity related codes, & updated test cases * feat: added length dicrease code on dequeue
1 parent 03d0b1e commit e05b443

File tree

2 files changed

+112
-56
lines changed

2 files changed

+112
-56
lines changed

Data-Structures/Queue/Queue.js

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,114 @@
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 operation complexities 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
10+
#size
11+
1112
constructor () {
12-
// This is the array representation of the queue
13-
this.queue = []
13+
this.head = null
14+
this.tail = null
15+
this.#size = 0
16+
17+
return Object.seal(this)
1418
}
1519

16-
// methods
17-
// Add a value to the end of the queue
18-
enqueue (item) {
19-
this.queue.push(item)
20+
get length () {
21+
return this.#size
2022
}
2123

22-
// Removes the value at the front of the queue
24+
/**
25+
* @description - Add a value to the end of the queue
26+
* @param {*} data
27+
* @returns {number} - The current size of queue
28+
*/
29+
enqueue (data) {
30+
const node = { data, next: null }
31+
32+
if (!this.head && !this.tail) {
33+
this.head = node
34+
this.tail = node
35+
} else {
36+
this.tail.next = node
37+
this.tail = node
38+
}
39+
40+
return ++this.#size
41+
}
42+
43+
/**
44+
* @description - Removes the value at the front of the queue
45+
* @returns {*} - The first data of the queue
46+
*/
2347
dequeue () {
24-
if (this.empty()) {
48+
if (this.isEmpty()) {
2549
throw new Error('Queue is Empty')
2650
}
2751

28-
return this.queue.shift() // remove the item at position 0 from the array and return it
52+
const firstData = this.peekFirst()
53+
54+
this.head = this.head.next
55+
56+
if (!this.head) {
57+
this.tail = null
58+
}
59+
60+
this.#size--
61+
62+
return firstData
2963
}
3064

31-
// Return the length of the queue
32-
length () {
33-
return this.queue.length
65+
/**
66+
* @description - Return the item at the front of the queue
67+
* @returns {*}
68+
*/
69+
peekFirst () {
70+
if (this.isEmpty()) {
71+
throw new Error('Queue is Empty')
72+
}
73+
74+
return this.head.data
3475
}
3576

36-
// Return the item at the front of the queue
37-
peek () {
38-
if (this.empty()) {
77+
/**
78+
* @description - Return the item at the tail of the queue
79+
* @returns {*}
80+
*/
81+
peekLast () {
82+
if (this.isEmpty()) {
3983
throw new Error('Queue is Empty')
4084
}
4185

42-
return this.queue[0]
86+
return this.tail.data
4387
}
4488

45-
// List all the items in the queue
46-
view (output = value => console.log(value)) {
47-
output(this.queue)
89+
/**
90+
* @description - Return the array of Queue
91+
* @returns {Array<*>}
92+
*/
93+
toArray () {
94+
const array = []
95+
let node = this.head
96+
97+
while (node) {
98+
array.push(node.data)
99+
node = node.next
100+
}
101+
102+
return array
48103
}
49104

50-
// Return Is queue empty ?
51-
empty () {
52-
return this.queue.length === 0
105+
/**
106+
* @description - Return is queue empty or not
107+
* @returns {boolean}
108+
*/
109+
isEmpty () {
110+
return this.length === 0
53111
}
54112
}
55113

56-
export { Queue }
114+
export default Queue
Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,46 @@
1-
import { Queue } from '../Queue'
1+
import Queue from '../Queue'
22

3-
describe('Queue', () => {
4-
it('Check enqueue/dequeue', () => {
5-
const queue = new Queue()
6-
queue.enqueue(1)
7-
queue.enqueue(2)
8-
queue.enqueue(8)
9-
queue.enqueue(9)
3+
describe('Testing the Queue DS', () => {
4+
const queue = new Queue()
105

11-
expect(queue.dequeue()).toBe(1)
12-
expect(queue.dequeue()).toBe(2)
6+
it('Testing enqueue method', () => {
7+
expect(queue.enqueue(1)).toBe(1)
8+
expect(queue.enqueue(2)).toBe(2)
9+
expect(queue.enqueue(8)).toBe(3)
10+
expect(queue.enqueue(9)).toBe(4)
1311
})
1412

15-
it('Check length', () => {
16-
const queue = new Queue()
17-
18-
queue.enqueue(1)
19-
queue.enqueue(2)
20-
queue.enqueue(8)
21-
queue.enqueue(9)
13+
it('Testing length after enqueue', () => {
14+
expect(queue.length).toBe(4)
15+
})
2216

23-
expect(queue.length()).toBe(4)
17+
it('Testing peekFirst & peekLast methods', () => {
18+
expect(queue.peekFirst()).toBe(1)
19+
expect(queue.peekLast()).toBe(9)
2420
})
2521

26-
it('Check peek', () => {
27-
const queue = new Queue()
22+
it('Testing toArray method', () => {
23+
expect(queue.toArray()).toEqual([1, 2, 8, 9])
24+
})
2825

29-
queue.enqueue(1)
30-
queue.enqueue(2)
31-
queue.enqueue(8)
32-
queue.enqueue(9)
26+
it('Testing dequeue method', () => {
27+
expect(queue.dequeue()).toBe(1)
28+
expect(queue.dequeue()).toBe(2)
29+
})
3330

34-
expect(queue.peek()).toBe(1)
31+
it('Testing length after dequeue', () => {
32+
expect(queue.length).toBe(2)
3533
})
3634

37-
it('Check empty', () => {
35+
it('Testing isEmpty method', () => {
3836
const queue = new Queue()
39-
expect(queue.empty()).toBeTruthy()
37+
expect(queue.isEmpty()).toBeTruthy()
4038

4139
queue.enqueue(1)
4240
queue.enqueue(2)
4341
queue.enqueue(8)
4442
queue.enqueue(9)
4543

46-
expect(queue.empty()).toBeFalsy()
44+
expect(queue.isEmpty()).toBeFalsy()
4745
})
4846
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy