import java.util.LinkedList;import java.util.Queue;publicclassMyStack{private Queue<Integer> que =newLinkedList<>();/** Initialize your data structure here. */publicMyStack(){}/** Push element x onto stack. */publicvoidpush(int x){
que.add(x);}/** Removes the element on top of the stack and returns that element. */publicintpop(){//出栈顶元素//将链表里的前n-1个元素转移到后方(移除再加就变到了后面)for(int i =0;i < que.size()-2;i++){
que.add(que.remove());}return que.remove();}/** Get the top element. */publicinttop(){//这里和pop的区别就是保存栈顶元素for(int i =0;i < que.size()-2;i++){
que.add(que.remove());}int result = que.remove();
que.add(result);return result;}/** Returns whether the stack is empty. */publicbooleanempty(){if(que.size()<=0){returntrue;}else{returnfalse;}}}
利用LinkedList实现一个简单的栈import java.util.LinkedList;import java.util.Queue;public class MyStack { private Queue<Integer> que = new LinkedList<>(); /** Initialize your data structur...