栈队列互相转换

两个 队列 实现 栈:

 

队列结构是先入先出,用两个队列结构,数组从队列1到队列2,元素顺序不会变。

栈结构是先入后出。即每次出栈的内容都是将队尾弹出,所以准备两个队列,

主队列 一个是加入数据的时候进入的队列(用来入栈)

辅队列 一个是弹出数据的时候(弹出队尾数据)所需队列。

弹出数据:将队列1 中的数据依次加入队列2,直到队列1中只剩一个数据为止。此时剩余的数据便是栈需要弹出的数据,将这个数据弹出即可)为了节省时间来回入队出队时间,此时将队列2改为主队列,数据中的元素位置并没有任何改变。

总结:push函数直接将数存入主队列即可,pop函数则需要将队列1中的数转移至队列2,弹出队尾,然后切换主辅队列。

代码:实现一个类,此类中有peek,pop,push函数

两个 栈 实现 队列:

栈push用来入数据,栈pop用来出数据.

push时,数据直接进入栈push。

出数据:先异常判断,两个栈都为空,抛出异常。当pop栈为空时,把栈1的数据倒入栈2,一次性倒完。不为空时,直接从pop中出数据(此时不可以向pop栈倒数据,否则会造成数据顺序混乱。)

注意:倒数据时,只有pop栈里已经没有数据时才可以把数据倒入pop栈内,并且在倒的过程中要把push栈的全部数据倒入

代码:实现一个类,此类中有peek,poll,push函数

package Queue;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

//队列实现栈
public class Convert {
	public static class TwoQueueStack {
		Queue<Integer> queue;
		Queue<Integer> help;
		public TwoQueueStack() {
			queue = new LinkedList<Integer>();
			help = new LinkedList<Integer>();
		}
		//push
		public void push(Integer item) {
			queue.add(item);
		}
		//peek
		public Integer peek() {
			if (queue.isEmpty()) {
				throw new RuntimeException("Stack is empty!");
			}
			while (queue.size() > 1) {
				help.add(queue.poll());
			}
			Integer ans = queue.peek();
			help.add(queue.poll());
			swap();
			return ans;
		}
		//pop
		public Integer pop() {
			if (queue.isEmpty()) {
				throw new RuntimeException("Stack is empty!");
			}
			while (queue.size() > 1) {
				help.add(queue.poll());
			}
			Integer ans = queue.poll();
			swap();
			return ans;
		}
		//此函数不要加入参数传递,不然不会成功交换
		private void swap() {
			// TODO Auto-generated method stub
			Queue<Integer> t;
			t = queue;
			queue = help;
			help = t;
		}
	}

//栈实现队列
//不存在交换
	public static class TwoStackQueue{
		Stack <Integer> pushSt;
		Stack<Integer> popSt;
		TwoStackQueue(){
			pushSt=new Stack<Integer>();
			popSt=new Stack<Integer>();
		}
		public void push(Integer item) {
			pushSt.push(item);
		}
		public Integer peek() {
			//如果都为空,抛出异常
			if(pushSt.isEmpty()&&popSt.isEmpty()){
				throw new RuntimeException("the queue is empty");
			}else if(popSt.isEmpty()) {
				while(!pushSt.isEmpty()) {
					popSt.push(pushSt.pop());//倒数据
				}	
			}
			return popSt.peek();
		}
		public Integer poll() {
			//如果都为空,抛出异常
			if(pushSt.isEmpty()&&popSt.isEmpty()){
				throw new RuntimeException("the queue is empty");
			}else if(popSt.isEmpty()) {
				while(!pushSt.isEmpty()) {
					popSt.push(pushSt.pop());//倒数据
				}
			}
			return popSt.pop();
		}
	}
	public static void main(String[] args) {
		TwoQueueStack b = new TwoQueueStack();
		//TwoStackQueue b = new TwoStackQueue();
		b.push(6);
		b.push(3);
		System.out.println(b.pop());
		System.out.println(b.pop());
		b.push(5);
		b.push(4);
		b.push(9);
		b.push(11);
		System.out.println(b.pop());
		System.out.println(b.pop());
		System.out.println(b.peek());
		System.out.println(b.pop());
		b.push(2);
		b.push(1);
		System.out.println(b.pop());
		System.out.println(b.pop());
		System.out.println(b.peek());
		System.out.println(b.pop());
		//System.out.println(b.peek());
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值