1、问题
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
你的实现应该支持如下操作:
MyCircularQueue(k)
: 构造器,设置队列长度为 k 。Front
: 从队首获取元素。如果队列为空,返回 -1 。Rear
: 获取队尾元素。如果队列为空,返回 -1 。enQueue(value)
: 向循环队列插入一个元素。如果成功插入则返回真。deQueue()
: 从循环队列中删除一个元素。如果成功删除则返回真。isEmpty()
: 检查循环队列是否为空。isFull()
: 检查循环队列是否已满。
2、示例
MyCircularQueue circularQueue = new MyCircularQueue(3); // 设置长度为 3 circularQueue.enQueue(1); // 返回 true circularQueue.enQueue(2); // 返回 true circularQueue.enQueue(3); // 返回 true circularQueue.enQueue(4); // 返回 false,队列已满 circularQueue.Rear(); // 返回 3 circularQueue.isFull(); // 返回 true circularQueue.deQueue(); // 返回 true circularQueue.enQueue(4); // 返回 true circularQueue.Rear(); // 返回 4
3、理解题意
题意:利用数组实现循环队列的一下方法
4、具体步骤
(1)数组构建循环队列需要哪些元素?数组queue、头指针front、尾指针rear、capacity队列最大容量、size队列中当前元素数量
(2)判断队列是否为空,直接判断当前元素size是否为0
(3)判断队是否满,直接判断当前元素数size是否和队列的最大长度相同
(4)插入:
1)判断队满,满则返回。
2)判断队空?空,头指针前移:非空,尾指针前移,插入,当前元素数+1
(5)删除
1)判空?空,返回:非空(最后一个元素?是,重置头、尾指针:否,头指针前移,当前元素-1)
(6)从队首获取元素
1)判空?空,返回-1:非空,返回头指针指向的元素
(7)从队尾获取元素
1)空?空,返回-1:非空,返回尾指针指向的元素
5、完整代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>设计循环队列</title>
</head>
<body>
<p>
设计你的循环队列实现。 循环队列是一种线性数据结构,其操作表现基于
FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。
</p>
<p>
循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间。但是使用循环队列,我们能使用这些空间去存储新的值。
</p>
<p>
<h4>你的实现应该支持如下操作:</h4>
MyCircularQueue(k): 构造器,设置队列长度为 k 。<br>
Front: 从队首获取元素。如果队列为空,返回 -1 。<br>
Rear: 获取队尾元素。如果队列为空,返回 -1 。<br>
enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。<br>
deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。<br>
isEmpty(): 检查循环队列是否为空。<br>
isFull(): 检查循环队列是否已满。<br>
</p>
</body>
<script>
/**
* @param {number} k
*/
var MyCircularQueue = function(k) {
this.queue = new Array(k)
// 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1
this.front = -1
this.rear = -1
// 当前元素数量
this.size = 0
// 队列最大容量
this.capacity = k
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function(value) {
// 判断队列是否为空,如果为空,头指针需要向前移动
if (this.isEmpty()) {
this.front = 0
}
if (this.isFull()) {
console.log(false,this.queue);
return false
}
// 添加
// 尾指针向前
this.rear = (this.rear + 1)%this.capacity
// 添加
this.queue[this.rear] = value
this.size++
console.log(true,this.queue);
return true
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function() {
//
if (this.isEmpty()) {
console.log('false',this.queue);
return false
}
if (this.front===this.rear) {
// 队列中只有一个元素且该元素即将被删除
this.front = -1
this.rear = -1
}else{
// 头指针前移,删除元素
this.front = (this.front+1)%this.capacity
}
this.size--;
console.log(true,this.queue);
return true
};
/**
* @return {number}
*/
MyCircularQueue.prototype.Front = function() {
if (this.isEmpty()) {
console.log('-1',this.queue);
return -1
}
console.log(this.queue[this.front]);
return this.queue[this.front]
};
/**
* @return {number}
*/
MyCircularQueue.prototype.Rear = function() {
if (this.isEmpty()) {
return -1
}
console.log(this.queue[(this.rear + this.capacity ) % this.capacity],this.queue);
// this.rear + this.capacity确保当 this.rear 为 0 时,计算出的索引不会是负数
return this.queue[(this.rear + this.capacity ) % this.capacity]
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function() {
if (this.size === 0) {
return true
}
return false
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function() {
if (this.size === this.capacity) {
return true
}
return false
};
var circularQueue = new MyCircularQueue(3); // 设置长度为 3
circularQueue.enQueue(1); // 返回 true
circularQueue.enQueue(2); // 返回 true
circularQueue.enQueue(3); // 返回 true
circularQueue.enQueue(4); // 返回 false,队列
circularQueue.Rear(); // 返回 3
circularQueue.isFull(); // 返回 true
circularQueue.deQueue(); // 返回 true
circularQueue.enQueue(4); // 返回 true
circularQueue.Rear(); // 返回 4
/**
* 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()
*/
</script>
</html>
6、力扣通过代码
var MyCircularQueue = function(k) {
this.queue = new Array(k)
// 为了区分队列为空和队列中有元素,让头指针和尾指针指向-1
this.front = -1
this.rear = -1
// 当前元素数量
this.size = 0
// 队列最大容量
this.capacity = k
};
/**
* @param {number} value
* @return {boolean}
*/
MyCircularQueue.prototype.enQueue = function(value) {
// 判断队列是否为空,如果为空,头指针需要向前移动
if (this.isEmpty()) {
this.front = 0
}
if (this.isFull()) {
console.log(false,this.queue);
return false
}
// 添加
// 尾指针向前
this.rear = (this.rear + 1)%this.capacity
// 添加
this.queue[this.rear] = value
this.size++
console.log(true,this.queue);
return true
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.deQueue = function() {
//
if (this.isEmpty()) {
console.log('false',this.queue);
return false
}
if (this.front===this.rear) {
// 队列中只有一个元素且该元素即将被删除
this.front = -1
this.rear = -1
}else{
// 头指针前移,删除元素
this.front = (this.front+1)%this.capacity
}
this.size--;
console.log(true,this.queue);
return true
};
/**
* @return {number}
*/
MyCircularQueue.prototype.Front = function() {
if (this.isEmpty()) {
console.log('-1',this.queue);
return -1
}
console.log(this.queue[this.front]);
return this.queue[this.front]
};
/**
* @return {number}
*/
MyCircularQueue.prototype.Rear = function() {
if (this.isEmpty()) {
return -1
}
console.log(this.queue[(this.rear + this.capacity ) % this.capacity],this.queue);
return this.queue[(this.rear + this.capacity) % this.capacity]
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isEmpty = function() {
if (this.size === 0) {
return true
}
return false
};
/**
* @return {boolean}
*/
MyCircularQueue.prototype.isFull = function() {
if (this.size === this.capacity) {
return true
}
return false
};