欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。...

发布时间:2024/4/17 45 豆豆
生活随笔 收集整理的这篇文章主要介绍了 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目描述

用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 import java.util.Stack; public class Solution {     Stack<Integer> stack1 = new Stack<Integer>();     Stack<Integer> stack2 = new Stack<Integer>();     public void push(int node) {         int temp = 0;         if (stack1.isEmpty()) {             stack1.push(node);             return;         }         while(!stack1.isEmpty()){             temp=stack1.pop();             stack2.push(temp);         }         stack1.push(node);         while(!stack2.isEmpty()){             temp = stack2.pop();             stack1.push(temp);         }             }     public int pop() {        if(stack1.isEmpty()){              throw new RuntimeException("");        }         return stack1.pop();     } }

转载于:https://www.cnblogs.com/bb3q/p/5084058.html

总结

以上是生活随笔为你收集整理的用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。