day10 232.用栈实现队列&&225. 用队列实现栈

  1. 用栈实现队列(MyQueue)
    功能描述
    数据结构:
    使用两个栈(list1 和 list2)来模拟一个队列的行为。
    list1 用于存储新插入的元素,list2 用于反转元素顺序以实现队列的 FIFO 特性。
    方法实现:
    push(int x):
    将元素压入 list1。
    如果 list1 为空且 list2 不为空,则先将 list2 中的元素全部转移到 list1,再压入新元素。
    pop():
    如果 list2 为空,将 list1 中的所有元素转移到 list2,然后从 list2 弹出顶部元素。
    如果两个栈都为空,返回 Integer.MIN_VALUE。
    peek():
    与 pop() 类似,但不移除元素,直接返回 list2 的顶部元素。
    empty():
    检查两个栈是否都为空。
    //232.用栈实现队列
    class MyQueue {
    LinkedList list1;
    LinkedList list2;

    public MyQueue() {
    list1 = new LinkedList<>();
    list2 = new LinkedList<>();
    }

    public void push(int x) {
    if(list1.isEmpty()&&list2.isEmpty()){
    list1.push(x);
    }else {
    if(list1.isEmpty()){
    while(!list2.isEmpty()){
    list1.push(list2.pop());
    }
    list1.push(x);
    }else {
    list1.push(x);
    }
    }
    }

    public int pop() {
    //1 <= x <= 9
    if(list1.isEmpty()&&list2.isEmpty())return Integer.MIN_VALUE;
    if (list2.isEmpty()) {
    while (!list1.isEmpty()) {
    list2.push(list1.pop());
    }
    }
    return list2.pop();
    }

    public int peek() {
    if(list1.isEmpty()&&list2.isEmpty())return Integer.MIN_VALUE;
    if (list2.isEmpty()) {
    while (!list1.isEmpty()) {
    list2.push(list1.pop());
    }
    }
    return list2.peek();
    }

    public boolean empty() {
    return list1.isEmpty()&&list2.isEmpty();
    }
    }

/**

  • 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();
    */
  1. 用队列实现栈(MyStack)
    功能描述
    数据结构:
    使用一个队列(list1)来模拟栈的行为。
    通过循环将队列头部的元素移到尾部,实现类似栈的 LIFO 特性。
    方法实现:
    push(int x):
    将元素加入队列尾部。
    pop():
    通过循环将队列头部的元素移到尾部,直到最后一个元素到达头部,然后弹出。
    top():
    与 pop() 类似,但不移除元素,直接返回队列的最后一个元素。
    empty():
    检查队列是否为空。
    //225. 用队列实现栈
    class MyStack {
    LinkedList list1;
    public MyStack() {
    list1 = new LinkedList<>();
    }

    public void push(int x) {
    list1.offer(x);
    }

    public int pop() {
    if (list1.isEmpty()) {
    return Integer.MIN_VALUE;
    }
    int len = list1.size();
    for (int i = 0; i < len-1; i++) {
    list1.offer(list1.poll());
    }
    return list1.poll();
    }

    public int top() {
    if (list1.isEmpty()) {
    return Integer.MIN_VALUE;
    }
    int len = list1.size();
    for (int i = 0; i < len-1; i++) {
    list1.offer(list1.poll());
    }
    int res = Integer.MAX_VALUE;
    if (!list1.isEmpty()) {
    res=list1.peek();
    list1.offer(list1.poll());
    }
    return res;
    }

    public boolean empty() {
    return list1.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();
    */
posted @ 2025-01-23 23:43  123木头人-10086  阅读(40)  评论(0)    收藏  举报