java queue

// Queue<Integer> queue = new LinkedList<>();

queue.add() 入队列 (满的队列中加入新的元素时)调用 add() 方法就会抛出一个 unchecked 异常

queue.offer() 入队列 (满的队列中加入新的元素时) 调用 offer() 方法会返回 false

int pop() 从队列的开头移除并返回元素

int peek() 返回队列开头的元素

boolean empty() 如果队列为空,返回 true ;否则,返回 false

void push(int x) 将元素 x 压入栈顶。

int top() 返回栈顶元素。

MyCircularQueue(k): 构造器,设置队列长度为 k 。

Front: 从队首获取元素。如果队列为空,返回 -1 。

Rear: 获取队尾元素。如果队列为空,返回 -1 。

enQueue(value): 向循环队列插入一个元素。如果成功插入则返回真。

deQueue(): 从循环队列中删除一个元素。如果成功删除则返回真。

isEmpty(): 检查循环队列是否为空。

isFull(): 检查循环队列是否已满。

public class MyQueue {
 
    private class Node {
        private int val; //数据域
        private Node next; //指针域名
 
        private Node(int val) {
            this.val = val;
        }
    }
    //队头入,队尾出
    private Node head; //队头引用
    private Node last; //队尾引用
    private int size; //队列有效数据个数
}
// 队尾入队列
public boolean offer(int val) {
    Node newNode = new Node(val);
    // 如果是第一次入队列
    if (this.head == null) {
        this.head = newNode;
        this.last = newNode;
    } else {
        this.last.next = newNode;
        this.last = this.last.next;
    }
    this.size++;
    return true;
}
// 队头出队列
public int poll() {
    Node node = this.head;
    // 如果队列为null
    if (this.head == null) {
        throw new MyQueueIsEmptyException("队列为空!");
    } else {
        this.head = this.head.next;
    }
    this.size--;
    return node.val;
}
// 取队头元素
public int peek() {
    // 如果队列为null
    if (this.head == null) {
        throw new MyQueueIsEmptyException("队列为空!");
    } else {
        return this.head.val;
    }
}
public class MyCircularQueue {
 
    private int array[]; //存放数据的数组
    private int front; // 队头下标
    private int rear; // 队尾下标
 
    public MyCircularQueue(int k) {
        this.array = new int[k + 1]; //因为我们要浪费一个空间,所以需要多开辟一个空间
    }
 
    // 入队列
    public boolean enQueue(int value) {
        // 如果队列满的情况
        if (isFull()) {
            return false;
        }
        // 没有满就往队尾入元素
        this.array[this.rear] = value;
        // rear往前走一步,需要空出一个位置,所以当rear走到length-1时,需要修正下rear
        this.rear = (this.rear + 1) % this.array.length;
        return true;
    }
 
    // 出队列
    public boolean deQueue() {
        // 如果队列为null的情况
        if (isEmpty()) {
            return false;
        }
        // 从队头出队列,需要修正队头的位置
        this.front = (this.front + 1) % this.array.length;
        return true;
    }
    
    // 取队头元素
    public int Front() {
        // 如果队列为null的情况
        if (isEmpty()) {
            return -1;
        }
        return this.array[this.front]; //返回队头元素
    }
 
    // 取队尾元素
    public int Rear() {
        // 如果队列为null的情况
        if (isEmpty()) {
            return -1;
        }
        // 如果rear为0的情况,我们需要特殊处理
        int index = this.rear == 0 ? this.array.length - 1 : this.rear - 1;
        return this.array[index]; //返回队尾元素
    }
 
    // 判断队列是否为空
    public boolean isEmpty() {
        // 当front和rear相遇了,则表示队列中没有元素
        return this.front == this.rear;
    }
 
    // 判断队列是否满了
    public boolean isFull() {
        // 因为我们会浪费一个空间,所以rear+1等于front就满了
        // 但是我们要rear防止越界,所以要进行修正:(this.rear + 1) % this.array.length
        return (this.rear + 1) % this.array.length == this.front;
    }
}
class MyStack {
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;
 
    public MyStack() {
        this.qu1 = new LinkedList<>();
        this.qu2 = new LinkedList<>();
    }
    
    public void push(int x) {
        // 两个队列都为null的情况
        if (this.qu1.isEmpty() && this.qu2.isEmpty()) {
            this.qu1.offer(x);
            return;
        }
 
        // 哪个队列不为null往哪个队列中入元素
        if (!this.qu1.isEmpty()) {
            this.qu1.offer(x);
        } else {
            this.qu2.offer(x);
        }
    }
    
    public int pop() {
        // 如果两个队列都为空的情况下,就不能出队操作
        if (empty()) {
            return -1;
        }
        // 先将不为空的队列的size-1个元素全部放到为空的队列中
        if (!this.qu1.isEmpty()) {
            while (qu1.size() - 1 != 0) {
                qu2.offer(qu1.poll());
            }
            return qu1.poll(); //返回最后一个元素
        } else {
            while (qu2.size() - 1 != 0) {
                qu1.offer(qu2.poll());
            }
            return qu2.poll(); //返回最后一个元素
        }
    }
    
    public int top() {
        // 如果队列为null
        if (empty()) {
            return -1;
        }
        int ret = 0; //保留剩余最后一个栈顶元素的变量
        // 先将不为空的队列的size-1个元素全部放到为空的队列中
        if (!this.qu1.isEmpty()) {
            while (qu1.size() - 1 != 0) {
                qu2.offer(qu1.poll());
            }
            ret = qu1.peek();
            qu2.offer(qu1.poll());
        } else {
            while (qu2.size() - 1 != 0) {
                qu1.offer(qu2.poll());
            }
            ret = qu2.peek();
            qu1.offer(qu2.poll()); //取栈顶元素不能删除掉,还得入另一个队列中去
        }
        return ret;
    }
    
    public boolean empty() {
        return this.qu1.isEmpty() && this.qu2.isEmpty(); //两个队列都为空,栈才为空
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值