-
Notifications
You must be signed in to change notification settings - Fork 0
/
622.js
63 lines (57 loc) · 1.41 KB
/
622.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
var MyCircularQueue = function(k) {
this.size = k;
this.buffer = [];
this.front = 0;
this.rear = -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.isFull()) return false;
this.buffer[++this.rear] = 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.isEmpty())return false;
this.buffer[this.front]=undefined;
this.front++;
return true;
};
/**
* Get the front item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Front = function() {
if(this.isEmpty()) return -1;
return this.buffer[this.front];
};
/**
* Get the last item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Rear = function() {
if(this.isEmpty()) return -1;
return this.buffer[this.rear];
};
/**
* Checks whether the circular queue is empty or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function() {
if(this.rear<this.front)return true;
return false;
};
/**
* Checks whether the circular queue is full or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function() {
return this.rear-this.front+1===this.size;
};