/**
* Project Name:test-queue
* File Name:Queue.java
* Package Name:com.six.test.queue
* Copyright (c) 2016, www.windo-soft.com All Rights Reserved.
*
*/
package com.six.test.queue;
import java.io.Serializable;
/**
* ClassName:Queue <br/>
*
* @author six
* @version
* @since JDK 1.6
* @see
*/
public class Queue implements Serializable {
/**
* serialVersionUID:TODO(用一句话描述这个变量表示什么).
*
* @since JDK 1.6
*/
private static final long serialVersionUID = -8754063046843872291L;
private int maxLength;// 队列最大长度
private QueueItem[] items;
private int front; // 队头索引(包含)
private int rear;// 队尾索引(包含)
private int nItems;// 元素的个数
public Queue(int maxLength) {
this.maxLength = maxLength;
items = new QueueItem[maxLength];
nItems = 0;
rear = -1;
front = 0;
}
/** 队尾增加一个元素 */
public Queue add(QueueItem item) {
if (item == null) {
return this;
}
if (isFull()) {
// 超出最大容量
throw new RuntimeException("超出最大容量");
}
if (rear == maxLength - 1) {
rear = -1;
}
items[++rear] = item;
increaseNItem();
return this;
}
public void increaseNItem() {
if (isFull()) {
return;
}
nItems++;
}
/** 队头删除一个元素 */
public QueueItem remove() {
if (isEmpty()) {
throw new RuntimeException("容量为0.");
}
QueueItem result = items[front];
if (front == maxLength - 1) {
items[maxLength - 1] = null;
front = 0;
} else {
items[front++] = null;
}
nItems--;
return result;
}
public QueueItem peekFirst() {
return items[front];
}
public QueueItem peekLast() {
return items[rear];
}
public boolean isEmpty() {
return nItems == 0;
}
public boolean isFull() {
return nItems == maxLength;
}
public int size() {
return nItems;
}
public int getLastIndex() {
return rear;
}
public int getFirstIndex() {
return front;
}
public int getMaxLength() {
return maxLength;
}
public QueueItem peek(int index) {
return items[index];
}
public static void printf(Queue queue) {
int firstIndex = queue.getFirstIndex();
System.out.println("firstindex: " + firstIndex);
int lastIndex = queue.getLastIndex();
System.out.println("lastindex: " + lastIndex);
for (int index = firstIndex; index < queue.getMaxLength(); index++) {
System.out.println("index: " + index + ", " + queue.peek(index));
}
if (lastIndex < firstIndex) {
for (int index = 0; index <= lastIndex; index++) {
System.out.println("index: " + index + ", " + queue.peek(index));
}
}
System.out.println("-----------------");
}
public static void main(String[] args) {
Queue queue = new Queue(5);
QueueItem item1 = new QueueItem(0);
QueueItem item2 = new QueueItem(1);
QueueItem item3 = new QueueItem(2);
QueueItem item4 = new QueueItem(3);
QueueItem item5 = new QueueItem(4);
QueueItem item6 = new QueueItem(5);
queue.add(item1);
printf(queue);
queue.add(item2);
printf(queue);
queue.add(item3);
printf(queue);
queue.add(item4);
printf(queue);
queue.add(item5);
printf(queue);
queue.remove();
printf(queue);
queue.add(item6);
printf(queue);
}
}
package com.six.test.queue;
import java.io.Serializable;
public class QueueItem implements Serializable {
private static final long serialVersionUID = 1257276035482303273L;
private int id;
public QueueItem(int id) {
this.id = id;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "QueueItem [id=" + id + "]";
}
}
在Queue类中包含数据项计数字段nItems会使insert()和remove()方法增加一点额外的操作,因为insert()和 remove()方法必须分别递增和递减这个变量值。这可能算不上额外的开销,但是如果处理大量的插入和移除操作,这就可能会影响性能了。
因为,一些队列的实现不使用数据项计数的字段,而是通过front和rear来计算出队列是否空或者满以及数据项的个数。如果这样做,isEmpty()、ifFull()和size()例程会相当复杂,因为就像前面讲过的那样,数据项的序列或者被折成两段,或者是连续的一段。
参考:http://www.cnblogs.com/key2012/archive/2012/12/18/2822939.html