您的位置:首页 > 其它

学习队列结构

2016-08-05 22:12 148 查看
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  结构 队列