JAVA数据结构---循环队列
2016-03-14 17:50
567 查看
本程序循环队列使用数组实现的,
queueSize默认情况下的长度为10,初始化时候可以传递参数;
count表示队列中实际存入数据的多少;
头指针head和尾指针tail
代码分为三个文件:
定义接口CirQueInf.java
定义循环队列CirQue.java
测试主类MainTest.java
接口:
循环队列:
测试主类:
测试结果
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
相关文章推荐
- JAVA数据结构---循环队列
- JAVA数据结构---循环队列
- JAVA数据结构---循环队列
- HDFS源码分析心跳汇报之数据结构初始化
- 数据结构(5)线性表之链表C++实现带头结点的单链表合并
- 队列的数组实现(循环队列)
- hdu 1394(树状数组求逆序数)
- 数据结构与算法面试题80道(35)
- 数据结构与算法面试题80道(34)
- [数据结构-查询区间最小值小结(RMQ问题(Range Minimum Query))]
- 数据结构与算法面试题80道(33)
- [数据结构-树状数组小结]
- [数据结构-划分树小结]
- 数据结构与算法面试题80道(32)
- stl源码剖析学习笔记(一)重点数据结构概览
- 2.0新版本对数据结构进行了大幅修改:
- 数据结构与算法面试题80道(30)
- 堆排序(绝对详细)
- 栈的应用—平衡符号
- 数据结构与算法面试题80道(29)