代码随想录算法训练营day10_232.用栈实现队列、225. 用队列实现栈

Java中的栈和队列

栈的实现

使用Java的集合类Stack

  1. boolean isEmpty();//判断当前栈是否为空,等价于empty();
  2. synchronized E peek();//获得当前栈顶元素
  3. Synchronized E pop();//获得当前栈顶元素并删除
  4. E push(E object);//将元素加入栈顶
  5. Synchronized int search(Object o);//查找元素在栈中的位置,由栈底向栈顶方向数

Stack<E> stack = new Stack<>();
LinkedList<E> stack = new LinkedList<>();

队列的实现

使用add和remove再失败的时候会抛出异常所以使用offer和poll

  1. 队列方法:offer(e); 等效方法:offer(e) / offerLast(e);//向队尾添加元素
  2. 队列方法:poll(e); 等效方法:poll(e) / pollFirst(e);//获取队首元素并删除
  3. 队列方法:peek(e); 等效方法:peek(e) / peeFirst(e);//向获取队首元素

Queue<E> queue = new LinkedList<>();

232.用栈实现队列

题目链接
思路:
1.由于栈的进出原则为LIFO,所以需要连个栈来改变成FIFO
2.push方法的实现:直接压入stack1
3.pop和peek实现类似,只有返回操作不一样
4.首先判断stack2是否为空,如果不为空直接返回
5.如果为空,如果stack1不为空,就使用while循环将stack1中的元素全部放入stack2实现顺序的改变
注意:
在实现pop和peek方法的时候,一定要注意先判断stack2还有没有元素,应该被先返回

class MyQueue {


    Stack<Integer> stack1;
    Stack<Integer> stack2;


    public MyQueue() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }
    
    public void push(int x) {
        stack1.push(x);
    }
    
    public int pop() {
        if (!stack2.isEmpty()) {
            return stack2.pop();
        }
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
        return stack2.pop();
    }
    
    public int peek() {
        if (!stack2.isEmpty()) {
            return stack2.peek();
        }
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
        return stack2.peek();
    }
    
    public boolean empty() {
        if (stack1.isEmpty() && stack2.isEmpty()) {
            return true;
        }
        return false;
    }
}

225. 用队列实现栈

题目链接
思路:
我使用了一个双端队列Deque直接就实现了,很疑惑

class MyStack {


    Deque<Integer> queue;


    public MyStack() {
        queue = new LinkedList<>();
    }
    
    public void push(int x) {
        queue.addLast(x);
    }
    
    public int pop() {
        return queue.pollLast();
    }
    
    public int top() {
        return queue.peekLast();
    }
    
    public boolean empty() {
        return queue.isEmpty();
    }
}

使用Queue来实现

class MyStack {

    Queue<Integer> queue;

    public MyStack() {
        queue = new LinkedList<>();
    }

    //每 offer 一个数(A)进来,都重新排列,把这个数(A)放到队列的队首
    public void push(int x) {
        queue.offer(x);
        int size = queue.size();
        //移动除了 A 的其它数
        while (size-- > 1)
            queue.offer(queue.poll());
    }

    public int pop() {
        return queue.poll();
    }

    public int top() {
        return queue.peek();
    }

    public boolean empty() {
        return queue.isEmpty();
    }
}

使用两个Queue实现

class MyStack {

    Queue<Integer> queue1; // 和栈中保持一样元素的队列
    Queue<Integer> queue2; // 辅助队列

    /** Initialize your data structure here. */
    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }
    
    /** Push element x onto stack. */
    public void push(int x) {
        queue2.offer(x); // 先放在辅助队列中
        while (!queue1.isEmpty()){
            queue2.offer(queue1.poll());
        }
        Queue<Integer> queueTemp;
        queueTemp = queue1;
        queue1 = queue2;
        queue2 = queueTemp; // 最后交换queue1和queue2,将元素都放到queue1中
    }
    
    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queue1.poll(); // 因为queue1中的元素和栈中的保持一致,所以这个和下面两个的操作只看queue1即可
    }
    
    /** Get the top element. */
    public int top() {
        return queue1.peek();
    }
    
    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue1.isEmpty();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小C卷Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值