概述
最近在看数据结构与算法之类的书,看到喜欢的小东西随手记一下。
/**
* 简单循环数组构造队列结构
* @author Administrator@2018年12月12日 下午8:30:47
*/
public class ArrayQueue {
public int capacity;
public int[] arrayQueue;
public int front;
public int rear;
public ArrayQueue(int size) {
capacity = size;
arrayQueue = new int[size];
front = -1;
rear = -1;
}
/**
* 队列是否为空
* @return
* @author Administrator@2018年12月12日 下午8:33:59
*/
public boolean isEmpty() {
return (front == -1);
}
/**
* 判断队列是否已经满了
* @return
* @author Administrator@2018年12月12日 下午8:35:02
*/
public boolean isFull() {
return ((rear + 1)%capacity == front);
}
/**
* 删除队列首元素
* @return
* @author Administrator@2018年12月12日 下午8:36:33
*/
public int arrayPoll() {
if(isEmpty()) {
throw new NullPointerException("队列已经空了!");
}
int data = arrayQueue[front];
if(front == rear) {
front = rear = -1;
}else {
front = (front + 1) % capacity;
}
return data;
}
/**
* 元素入队
* @param element
* @return
* @author Administrator@2018年12月12日 下午8:37:09
*/
public int arrayOffer(int element) {
if(isFull()) {
throw new IndexOutOfBoundsException("队列已满!");
}
rear = (rear + 1) % capacity;
arrayQueue[rear] = element;
if(front == -1) {
front = rear;
}
return arrayQueue[front];
}
/**
* 获取队首元素
* @return
* @author Administrator@2018年12月12日 下午8:37:53
*/
public int arrayPeek() {
if(isEmpty()) {
throw new NullPointerException("队列已经空了!");
}
return arrayQueue[front];
}
/**
* 获取队列元素的个数
* @return
* @author Administrator@2018年12月12日 下午8:48:18
*/
public int arrayQueueCapicity() {
return (rear - front + 1 + capacity) % capacity;
}
}
最后
以上就是直率汽车为你收集整理的简单循环数组实现队列的全部内容,希望文章能够帮你解决简单循环数组实现队列所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复