南昌网站建设kaiu,在线平面设计软件免费版,制作公司网页多钱,网页制作三合一案例教程设计你的循环队列实现。 循环队列是一种线性数据结构#xff0c;其操作表现基于 FIFO#xff08;先进先出#xff09;原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里其操作表现基于 FIFO先进先出原则并且队尾被连接在队首之后以形成一个循环。它也被称为“环形缓冲器”。循环队列的一个好处是我们可以利用这个队列之前用过的空间。在一个普通队列里一旦一个队列满了我们就不能插入下一个元素即使在队列前面仍有空间。但是使用循环队列我们能使用这些空间去存储新的值。你的实现应该支持如下操作1、MyCircularQueue(k): 构造器设置队列长度为 k 。2、Front: 从队首获取元素。如果队列为空返回 -1 。3、Rear: 获取队尾元素。如果队列为空返回 -1 。4、enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。5、deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。6、isEmpty(): 检查循环队列是否为空。7、isFull(): 检查循环队列是否已满。 示例MyCircularQueue circularQueue new MyCircularQueue(3); // 设置长度为 3circularQueue.enQueue(1); // 返回 truecircularQueue.enQueue(2); // 返回 truecircularQueue.enQueue(3); // 返回 truecircularQueue.enQueue(4); // 返回 false队列已满circularQueue.Rear(); // 返回 3circularQueue.isFull(); // 返回 truecircularQueue.deQueue(); // 返回 truecircularQueue.enQueue(4); // 返回 truecircularQueue.Rear(); // 返回 4 提示1、所有的值都在 0 至 1000 的范围内2、操作数将在 1 至 1000 的范围内3、请不要使用内置的队列库。思路数组下标循环的小技巧1. 下标最后再往后(offset 小于 array.length): index (index offset) % array.length2. 下标最前再往前(offset 小于 array.length): index (index array.length - offset) % array.length如何区分空与满1. 通过添加 size 属性记录2. 保留一个位置3. 使用标记代码class MyCircularQueue {public int front;//队头下标public int rear;public int[] elem;//构造方法,k 队列的长度public MyCircularQueue(int k) {this.elemnew int[k1];}//入队public boolean enQueue(int value) {if (isFull()){return false;}this.elem[rear]value;this.rear(this.rear1)%this.elem.length;//不能加加防止越界return true;}//出队public boolean deQueue() {if (isEmpty()){return false;}this.front(this.front1)%this.elem.length;return true;}//获取队头元素public int Front() {if (isEmpty()){return -1;}return this.elem[this.front];}//获取队尾元素public int Rear() {if (isEmpty()){return -1;}int index-1;if (this.rear0){indexthis.elem.length-1;}else {indexthis.rear-1;}return this.elem[index];}public boolean isEmpty() {return this.frontthis.rear;}public boolean isFull() {if ((this.rear1)%this.elem.lengththis.front){return true;}return false;}
}