《剑指offer》练习-面试题9-用两个栈实现队列

题目:用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

链接:https://www.nowcoder.com/questionTerminal/54275ddae22f475981afa2244dd448c6

思路

stack1负责添加元素,将stack1中添加的元素压入stack2,stack2负责弹出元素。在弹出元素的时候,考虑①stack2不为空,直接弹出栈顶元素。②stack2为空,将stack1中的元素压入,再弹出。

package offer;

import java.util.Stack;

public class Solution9 {

	Stack<Integer> stack1 = new Stack<Integer>();
	Stack<Integer> stack2 = new Stack<Integer>();

	// 队列的数据压入过程
	public void push(Integer element) {
		stack1.push(element);
	}

	// 队列的数据弹出过程
	public Integer pop() {
		if (stack2.size() <= 0) { // 第二个栈为空
			while (stack1.size() > 0) { // 第一个栈不为空
				stack2.push(stack1.pop()); // 将第一个栈的数据亚茹第二个栈中
			}
		}

		if (stack2.isEmpty()) {
			try {
				throw new Exception("queue is empty");
			} catch (Exception e) {
				// e.printStackTrace();
			}
		}

		Integer head = stack2.pop();
		return head;
	}

	public static void main(String[] args) {
		Solution9 sq = new Solution9();
		sq.push(1);
		sq.push(3);
		sq.push(5);
		sq.push(4);
		sq.push(2);

		System.out.println(sq.pop());
		System.out.println(sq.pop());

		sq.push(7);
		System.out.println(sq.pop());
	}
} 

题目:用两个队列实现一个栈

思路

(1)压入数据时,①当两个队列都为空时,优先选择queue1,如(a);②queue1为空,queue2不为空时,直接放入queue2,如(b);③queue1不为空,queue2为空,直接放入queue1;

(2)弹出数据时,①两个队列都为空,则没有数据可以弹出;②queue1为空,queue2不为空,将queue2中的元素一次放入queue1中,知道遇到最后一个元素,直接弹出即可;③queue1不为空,queue2为空,同②。

package offer;

import java.util.ArrayDeque;
import java.util.Queue;

public class Solution91 {

	Queue<Integer> queue1 = new ArrayDeque<Integer>();
	Queue<Integer> queue2 = new ArrayDeque<Integer>();

	// 向栈中压入数据
	public void push(Integer element) {
		// 两个队列都为空时,优先考虑queue1
		if (queue1.isEmpty() && queue2.isEmpty()) {
			queue1.add(element);
			return;
		}

		// 如果queue1为空,queue2有数据,直接放入queue2
		if (queue1.isEmpty()) {
			queue2.add(element);
			return;
		}

		// 如果queue2为空,queue1有数据,直接放入queue1中
		if (queue2.isEmpty()) {
			queue1.add(element);
			return;
		}
	}

	// 从栈中弹出一个数据
	public Integer pop() {
		// 如果两个队列都为空,则没有元素可以弹出,异常
		if (queue1.isEmpty() && queue2.isEmpty()) {
			try {
				throw new Exception("stack is empty!");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		// 如果queue1没有元素,queue2有元素,将queue2中的元素依次放入queue1中,直到最后一个元素弹出即可
		if (queue1.isEmpty()) {
			while (queue2.size() > 1) {
				queue1.add(queue2.poll());
			}
			return queue2.poll();
		}

		// 如果queue2中没有元素,queue1中有元素,将queue1中的元素一次放入queue2中,直到最后一个元素弹出即可
		if (queue2.isEmpty()) {
			while (queue1.size() > 1) {
				queue2.add(queue1.poll());
			}
			return queue1.poll();
		}

		return (Integer) null;
	}

	public static void main(String[] args) {
		Solution91 qs = new Solution91();
		qs.push(2);
		qs.push(4);
		qs.push(7);
		qs.push(5);
		System.out.println(qs.pop());
		System.out.println(qs.pop());

		qs.push(1);
		System.out.println(qs.pop());
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值