仅使用栈结构实现队列结构

import java.util.Stack;

/**
 * 使用栈结构实现队列结构
 * @author Colin
 *
 */
public class QueqeByStack {
	private Stack<Integer> data;
	private Stack<Integer> help;
	
	public QueqeByStack(){
		data=new Stack<Integer>();
		help=new Stack<Integer>();
	}
	
	public void push(Integer num){
		data.push(num);
	}
	
	public Integer pop(){
		while(!data.isEmpty()){
			help.push(data.pop());
		}
		int value=help.pop();
		while(!help.isEmpty()){
			data.push(help.pop());
		}
		return value;
	}
	
	public Integer peek(){
		while(!data.isEmpty()){
			help.push(data.pop());
		}
		int value=help.peek();
		while(!help.isEmpty()){
			data.push(help.pop());
		}
		return value;
	}

猜你喜欢

转载自blog.csdn.net/qq_42667028/article/details/86667675