您的位置:首页 > 理论基础 > 数据结构算法

JAVA数据结构---循环队列

2016-03-14 17:50 453 查看
本程序循环队列使用数组实现的,

queueSize默认情况下的长度为10,初始化时候可以传递参数;

count表示队列中实际存入数据的多少;

头指针head和尾指针tail

代码分为三个文件:

定义接口CirQueInf.java

定义循环队列CirQue.java

测试主类MainTest.java

接口:

public interface CirQueIntf<T> {
// 从尾入
public abstract void enQueue(T t);
// 从头部出
public abstract void deQueue();
// 循环队列元素的个数
public abstract int Count();
// 判断队列是否为空
public abstract boolean isEmpty();
// 判断队列是否满
public abstract boolean isFull();
// 打印队列
public abstract void printQueue();
}


循环队列:

public class CirQue<T> implements CirQueIntf<T> {
// 定义数据
T[] a;
private static final int DEFAULT_SIZE = 10;
int head;
int tail;
int count;
int queueSize;
// 构造函数
@SuppressWarnings("unchecked")
public CirQue() {
a = (T[]) new Object[DEFAULT_SIZE];
head = 0;
tail = 0;
count = 0;
queueSize = DEFAULT_SIZE;
}
@SuppressWarnings("unchecked")
public CirQue(int num) {
a = (T[]) new Object[num];
head = 0;
tail = 0;
count = 0;
queueSize = num;
}
// 接口实现
public void enQueue(T t) {
// 循环队列为空,起点不一定是0,需要变换
if (isEmpty() == true) {
a[tail] = t;
count++;
}
// 循环队列不为空,也没有满,可以装入元素,需要变换
else if (isFull() == false && isEmpty() == false) {
if (tail >= 0 && tail <= queueSize - 2) {
tail++;
a[tail] = t;
count++;
} else if (tail == queueSize - 1) {
a[0] = t;
tail = 0;
count++;
}
}
// 循环队列已经满了,不能装入元素
else if (isFull() == true)
System.out.println("循环队列已经满了,请先退出队列.");
}
// 退出队列
public void deQueue() {
// 循环队列为空
if (isEmpty() == true) {
System.out.println("队列为空,无法删除.");
}
// 循环队列不为空
else if (isEmpty() == false) {
if (head >= 0 && head <= queueSize - 2) {
head++;
count--;
} else if (head == queueSize - 1) {
head = 0;
count--;
}
}
}
// 队列里元素的个数
public int Count() {
System.out.println("当前队列的长度是:" + count);
return count;
}
// 队列是否为空
public boolean isEmpty() {
if (count == 0)
return true;
else
return false;
}
// 循环队列是否满了
public boolean isFull() {
if (count == queueSize)
return true;
else
return false;
}

// 打印循环队列
public void printQueue() {
if (isEmpty() == true) {
System.out.println("队列为空,没有元素");
} else if (isEmpty() == false) {
for (int i = head; i <= head + count - 1; i++) {
if (i <= queueSize - 1) {
System.out.print(a[i] + " ");
} else {
System.out.print(a[i % queueSize] + " ");
}
}
}
System.out.println();
System.out.println("长度" + count);
}
}


测试主类:

public class MainTest {
public static void main(String[] args) {

CirQue<String> cq = new CirQue<String>(5);
System.out.println("\n进行退队操作如下:");
cq.deQueue();
System.out.println("\n进行入队操作如下:");
cq.enQueue("111");
cq.printQueue();
cq.enQueue("222");
cq.printQueue();
cq.enQueue("333");
cq.printQueue();
cq.enQueue("444");
cq.printQueue();
cq.enQueue("555");
cq.printQueue();
cq.enQueue("666");
cq.printQueue();
System.out.println("\n进行退队操作如下:");
cq.deQueue();
cq.printQueue();
cq.deQueue();
cq.printQueue();
System.out.println("\n证明循环队列如下:");
cq.enQueue("7777");
// 7777加入时候,head=2,tail=0,实现了循环
cq.printQueue();
}
}


测试结果

进行退队操作如下:
队列为空,无法删除.

进行入队操作如下:
111
长度1
111 222
长度2
111 222 333
长度3
111 222 333 444
长度4
111 222 333 444 555
长度5
循环队列已经满了,请先退出队列.
111 222 333 444 555
长度5

进行退队操作如下:
222 333 444 555
长度4
333 444 555
长度3

证明循环队列如下:
333 444 555 7777
长度4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: