package com.example;import java.util.LinkedList;import java.util.Queue;publicclassMyStack{/** Initialize your data structure here. */private Queue <Integer> queue;publicMyStack(){
queue=newLinkedList<Integer>();}/** Push element x onto stack. */publicvoidpush(int x){
queue.add(x);}/** Removes the element on top of the stack and returns that element. */publicintpop(){int size=queue.size();for(int i =0; i <size-1; i++){
Integer e=queue.remove();
queue.add(e);}return queue.remove();}/** Get the top element. */publicinttop(){int siez=queue.size();for(int i =0; i <siez-1; i++){
Integer e =queue.remove();
queue.add(e);}
Integer temp=queue.remove();
queue.add(temp);return temp;}/** Returns whether the stack is empty. */publicbooleanempty(){return queue.isEmpty();}}
用队列实现栈题目解题思路代码示例package com.example;import java.util.LinkedList;import java.util.Queue;public class MyStack { /** Initialize your data structure here. */ private Queue <Integer> queue; public MyStack() { queue= new LinkedList