package com.queue;import java.util.LinkedList;import java.util.Queue;/**
* Description: 两个队列实现一个栈
* Author: Administrator
* Date: 2019/5/28 0028
* Time: 11:01
*/publicclassTestMyStack{private Queue<Integer> queue1;private Queue<Integer> queue2;privateint usedSize;/** Initialize your data structure here. */publicTestMyStack(){this.queue1 =newLinkedList<>();this.queue2 =newLinkedList<>();this.usedSize =0;}/** Push element x onto stack. *///入到不为空的队列publicvoidpush(int x){if(!queue1.isEmpty()){
queue1.add(x);}elseif(!queue2.isEmpty()){
queue2.add(x);}else{
queue1.add(x);}this.usedSize++;}/** Removes the element on top of the stack and returns that element. *///从不为空的队列中删除publicintpop(){int data =0;if(!queue1.isEmpty()){for(int i =1; i <= usedSize-1; i++){
queue2.add(queue1.poll());}
data = queue1.poll();}else{for(int i =1; i <= usedSize-1; i++){
queue1.add(queue2.poll());}
data = queue2.poll();}this.usedSize--;return data;}/** Get the top element. */publicinttop(){if(empty()){thrownewUnsupportedOperationException("无数据");}int data =0;if(!queue1.isEmpty()){for(int i =0; i <this.usedSize; i++){
data = queue1.poll();
queue2.add(data);}}else{for(int i =0; i <this.usedSize; i++){
data = queue2.poll();
queue1.add(data);}}return data;}/** Returns whether the stack is empty. */publicbooleanempty(){returnthis.usedSize ==0;}}