Skip to content

Commit 4aac366

Browse files
merge: Refactor Code and Add test case (#845)
1 parent 2ae00a9 commit 4aac366

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

Data-Structures/Queue/Queue.js

+19-16
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,52 @@
55
* implementation uses an array to store the queue.
66
*/
77

8-
// Functions: enqueue, dequeue, peek, view, length
9-
10-
const Queue = (function () {
8+
// Functions: enqueue, dequeue, peek, view, length, empty
9+
class Queue {
1110
// constructor
12-
function Queue () {
11+
constructor () {
1312
// This is the array representation of the queue
1413
this.queue = []
1514
}
1615

1716
// methods
1817
// Add a value to the end of the queue
19-
Queue.prototype.enqueue = function (item) {
18+
enqueue (item) {
2019
this.queue.push(item)
2120
}
2221

2322
// Removes the value at the front of the queue
24-
Queue.prototype.dequeue = function () {
25-
if (this.queue.length === 0) {
23+
dequeue () {
24+
if (this.empty()) {
2625
throw new Error('Queue is Empty')
2726
}
2827

29-
const result = this.queue[0]
30-
this.queue.splice(0, 1) // remove the item at position 0 from the array
31-
32-
return result
28+
return this.queue.shift() // remove the item at position 0 from the array and return it
3329
}
3430

3531
// Return the length of the queue
36-
Queue.prototype.length = function () {
32+
length () {
3733
return this.queue.length
3834
}
3935

4036
// Return the item at the front of the queue
41-
Queue.prototype.peek = function () {
37+
peek () {
38+
if (this.empty()) {
39+
throw new Error('Queue is Empty')
40+
}
41+
4242
return this.queue[0]
4343
}
4444

4545
// List all the items in the queue
46-
Queue.prototype.view = function (output = value => console.log(value)) {
46+
view (output = value => console.log(value)) {
4747
output(this.queue)
4848
}
4949

50-
return Queue
51-
}())
50+
// Return Is queue empty ?
51+
empty () {
52+
return this.queue.length === 0
53+
}
54+
}
5255

5356
export { Queue }
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Queue } from '../Queue'
2+
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)
10+
11+
expect(queue.dequeue()).toBe(1)
12+
expect(queue.dequeue()).toBe(2)
13+
})
14+
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)
22+
23+
expect(queue.length()).toBe(4)
24+
})
25+
26+
it('Check peek', () => {
27+
const queue = new Queue()
28+
29+
queue.enqueue(1)
30+
queue.enqueue(2)
31+
queue.enqueue(8)
32+
queue.enqueue(9)
33+
34+
expect(queue.peek()).toBe(1)
35+
})
36+
37+
it('Check empty', () => {
38+
const queue = new Queue()
39+
expect(queue.empty()).toBeTruthy()
40+
41+
queue.enqueue(1)
42+
queue.enqueue(2)
43+
queue.enqueue(8)
44+
queue.enqueue(9)
45+
46+
expect(queue.empty()).toBeFalsy()
47+
})
48+
})

0 commit comments

Comments
 (0)