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
classQueue{
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+
returnObject.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+
getlength(){
21+
returnthis.#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+
constnode={ 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
thrownewError('Queue is Empty')
2650
}
2751

28-
returnthis.queue.shift()// remove the item at position 0 from the array and return it
52+
constfirstData=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+
returnfirstData
2963
}
3064

31-
// Return the length of the queue
32-
length(){
33-
returnthis.queue.length
65+
/**
66+
* @description - Return the item at the front of the queue
67+
* @returns{*}
68+
*/
69+
peekFirst(){
70+
if(this.isEmpty()){
71+
thrownewError('Queue is Empty')
72+
}
73+
74+
returnthis.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
thrownewError('Queue is Empty')
4084
}
4185

42-
returnthis.queue[0]
86+
returnthis.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+
constarray=[]
95+
letnode=this.head
96+
97+
while(node){
98+
array.push(node.data)
99+
node=node.next
100+
}
101+
102+
returnarray
48103
}
49104

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

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

3-
describe('Queue',()=>{
4-
it('Check enqueue/dequeue',()=>{
5-
constqueue=newQueue()
6-
queue.enqueue(1)
7-
queue.enqueue(2)
8-
queue.enqueue(8)
9-
queue.enqueue(9)
3+
describe('Testing the Queue DS',()=>{
4+
constqueue=newQueue()
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-
constqueue=newQueue()
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-
constqueue=newQueue()
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
constqueue=newQueue()
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)