本文利用java语言模拟J队列的实现,下面先对队列的相关概念作简单介绍:
队列:一种先进先出(FIFO)的线性表,只允许在表的一端进行插入,而在另一端进行删除;
队尾:允许插入元素的一端; 队头:允许删除元素的一端;
队列有两种存储方式,分别对应链队列和循环队列;
1. 队列接口Queue
package org.sky.queue;
public interface Queue<E> {
/**
* Inserts the specified element into this queue if it is possible to do so immediately
* without violating capacity restrictions.
* @param e
* @return true if the element was added to this queue, else false
*/
public boolean offer(E e);
/**
* Retrieves and removes the head of this queue, or returns null if this queue is empty.
* @return the head of this queue, or null if this queue is empty
*/
public E poll();
/**
* Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
* @return the head of this queue, or null if this queue is empty
*/
public E peek();
}
2. 同步(安全的)链队列
package org.sky.queue;
import java.util.Collection;
public class SynchronizedLinkedQueue<E> implements Queue<E> {
//队列头指针
private Node<E> front;
//队列尾指针
private Node<E> rear;
//队列包含的元素个数
private int size = 0;
//结点
private static class Node<E>{
E element;
Node<E> next;
public Node(E element, Node<E> next){
this.element = element;
this.next = next;
}
}
/**
* 构建空的链队列
*/
public SynchronizedLinkedQueue(){
front = rear = new Node<E>(null, null); //头结点,不存储任何数据元素信息
}
/**
* 根据传入的集合创建链队列
* @param c
*/
public SynchronizedLinkedQueue(Collection<? extends E> c){
if(c != null && c.size() > 0){
for(Object o: c){
Node<E> node = new Node<E>((E)o, null);
rear.next = node;
rear = node;
size++;
}
}
}
/**
* 将给定元素添加到队列
* @return 添加成功,返回true;否则,返回false
*/
@Override
public synchronized boolean offer(E e) {
if(front == null && rear == null){
return false;
}
Node<E> node = new Node<E>(e, null);
rear.next = node;
rear = node;
size++;
return true;
}
/**
* 删除队头结点,并在队列非空时,返回队头结点包含的元素
* @return 队头结点包含的元素
*/
@Override
public synchronized E poll() {
if(front == rear){
return null;
}
E Element = front.next.element;
if(front.next == rear){
front.next = null;
rear = front;
}else{
front.next = front.next.next;
}
size--;
return Element;
}
/**
* 队列非空,则返回队头结点包含的元素
* @return 队头结点包含的元素
*/
@Override
public synchronized E peek() {
if(front == rear){
return null;
}
E Element = front.next.element;
return Element;
}
}
3. 同步(安全的)循环队列
package org.sky.queue;
public class SynchronizedCircularQueue<E> implements Queue<E> {
//用于保循环队列中的元素
private Object[] objs;
//循环队列中的元素个数
private int size = 0;
//循环队列的长度
private int initialCapacity = 10;
//队头指示符
private int front = 0;
//队尾指示符
private int rear = 0;
/**
* 构造默认长度为10的空表
*/
public SynchronizedCircularQueue(){
objs = new Object[initialCapacity+1];
rear++;
}
/**
* 构造指定长度的空表
* @param initialCapacity
*/
public SynchronizedCircularQueue(int initialCapacity){
if(initialCapacity < 0){
try{
throw new Exception("Illegal initSize:" + initialCapacity);
}catch(Exception e){
e.printStackTrace();
}
}
this.initialCapacity = initialCapacity+1;
objs = new Object[initialCapacity+1];
rear++;
}
/**
* 将给定元素添加到队列
* @return 添加成功,返回true;否则,返回false
*/
@Override
public boolean offer(E e) {
if(front - rear == 1 || rear - front == initialCapacity){
try{
throw new Exception("CircularQueue is full !");
}catch(Exception exception){
exception.printStackTrace();
}
return false;
}
objs[rear] = e;
if(rear == initialCapacity){
rear = 0;
}else{
rear++;
}
return false;
}
/**
* 删除队头结点,并在队列非空时,返回队头结点包含的元素
* @return 队头结点包含的元素
*/
@Override
public E poll() {
if(rear - front == 1 || front - rear == initialCapacity){
try{
throw new Exception("CircularQueue is empty !");
}catch(Exception exception){
exception.printStackTrace();
}
return null;
}
if(rear == 0){
rear = initialCapacity;
}else{
rear = rear - 1;
}
return (E)objs[rear];
}
/**
* 队列非空,则返回队头结点包含的元素
* @return 队头结点包含的元素
*/
@Override
public E peek() {
if(rear - front == 1 || front - rear == initialCapacity){
try{
throw new Exception("CircularQueue is empty !");
}catch(Exception exception){
exception.printStackTrace();
}
return null;
}
E element;
if(rear == 0){
element = (E)objs[initialCapacity];
}else{
element = (E)objs[rear - 1];
}
return element;
}
}