数组实现队列
前言
面试遇到一道算法题: 使用一个数组来实现一个先进先出(FIFO)的队列。
思路
使用数组实现一个FIFO队列,很容易跟循环数组联系起来,首先使用两个指针来指向head、tail两个位置,也可以理解为first、last,假设数组的长度为n,数组下表从0开始,最后一个则为n-1,如果我们想让这个数组循环用起来的话,那么n-1后面就应该是0.
可能出现的情况
代码及测试代码
/**
* @desc :
* @author :tigermeng.
* @date :12:15 2019-04-22
*/
package com.algorithm;
import java.util.Random;
/**
* 通过数组实现先进先出队列
* @desc :数组
* @author :tigermeng.
* @date :12:15 2019-04-22
*
*/
public class FifoQueueByArray {
/**
* 默认队列大小
*/
private static final int DEFAULT_SIZE = 10;
private int capacitySize;
private String[] queue;
private int head;
private int tail;
private int realSize;
public FifoQueueByArray(int capacitySize) {
init(capacitySize);
}
private void init(int capacitySize) {
if (capacitySize <= 0) {
capacitySize = DEFAULT_SIZE;
}
if(capacitySize == 1){
this.capacitySize = capacitySize;
queue = new String[capacitySize];
head = tail = 0;
realSize = 0;
}
this.capacitySize = capacitySize;
queue = new String[capacitySize];
head = tail = 0;
realSize = 0;
}
//进入队列
public void put(String val) throws Exception {
if(isFull()){
throw new Exception("queue is full ,can't put ["+val+"]");
}
queue[tail] = val;
this.tail = (++this.tail >= this.capacitySize ? this.tail - this.capacitySize : this.tail );
incrementSize();
}
private void incrementSize() {
this.realSize ++ ;
if(this.realSize >= this.capacitySize){
this.realSize = this.capacitySize;
} else {
this.realSize = tail - head > 0 ? tail - head : head-tail;
}
}
/**
* 判断队满
* @return
*/
public boolean isFull(){
return this.tail == this.head && queue[head] != null;
}
/**
* 判断队空
* @return
*/
public boolean isEmpty(){
return this.tail == this.head && queue[tail]== null;
}
//取出队列
public String get() throws Exception {
if (isEmpty()){
throw new Exception("the queue is empty!");
}
String headVal = queue[head];
//清空
queue[head] = null;
this.head = (++this.head >= this.capacitySize ? this.head - this.capacitySize : this.head );
this.realSize = tail - head > 0 ? tail - head : head-tail;
return headVal;
}
//get the count of this queue
public int getCount() {
int count = this.tail - this.head;
return count < 0 ? count + this.capacitySize : count;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(this.getClass().getSimpleName()).append(":");
for (int i= 0; i < this.capacitySize; i++) {
sb.append(" ").append(queue[i] == null ? " @":queue[i]);
}
return sb.toString();
}
public static void main(String[] args){
//模拟多次出队及入队情况
FifoQueueByArray queue = new FifoQueueByArray(10);
System.out.println("=======before==============");
System.out.println(queue);
for (int i = 0; i < 3; i++) {
randomest(queue);
}
}
private static void randomest(FifoQueueByArray queue){
Random random = new Random();
//入
int in = random.nextInt(15);
System.out.println("in====="+in);
for (int i = 0; i < in; i++) {
try {
queue.put(" V");
} catch (Exception e) {
System.out.println("满了"+e);
break;
}
System.out.println(queue);
}
System.out.println("=====================");
System.out.println("after in queue size " + queue.getCount());
//出
int out = random.nextInt(15);
System.out.println("out====="+out);
for (int i = 0; i < out; i++) {
try {
queue.get();
} catch (Exception e) {
System.out.println("空了 " + e);
break;
}
System.out.println(queue);
}
System.out.println("=====================");
System.out.println("after out queue size " + queue.getCount());
}
}
测试结果
=======before==============
FifoQueueByArray: @ @ @ @ @ @ @ @ @ @
in=====4
FifoQueueByArray: V @ @ @ @ @ @ @ @ @
FifoQueueByArray: V V @ @ @ @ @ @ @ @
FifoQueueByArray: V V V @ @ @ @ @ @ @
FifoQueueByArray: V V V V @ @ @ @ @ @
=====================
after in queue size 4
out=====9
FifoQueueByArray: @ V V V @ @ @ @ @ @
FifoQueueByArray: @ @ V V @ @ @ @ @ @
FifoQueueByArray: @ @ @ V @ @ @ @ @ @
FifoQueueByArray: @ @ @ @ @ @ @ @ @ @
空了 java.lang.Exception: the queue is empty!
=====================
after out queue size 0
in=====3
FifoQueueByArray: @ @ @ @ V @ @ @ @ @
FifoQueueByArray: @ @ @ @ V V @ @ @ @
FifoQueueByArray: @ @ @ @ V V V @ @ @
=====================
after in queue size 3
out=====11
FifoQueueByArray: @ @ @ @ @ V V @ @ @
FifoQueueByArray: @ @ @ @ @ @ V @ @ @
FifoQueueByArray: @ @ @ @ @ @ @ @ @ @
空了 java.lang.Exception: the queue is empty!
=====================
after out queue size 0
in=====6
FifoQueueByArray: @ @ @ @ @ @ @ V @ @
FifoQueueByArray: @ @ @ @ @ @ @ V V @
FifoQueueByArray: @ @ @ @ @ @ @ V V V
FifoQueueByArray: V @ @ @ @ @ @ V V V
FifoQueueByArray: V V @ @ @ @ @ V V V
FifoQueueByArray: V V V @ @ @ @ V V V
=====================
after in queue size 6
out=====13
FifoQueueByArray: V V V @ @ @ @ @ V V
FifoQueueByArray: V V V @ @ @ @ @ @ V
FifoQueueByArray: V V V @ @ @ @ @ @ @
FifoQueueByArray: @ V V @ @ @ @ @ @ @
FifoQueueByArray: @ @ V @ @ @ @ @ @ @
FifoQueueByArray: @ @ @ @ @ @ @ @ @ @
空了 java.lang.Exception: the queue is empty!
=====================
after out queue size 0
Process finished with exit code 0