- 博主简介:努力学习的22级计算机科学与技术本科生一枚
- 博主主页: @是瑶瑶子啦
- 每日一言: 每一个不曾起舞的日子,都是对生命的辜负。——尼采
Q.rear == Q.front
Q.rear.next == Q.front
/(rear+1)%size == front
(满的时候可以看上图,此时rear指向的空间浪费掉了)⭐这里就要注意,因为是浪费一个空间来判满的,所以比如我们需要一个容量为
k
的循环队列,那么实际的物理容量应该设计为k+1
个!!!(这题在下面代码有体现,否则只能存k-1个)!
头尾指针含义(重点)
font
:指向队头元素rear
:下一个待插入元素的位置
♀️代码:
class MyCircularQueue {
int[] myCircularQueue;
int front = 0;
int rear = 0;
int size = 0;
//构造函数,创建一个循环队列
public MyCircularQueue(int k) {
this.size = k+1;//!注意,这里需要+1
this.myCircularQueue = new int[size];
}
//入队操作
public boolean enQueue(int value) {
if (isFull()){
return false;
}
myCircularQueue[rear] = value;
rear = (rear+1)%size;
return true;
}
//出队操作
public boolean deQueue() {
if(isEmpty()){
return false;
}
front = (front + 1)%size;
return true;
}
//读取队头元素(注意判空)
public int Front() {
if(isEmpty()){
return -1;
}
return myCircularQueue[front];
}
//读取队尾元素(注意判空)
public int Rear() {
if(isEmpty()){
return -1;
}
return myCircularQueue[(rear - 1 + size) % size ];
}
//判空
public boolean isEmpty() {
if(front == rear){
return true;
}
return false;
}
//判满
public boolean isFull() {
if((rear+1)%size == front){
return true;
}
return false;
}
}
/**
* Your MyCircularQueue object will be instantiated and called as such:
* MyCircularQueue obj = new MyCircularQueue(k);
* boolean param_1 = obj.enQueue(value);
* boolean param_2 = obj.deQueue();
* int param_3 = obj.Front();
* int param_4 = obj.Rear();
* boolean param_5 = obj.isEmpty();
* boolean param_6 = obj.isFull();
*/
class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2 ;
public MyQueue() {
stack1 = new Stack<>();
stack2 = new Stack<>();
}
/** 添加元素到队尾 */
public void push(int x) {
stack1.push(x);
}
/** 将stack1的元素挪到stack2 */
public void stack1ToStack2(){
while(!stack1.empty()){
stack2.push(stack1.pop());
}
}
/** 删除队头的元素并返回 */
public int pop() {
if(stack2.empty()){
stack1ToStack2();
}
return stack2.pop();
}
/** 返回队头元素 */
public int peek() {
if(stack2.empty()){
stack1ToStack2();
}
return stack2.peek();
}
/** 判断队列是否为空 */
public boolean empty() {
return stack1.empty() && stack2.empty();
}
}
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = new MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* boolean param_4 = obj.empty();
*/
class MyStack {
Queue<Integer> queue;
public MyStack() {
queue = new LinkedList<>();
}
public void push(int x) {
//先直接添加
queue.offer(x);
//将新元素(栈顶元素)前的所有元素顺次移到栈顶元素之后
for (int i = 0; i < queue.size() - 1; i++){
queue.offer(queue.poll());
}
}
public int pop() {
return queue.poll();
}
public int top() {
return queue.peek();
}
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
queue1
,只不过在添加新元素之前,把新元素放在空的辅助队列queue2
中——使之处于队头(栈顶),然后将queue1
中的元素顺次移入,此时再把queue1
和queue2
互换即可(脱裤子放屁的感觉,和方法一基本上是一样的)class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
public MyStack() {
queue1 = new LinkedList<Integer>();
queue2 = new LinkedList<Integer>();
}
public void push(int x) {
queue2.offer(x);
while (!queue1.isEmpty()) {
queue2.offer(queue1.poll());
}
Queue<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
}
public int pop() {
return queue1.poll();
}
public int top() {
return queue1.peek();
}
public boolean empty() {
return queue1.isEmpty();
}
}
若有疑问的地方,欢迎随时在评论区or私信找瑶瑶子交流讨论
Java岛冒险记【从小白到大佬之路】
LeetCode每日一题–进击大厂
Go语言核心编程
算法