-
Notifications
You must be signed in to change notification settings - Fork 4
/
622.Queue_Circular.js
92 lines (86 loc) · 1.95 KB
/
622.Queue_Circular.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
/**
* 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).fill(null);
// 设置队列长度
this.maxSize = k;
// 队列头尾指针
this.headPointer = 0;
this.tailPointer = 0;
// 当前使用空间
this.currentSize = 0;
};
/**
* 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()) {
this.data[this.tailPointer] = value;
this.currentSize++;
if (!this.isEmpty()) {
this.tailPointer++;
}
return true;
}
return false;
};
/**
* Delete an element from the circular queue. Return true if the operation is successful.
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function() {
if (this.currentSize !== 0) {
this.currentSize--;
this.data[this.headPointer] = null;
this.headPointer++;
return true;
}
return false;
};
/**
* Get the front item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Front = function() {
console.log(this.isEmpty());
if (!this.isEmpty()) {
return this.data[this.headPointer];
} else {
return -1;
}
};
/**
* Get the last item from the queue.
* @return {number}
*/
MyCircularQueue.prototype.Rear = function() {
if (!this.isEmpty()) {
return this.data[this.tailPointer - 1];
} else {
return -1;
}
};
/**
* Checks whether the circular queue is empty or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function() {
return this.data.every(function(e) {
return Object.prototype.toString.call(e) === "[object Null]";
});
};
/**
* Checks whether the circular queue is full or not.
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function() {
if (this.currentSize == this.maxSize) {
return true;
}
return false;
};