栈和队列的常见面试题

1.1. 使用栈实现队列结构

思路

我们可以使用两个栈,一个起名为 pushStack 一个 popStack,在添加元素时,使用 pushStack来添加元素,当需要取出元素时,若 popStack中无数据,将 pushStack中所有数据全部弹出到popStack中,然后弹出popStack中的第一个元素,若 popStack中有数据,则弹出时调用popStack栈中的数据弹出。

注意
保证每次转移数据 popStack为空而且pushStack要全部弹出到popStack
 
package com.zhang.study.chapter03;

import java.util.Stack;

/**
 * 使用栈结构实现队列
 */
public class Code09_StackImplQueue {



    public static class MyQueue<T> {
        Stack<T> pushStack;
        Stack<T> popStack;

        public MyQueue() {
            this.pushStack = new Stack<>();
            this.popStack = new Stack<>();
        }

        public void pushToPop(){
            if(popStack.isEmpty()){
                while(!pushStack.isEmpty()){
                    popStack.push(pushStack.pop());
                }
            }
        }


        public void push(T item) {
            pushStack.push(item);
            pushToPop();
        }

        public T pop() {
            if (popStack.empty() && pushStack.empty()) {
                throw new RuntimeException("Queue is empty!");
            }
            pushToPop();
            return popStack.pop();
        }

        public T peek() {
            if (popStack.empty() && pushStack.empty()) {
                throw new RuntimeException("Queue is empty!");
            }
            pushToPop();
            return popStack.peek();
        }

    }

    public static void main(String[] args) {
        MyQueue<Integer> queue = new MyQueue<>();
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
        System.out.println(queue.pop());
    }

}

使用队列实现栈结构

思路

使用两个队列来反复横跳,一个正常的 queue一个帮助队列 helpQueue 在添加数据时,使用queue添加数据,需要取数据时,将数据移到 helpQueue 留下最后一个返回。然后交换 queuehelpQueue引用地址。

package com.zhang.study.chapter03;

import com.sun.jmx.remote.internal.ArrayQueue;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * 使用队列实现栈结构
 * 使用两个队列来反复横跳,一个正常的 queue一个帮助队列 helpQueue 在添加数据时,
 * 使用queue添加数据,需要取数据时,将数据移到 helpQueue 留下最后一个返回。然后交换 queue和 helpQueue引用地址。
 */
public class Code10_QueueImplStack {

    public static class QueueImplStack<T> {
        public Queue<T> queue;
        public Queue<T> help;

        public QueueImplStack() {
            queue = new LinkedList<T>();
            help = new LinkedList<T>();
        }

        public void push(T t) {
            queue.add(t);
        }

        public T poll() {
            if (queue.isEmpty()) {
                throw new RuntimeException("Queue is empty!");
            }
            while (queue.size() > 1) {
                help.offer(queue.poll());
            }
            T poll = queue.poll();

            Queue<T> temp = help;
            help = queue;
            queue = temp;
            return poll;
        }

        public boolean isEmpty() {
            return queue.isEmpty();
        }
        public T peek() {
            if (queue.isEmpty()) {
                throw new RuntimeException("Queue is empty!");
            }
            while (queue.size() > 1) {
                help.offer(queue.poll());
            }
            T ans = queue.poll();
            help.offer(ans);
            Queue<T> tmp = queue;
            queue = help;
            help = tmp;
            return ans;
        }


    }

    public static void main(String[] args) {
        System.out.println("test begin");
        QueueImplStack<Integer> myStack = new QueueImplStack<>();
        Stack<Integer> test = new Stack<>();
        int testTime = 1000000;
        int max = 1000000;
        for (int i = 0; i < testTime; i++) {
            if (myStack.isEmpty()) {
                if (!test.isEmpty()) {
                    System.out.println("Oops");
                }
                int num = (int) (Math.random() * max);
                myStack.push(num);
                test.push(num);
            } else {
                if (Math.random() < 0.25) {
                    int num = (int) (Math.random() * max);
                    myStack.push(num);
                    test.push(num);
                } else if (Math.random() < 0.5) {
                    if (!myStack.peek().equals(test.peek())) {
                        System.out.println("Oops");
                    }
                } else if (Math.random() < 0.75) {
                    if (!myStack.poll().equals(test.pop())) {
                        System.out.println("Oops");
                    }
                } else {
                    if (myStack.isEmpty() != test.isEmpty()) {
                        System.out.println("Oops");
                    }
                }
            }
        }

        System.out.println("test finish!");
    }
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值