您的位置:首页 > 其它

Cracking the coding interview--Q3.5

2014-11-12 14:30 323 查看
原文:

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());
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法