第一种实现方式:
package sort;
/**
* java 实现队列测试
* @author Brook Lieu
* @version v1.0 2014-9-29
*/
public class QueueDemo_1 {
public static void main(String[] args) {
ArrayQueue<String> queue = new ArrayQueue<String>(5);
queue.add("a");
queue.add("b");
queue.add("c");
queue.add("d");
System.out.println("front: " + queue.peekFront());
System.out.println("end: " + queue.peekEnd());
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println("==========");
queue.add("e");
queue.add("f");
queue.add("h");
queue.add("i");
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println("============");
System.out.println(queue.getHead());
System.out.println(queue.getTail());
}
}
package sort;
/**
* Java 数组队列
* @author Brook Lieu
* @version v1.0 2014-9-30
*/
public class ArrayQueue<T> {
private int head = 0;
private int tail = -1;
private int maxSize;
private T[] dataObjs;
private int elementNum = 0;
@SuppressWarnings("unchecked")
public ArrayQueue(int maxSize) {
this.maxSize = maxSize;
dataObjs = (T[]) new Object[maxSize];
}
public void add(T t) {
if (isFull()) {
throw new RuntimeException("队列已满无法再插入值!");
} else {
if (tail == maxSize - 1) {
tail = 0-1;
}
dataObjs[++tail] = t;
elementNum++;
}
}
public T delete() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能删除值!");
} else {
elementNum--;
if (head == maxSize -1) {
head = 0;
return dataObjs[maxSize - 1];
} else {
return dataObjs[head++];
}
}
}
/**
* 验证队列是否已满
* @return
*/
private boolean isFull() {
if (elementNum == maxSize) {
return true;
} else {
return false;
}
}
/**
* 是否为空
* @return
*/
public boolean isEmpty() {
if (elementNum == 0) {
return true;
} else {
return false;
}
}
/**
* 查看队首
* @return
*/
public T peekFront() {
if (!isEmpty()) {
return dataObjs[head];
} else {
throw new RuntimeException("队列为空,没有值可以查看");
}
}
/**
* 查看队尾
* @return
*/
public T peekEnd() {
if (!isEmpty()) {
return dataObjs[tail];
} else {
throw new RuntimeException("队列为空,没有值可以查看");
}
}
/**
* @return the head
*/
public int getHead() {
return head;
}
/**
* @param head the head to set
*/
public void setHead(int head) {
this.head = head;
}
/**
* @return the tail
*/
public int getTail() {
return tail;
}
/**
* @param tail the tail to set
*/
public void setTail(int tail) {
this.tail = tail;
}
/**
* @return the maxSize
*/
public int getMaxSize() {
return maxSize;
}
/**
* @param maxSize the maxSize to set
*/
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
/**
* @return the elementNum
*/
public int getElementNum() {
return elementNum;
}
/**
* @param elementNum the elementNum to set
*/
public void setElementNum(int elementNum) {
this.elementNum = elementNum;
}
}
输出结果为:
front: a
end: d
a
b
c
==========
d
e
f
h
i
============
3
2
====================================================================================================================
以上代码虽然实现了循环队列,但是由于增加了 elementNum 属性,需要不断的进行 插入和删除时,就要不断的执行 ++ 和 -- 工作。现在提供另一种更加帅气的实现方式。
package sort;
/**
* java 实现队列测试
* @author Brook Lieu
* @version v1.0 2014-9-29
*/
public class QueueDemo_1 {
public static void main(String[] args) {
ArrayQueue_2<String> queue = new ArrayQueue_2<String>(5);
queue.insrt("a");
queue.insrt("b");
queue.insrt("c");
queue.insrt("d");
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println("tail is " + queue.getTail());
System.out.println("head is " + queue.getHead());
System.out.println("==========");
queue.insrt("e");
queue.insrt("f");
queue.insrt("h");
//queue.insrt("i");
System.out.println("tail is " + queue.getTail());
System.out.println("head is " + queue.getHead());
System.out.println(queue.delete());
System.out.println(queue.delete());
System.out.println(queue.delete());
//System.out.println(queue.delete());
//System.out.println(queue.delete());
System.out.println("============");
System.out.println(queue.getElementNum());
System.out.println("============");
System.out.println(queue.getElementNum());
}
}
package sort;
import java.net.NetPermission;
import javax.rmi.CORBA.Tie;
import javax.xml.stream.events.EndDocument;
import javax.xml.transform.Templates;
/**
* 数组实现队列
* @author Brook Lieu
* @version v1.0 2014-10-1
* @param <T>
*/
public class ArrayQueue_2<T> {
private int head = 0;
private int tail = -1;
private int maxSize;
private T[] dataObjs;
@SuppressWarnings("unchecked")
public ArrayQueue_2(int maxSize) {
this.maxSize = maxSize + 1;
dataObjs = (T[]) new Object[this.maxSize];
}
public void insrt(T t) {
if (!isFull()) {
if (tail == (maxSize-1)) {
tail = -1;
}
dataObjs[++tail] = t;
} else {
throw new RuntimeException("队列已满,不能再插入值!");
}
}
public T delete() {
if (!isEmpty()) {
T temp = dataObjs[head];
if ((head + 1) == maxSize) {
head = 0;
} else {
head++;
}
return temp;
} else {
throw new RuntimeException("队列已为空,没有值可被删除!");
}
}
public boolean isEmpty() {
if ((tail + 1 == head) || (maxSize + head -1 == tail)) {
return true;
} else {
return false;
}
}
public boolean isFull() {
if ((tail - head + 2 == maxSize) || (tail + 2 == head)) {
return true;
} else {
return false;
}
}
/**
* 获取元素个数
* @return
*/
public int getElementNum() {
System.out.println("tail is " + tail);
System.out.println("head is " + head);
if (isEmpty()) {
return 0;
}
if (tail >= head) {
return tail - head + 1;
} else {
return maxSize - head + tail + 1;
}
}
/**
* @return the head
*/
public int getHead() {
return head;
}
/**
* @param head the head to set
*/
public void setHead(int head) {
this.head = head;
}
/**
* @return the tail
*/
public int getTail() {
return tail;
}
/**
* @param tail the tail to set
*/
public void setTail(int tail) {
this.tail = tail;
}
/**
* @return the maxSize
*/
public int getMaxSize() {
return maxSize;
}
/**
* @param maxSize the maxSize to set
*/
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
}
输出结果为:
a
b
c
tail is 3
head is 3
==========
tail is 0
head is 3
d
e
f
============
tail is 0
head is 0
1
============
tail is 0
head is 0
1