Cracking the coding interview--Q3.5

原文:

Implement a MyQueue class which implements a queue using two stacks.

译文:

使用两个栈实现一个队列MyQueue。


队列是先进先出的数据结构(FIFO),栈是先进后出的数据结构(FILO), 用两个栈来实现队列的最简单方式是:进入队列则往第一个栈压栈, 出队列则将第一个栈的数据依次压入第二个栈,然后出栈。此时第二个栈顶元素即为队头元素,然后可以把队2元素再次押回栈1.

package chapter_3_StacksandQueues;

import java.util.Scanner;

class Stack_3_5 {
	private int top;
	private int size;
	private int[] data;
	
	public Stack_3_5(int size) {
		this.size = size;
		this.top = -1;
		this.data = new int[size];
	}
	
	public boolean isEmpty() {
		return top < 0;
	}
	
	public boolean isFull() {
		return top == (size-1);
	}
	
	public void push(int value) {
		if(isFull()) {
			System.out.println("Stack is full!");
			return;
		}
		data[++top] = value;
	}
	
	public void pop() {
		if(isEmpty()) {
			System.out.println("Stack is empty!");
			return;
		}
		
		top --;
	}
	
	public int top() {
		if(isEmpty()) {
			System.out.println("Stack is empty!");
			return -1;
		}
		return data[top];
	}
}

class MyQueue {
	private Stack_3_5 stack1, stack2;
	private int size;
	
	public MyQueue(int size) {
		stack1 = new Stack_3_5(size);
		stack2 = new Stack_3_5(size);
		this.size = 0;
	}
	
	// 入队,则直接在数据元素栈入栈元素即可
	public void inQueue(int value) {
		if(stack1.isFull()) {
			System.out.println("MyQueue is full!");
			return;
		}
		stack1.push(value);
		size ++;
	}
	
	// 元素出队,则需要把数据元素栈内元素依次出栈存入另一个栈内,
	// 借助另一个栈获取队头元素
	public int deQueue() {
		if(stack1.isEmpty()) {
			System.out.println("MyQueue is empty!");
			return -1;
		}
		
		while(!stack1.isEmpty()) {
			int value = stack1.top();
			stack1.pop();
			stack2.push(value);
		}
		int result = stack2.top();
		stack2.pop();
		size --;
		while(!stack2.isEmpty()) {
			int value = stack2.top();
			stack2.pop();
			stack1.push(value);
		}
		return result;
	}
}

/**
 *
 * 使用两个栈实现一个队列MyQueue
 *
 */
public class Question_3_5 {
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int testNum = scanner.nextInt();
		scanner.nextLine();
		while(testNum -- > 0) {
			MyQueue queue = new MyQueue(5);
			
			String str = scanner.nextLine();
			String strs[] = str.split(" ");
			
			for(int i=0; i< 6; i++) {
				queue.inQueue(Integer.parseInt(strs[i]));
			}
			
			for(int i=0; i<3; i++) {
				int data = queue.deQueue();
				if(data != -1) {
					System.out.println("出队元素: " + data);
				}	
			}
			
			for(int i=6; i< strs.length; i++) {
				queue.inQueue(Integer.parseInt(strs[i]));
			}
		}
	}
}


上面的方法则每次获取队首元素都需要翻转整一个栈1,使得效率很低。

可以使用如下方法:

元素入队,则直接压入栈1,不用管栈2,如果元素出对,则首先判断栈2是否为空,如果为空,则栈1元素一次入栈压入栈2.,如果栈2不为空,则此时

获取栈2栈顶元素,因为此时栈2栈顶元素即为队首元素。这样没入入队和出对效率就增高,并且如果出对入队频繁,则队列的长度存储空间也会增加,

栈1和栈2加起来即为队列的长度,对首到中间的数据在栈2 中存储, 中间到对尾数据存储在栈1中。

package chapter_3_StacksandQueues;

import java.util.Scanner;

class MyQueue_3_5_2 {
	private Stack_3_5 stack1, stack2;
	private int size;
	
	public MyQueue_3_5_2(int size) {
		stack1 = new Stack_3_5(size);
		stack2 = new Stack_3_5(size);
		this.size = 0;
	}
	
	public boolean isEmpty() {
		return size == 0;
	}
	
	// 入队,则直接在数据元素栈入栈元素即可
	public void inQueue(int value) {
		if(stack1.isFull()) {
			System.out.println("MyQueue is full!");
			return;
		}
		stack1.push(value);
		size ++;
	}
	
	// 出栈则先获取第二个栈内数据,为空则第一个栈全部出栈并入栈第二个栈
	// 获取第二个栈顶元素,此时第二个栈顶元素就是队首元素
	public int deQueue() {
		if(size == 0) {
			System.out.println("MyQueue is empty!");
			return -1;
		}
		
		if(stack2.isEmpty()) {
			while(!stack1.isEmpty()) {
				int value = stack1.top();
				stack1.pop();
				stack2.push(value);
			}
		} 
		
		int result = stack2.top();
		stack2.pop();
		size --;
		return result;
	}
}

/**
*
* 使用两个栈实现一个队列MyQueue
*
*/
public class Question_3_5_2 {
	public static void main(String args[]) {
		Scanner scanner = new Scanner(System.in);
		int testNum = scanner.nextInt();
		scanner.nextLine();
		while(testNum -- > 0) {
			MyQueue_3_5_2 queue = new MyQueue_3_5_2(5);
			
			String str = scanner.nextLine();
			String strs[] = str.split(" ");
			
			for(int i=0; i< 6; i++) {
				queue.inQueue(Integer.parseInt(strs[i]));
			}
			
			for(int i=0; i<3; i++) {
				int data = queue.deQueue();
				if(data != -1) {
					System.out.println("出队元素: " + data);
				}	
			}
			
			for(int i=5; i< strs.length; i++) {
				queue.inQueue(Integer.parseInt(strs[i]));
			}
			
			while(!queue.isEmpty()) {
				System.out.println("出队元素: " + queue.deQueue());
			}
		}
	}
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值