-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdesign-circular-queue.js
More file actions
82 lines (75 loc) · 2.04 KB
/
Copy pathdesign-circular-queue.js
File metadata and controls
82 lines (75 loc) · 2.04 KB
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
/**
* Initialize your data structure here. Set the size of the queue to be k.
* @param {number} k
*/
var MyCircularQueue = function(k) {
this.data = Array(k);
this.size = k;
this.front = 0;
this.back = 0;
this.empty = true;
};
/**
* 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.front === this.back && this.empty === false) return false; // queue is full
this.data[this.back] = value;
this.back = (this.back + 1) % this.size;
this.empty = false;
return true;
};
/**
* Delete an element from the circular queue. Return true if the operation is successful.
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function() {
if (this.empty === true) return false;
this.data[this.front] = undefined;
this.front = (this.front + 1) % this.size;
if (this.back === this.front) this.empty = true;
return true;
};
/**
* Get the front item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Front = function() {
if (this.empty === true) return -1;
return this.data[this.front];
};
/**
* Get the last item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Rear = function() {
if (this.empty === true) return -1;
const index = this.back !== 0 ? this.back - 1 : this.size - 1;
return this.data[index];
};
/**
* Checks whether the circular queue is empty or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function() {
return this.empty;
};
/**
* Checks whether the circular queue is full or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function() {
return this.front === this.back && this.empty === false;
};
/**
* 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()
*/