package com.loong.datastructure;
/**
* 学习队列结构
*
* @author Loong
*
*/
public class Queue {
private Object[] item = new Object[100];
private int front;// 对头
private int rear;// 队尾
private int count;
public Queue() {
}
/**
* 初始化队列
*/
public void initQueue() {
front = rear = count = 0;
}
/**
* 判断队列是否为空
*
* @return
*/
public boolean isQueueEmpty() {
return count == 0;
}
/**
* 判断队列是否已满
*
* @return
*/
public boolean isQueueFull() {
return count == item.length;
}
/**
* 进队列
*
* @param o
*/
public void enQueue(Object o) {
if (isQueueFull()) {
throw new RuntimeException("队列已满");
}
if (rear >= this.item.length) {
if (count < this.item.length) {
rear = 0;
}
}
item[rear++] = o;
count++;
}
/**
* 获取队首元素
*
* @return
*/
public Object getFront() {
if (isQueueEmpty()) {
throw new RuntimeException("队列为空");
}
return this.item[front];
}
/**
* 出队列
*
* @return
*/
public Object deQueue() {
if (isQueueEmpty()) {
throw new RuntimeException("队列为空");
}
Object o = null;
if (front > this.item.length - 1) {
front = 0;
}
o = this.item[front];
this.item[front++] = null;
count--;
return o;
}
}
学习队列结构
最新推荐文章于 2018-10-22 10:42:50 发布