-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircleQueue.js
106 lines (97 loc) · 2.64 KB
/
circleQueue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a circle.
* It is also called "Ring Buffer".
* lc622 Design Circle Queue (https://leetcode.com/problems/design-circular-queue/)
* Initialize your data structure here. Set the size of the queue to be k.
* @param {number} k
*/
var MyCircularQueue = function (k) {
this.arr = new Array(k).fill(-1);
this.start = -1;
this.tail = -1;
};
/**
* Insert an element into the circular queue. Return true if the operation is successful.
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function (value) {
if (this.start === -1 && this.tail === -1) {
this.start = 0;
this.tail = 0;
this.arr[0] = value;
return true;
}
if ((this.tail + 1) % this.arr.length === this.start) {
return false;
} else {
this.tail = (this.tail + 1) % this.arr.length;
this.arr[this.tail] = value;
return true;
}
};
/**
* Delete an element from the circular queue. Return true if the operation is successful.
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function () {
if (this.start === -1 && this.tail === -1) {
return false;
}
if (this.start === this.tail) {
this.arr[this.start] = -1;
this.start = -1;
this.tail = -1;
return true;
} else {
this.arr[this.start] = -1;
this.start = (this.start + 1) % this.arr.length;
return true;
}
};
/**
* Get the front item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Front = function () {
if (this.start === -1) {
return -1;
} else {
return this.arr[this.start];
}
};
/**
* Get the last item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Rear = function () {
if (this.tail === -1) {
return -1;
} else {
return this.arr[this.tail];
}
};
/**
* Checks whether the circular queue is empty or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function () {
return this.start === -1 && this.tail === -1;
};
/**
* Checks whether the circular queue is full or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function () {
return (this.tail + 1) % this.arr.length === this.start;
};
/**
* Your MyCircularQueue object will be instantiated and called as such:
* var obj = new MyCircularQueue(k)
* var param_1 = obj.enQueue(value)
* var param_2 = obj.deQueue()
* var param_3 = obj.Front()
* var param_4 = obj.Rear()
* var param_5 = obj.isEmpty()
* var param_6 = obj.isFull()
*/