用两个栈来实现一个队列,完成队列的Push和Pop操作。

题目描述

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

先简单介绍下栈和队列

栈:栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表。它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出来)。栈具有记忆作用,对栈的插入与删除操作中,不需要改变栈底指针。(先进后出or后进先出)

队列:队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。(先进先出)

实现方式1

package com.jason.algorithm;

import java.util.Stack;

public class QueueImplmentByStatck {

	public static void main(String[] args) {
		QueueImplmentByStatck queueImplmentByStatck = new QueueImplmentByStatck();
		queueImplmentByStatck.push(1);
		queueImplmentByStatck.push(2);
		queueImplmentByStatck.push(3);
		System.out.println(queueImplmentByStatck.pop());
		System.out.println(queueImplmentByStatck.pop());
		queueImplmentByStatck.push(4);
		System.out.println(queueImplmentByStatck.pop());
		queueImplmentByStatck.push(5);
		System.out.println(queueImplmentByStatck.pop());
		System.out.println(queueImplmentByStatck.pop());
	}

	private int count = 0;
	// statck先进后出,Queue先进先出
	Stack<Integer> stack1 = new Stack<Integer>();
	Stack<Integer> stack2 = new Stack<Integer>();

	public void push(int node) {
		stack1.push(node);
	}

	public int pop() {
		stack2.push(count);
		count++;
		return stack1.get(stack2.pop());
	}
}
实现方式2

package com.jason.algorithm;

import java.util.Stack;

public class QueueImplmentByStatck1 {

	public static void main(String[] args) {
		QueueImplmentByStatck1 queueImplmentByStatck = new QueueImplmentByStatck1();
		queueImplmentByStatck.push(1);
		queueImplmentByStatck.push(2);
		queueImplmentByStatck.push(3);
		System.out.println(queueImplmentByStatck.pop());
		System.out.println(queueImplmentByStatck.pop());
		queueImplmentByStatck.push(4);
		System.out.println(queueImplmentByStatck.pop());
		queueImplmentByStatck.push(5);
		System.out.println(queueImplmentByStatck.pop());
		System.out.println(queueImplmentByStatck.pop());
	}

	// statck先进后出,Queue先进先出
	Stack<Integer> stack1 = new Stack<Integer>();
	Stack<Integer> stack2 = new Stack<Integer>();

	public void push(int node) {
		stack1.push(node);
	}

	public int pop() {
		// 当stack1不为null时,全部放到stack2里这时,stack2里就是和之前的stack1顺序刚好是反的
		while (!stack1.isEmpty()) {
			stack2.push(stack1.pop());
		}
		int first = stack2.pop();
		while (!stack2.isEmpty()) {
			stack1.push(stack2.pop());
		}
		return first;
	}

}
实现方式3

package com.jason.algorithm;

import java.util.Stack;

public class QueueImplmentByStatck2 {

	public static void main(String[] args) {
		QueueImplmentByStatck2 queueImplmentByStatck = new QueueImplmentByStatck2();
		queueImplmentByStatck.push(1);
		queueImplmentByStatck.push(2);
		queueImplmentByStatck.push(3);
		System.out.println(queueImplmentByStatck.pop());
		System.out.println(queueImplmentByStatck.pop());
		queueImplmentByStatck.push(4);
		System.out.println(queueImplmentByStatck.pop());
		queueImplmentByStatck.push(5);
		System.out.println(queueImplmentByStatck.pop());
		System.out.println(queueImplmentByStatck.pop());
	}

	// statck先进后出,Queue先进先出
	Stack<Integer> stack1 = new Stack<Integer>();
	Stack<Integer> stack2 = new Stack<Integer>();

	public void push(int node) {
		stack1.push(node);
	}

	public int pop() {
		if (stack1.empty() && stack2.empty()) {
			throw new RuntimeException("Queue is empty!");
		}
		// 每次stack2为空时,再去把stack1里内容放stack2
		if (stack2.empty()) {
			while (!stack1.empty()) {
				stack2.push(stack1.pop());
			}
		}
		return stack2.pop();
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值