队列接口
public interface QueueInterface<T> {
public void enqueue (T newEntry);
public T dequeue();
public T getFront();
public boolean isEmpty();
public void clear();
}
循环数组实现队列
import java.io.Serializable;
import java.util.prefs.BackingStoreException;
public class ArrayQueue<T> implements QueueInterface<T>, Serializable {
private T[] queue;
private int frontIndex;
private int backIndex;
private static final int INITIAL_CAPACITY = 5;
public ArrayQueue() {
this(INITIAL_CAPACITY);
}
public ArrayQueue(int initialCapacity) {
queue = (T[]) new Object[initialCapacity + 1];
frontIndex=0;
backIndex=initialCapacity;
}
@Override
public void enqueue(T newEntry) {
// TODO Auto-generated method stub
if (isArrayFull())
doubleArray();
backIndex=(backIndex+1)%queue.length;
queue[backIndex]=newEntry;
}
@Override
public T dequeue() {
// TODO Auto-generated method stub
T front=null;
if(!isEmpty())
{
front=queue[frontIndex];
queue[frontIndex]=null;
frontIndex=(frontIndex+1)%queue.length;
}
return front;
}
@Override
public T getFront() {
// TODO Auto-generated method stub
T front=null;
if(!isEmpty())
front=queue[frontIndex];
return front;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return frontIndex==((backIndex+1)%queue.length);
}
public void doubleArray()
{
T[] oldQueue=queue;
int oldSize=oldQueue.length;
queue =(T[])new Object[2*oldSize];
for(int index=0;index<oldSize-1;index++)
{
queue[index]=oldQueue[frontIndex];
frontIndex=(frontIndex+1)%oldSize;
}
frontIndex=0;
backIndex=oldSize-2;
}
public boolean isArrayFull()
{
return frontIndex==((backIndex+2)%queue.length);
}
@Override
public void clear() {
for(int i=0;i<queue.length;i++)
queue[i]=null;
frontIndex=0;
backIndex=queue.length-1;
// TODO Auto-generated method stub
}
public static void main(String[] argv)
{
ArrayQueue<Integer> arrayQueue=new ArrayQueue<Integer>();
arrayQueue.enqueue(1);
arrayQueue.enqueue(2);
arrayQueue.enqueue(3);
arrayQueue.enqueue(4);
arrayQueue.enqueue(5);
System.out.println("front : "+arrayQueue.frontIndex);
System.out.println("last : "+arrayQueue.backIndex);
arrayQueue.dequeue();
arrayQueue.dequeue();
arrayQueue.enqueue(6);
System.out.println(arrayQueue.getFront());
System.out.println("front : "+arrayQueue.frontIndex);
System.out.println("last : "+arrayQueue.backIndex);
arrayQueue.enqueue(6);
System.out.println("front : "+arrayQueue.frontIndex);
System.out.println("last : "+arrayQueue.backIndex);
arrayQueue.enqueue(7);
System.out.println("front : "+arrayQueue.frontIndex);
System.out.println("last : "+arrayQueue.backIndex);
System.out.println(arrayQueue.getFront());
}
}
链表实现队列
import java.io.Serializable;
import org.w3c.dom.Node;
public class LinkedQueue <T>implements QueueInterface<T>,Serializable{
private Node firstNode;
private Node lastNode;
public LinkedQueue()
{
firstNode=null;
lastNode=null;
}
private class Node implements Serializable
{
private T dataT;
private Node next;
public Node(T dataT, Node next) {
super();
this.dataT = dataT;
this.next = next;
}
public T getDataT() {
return dataT;
}
public void setDataT(T dataT) {
this.dataT = dataT;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
@Override
public void enqueue(T newEntry) {
// TODO Auto-generated method stub
Node newNode=new Node(newEntry,null);
if(isEmpty())
firstNode=newNode;
else
lastNode.setNext(newNode);
lastNode=newNode;
}
@Override
public T dequeue() {
// TODO Auto-generated method stub
T front=null;
if(!isEmpty())
{
front=firstNode.getDataT();
firstNode=firstNode.getNext();
if(firstNode==null)
lastNode=null;
}
return front;
}
@Override
public T getFront() {
// TODO Auto-generated method stub
T front=null;
if(!isEmpty())
front=firstNode.getDataT();
return front;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return firstNode==null;
}
@Override
public void clear() {
// TODO Auto-generated method stub
firstNode=null;
lastNode=null;
}
public static void main(String[] argv)
{
LinkedQueue<Integer> queue=new LinkedQueue<Integer>();
queue.enqueue(1);
System.out.println(queue.isEmpty());
System.out.println(queue.getFront());
queue.enqueue(2);
System.out.println(queue.dequeue());
System.out.println(queue.dequeue());
queue.clear();
System.out.println(queue.getFront());
}
}
循环链表实现队列
import java.io.Serializable;
import org.w3c.dom.Node;
public class CircularLinkedQueue<T> implements QueueInterface<T> ,Serializable{
private Node queueNode;
private Node freeNode;
public CircularLinkedQueue()
{
freeNode=new Node(null,null);
freeNode.setNext(freeNode);
queueNode=freeNode;
}
private class Node implements Serializable
{
private T data;
private Node next;
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Node(T data, Node next) {
super();
this.data = data;
this.next = next;
}
}
@Override
public void enqueue(T newEntry) {
// TODO Auto-generated method stub
if(isChainFull())
{
Node newNode=new Node(null, freeNode.getNext());
freeNode.setNext(newNode);
}
freeNode.setData(newEntry);
freeNode=freeNode.getNext();
}
@Override
public T dequeue() {
// TODO Auto-generated method stub
T front=null;
if(!isEmpty())
{
front=queueNode.getData();
queueNode.setData(null);
queueNode=queueNode.getNext();
}
return front;
}
@Override
public T getFront() {
// TODO Auto-generated method stub
T front=null;
if(!isEmpty())
front=queueNode.getData();
return front;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return freeNode==queueNode;
}
@Override
public void clear() {
// TODO Auto-generated method stub
if(!isEmpty())
{
while(queueNode!=freeNode)
{
queueNode.setData(null);
queueNode=queueNode.getNext();
}
}
queueNode=freeNode;
}
private boolean isChainFull()
{
return queueNode==freeNode.getNext();
}
public static void main(String[] argv)
{
LinkedQueue<Integer> queue=new LinkedQueue<Integer>();
queue.enqueue(1);
System.out.println(queue.isEmpty());
System.out.println(queue.getFront());
queue.enqueue(2);
System.out.println(queue.dequeue());
queue.clear();
System.out.println(queue.getFront());
}
}