# Java实现环形队列

public boolean add(E e)：加入队列
public E next()：加入返回当前指针元素并把指针指向下一个元素
public E prev()：返回当前元素，并把指针指向上一个元素
remove(E e)：删除队列中某一个元素

public class CircularQueue<E> {
private int size;

//指针
private Node<E> node;

private Node<E> first;
private Node<E> last;

private final int MODE_NEXT = 0;
private final int MODE_PREV = 1;
private int lastMode = MODE_NEXT; //最后一次操作，0为next，1为prev

public CircularQueue() {

}

/**
* 加入队列
* @param e
*/
public boolean add(E e){
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, first);
last = newNode;

if (node == null) node = newNode; //指针
if (l == null) {
first = newNode;
first.prev = first;
}
else {
l.next = newNode;
first.prev = l.next;
}

size++;
return true;
}

/**
* 返回当前指针元素并把指针指向下一个元素
* @return
*/
public E next() {
if (node == null) {
return null;
}
E e = node.item;
node = node.next;

lastMode = MODE_NEXT;
return e;
}

/**
* 返回当前元素，并把指针指向上一个元素
* @return
*/
public E prev() {
if (node == null) {
return  null;
}
E e = node.item;
node = node.prev;

lastMode = MODE_PREV;
return e;
}

/**
* 删除队列中某一个元素
* @param e
* @return
*/
public boolean remove(E e) {
if (e == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (e.equals(x.item)) {
return true;
}
}
}

size--;
return true;
}

public E peek(){
return node.item;
}

/**
* 删除节点
*/
E unlink(Node<E> x) {
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;

if (prev == x || next == x) {
this.first = null;
this.last = null;
this.node = null;
}

next.prev = prev;
prev.next = next;

if ((element==null&&this.node.item==null) || (element.equals(this.node.item))) {
this.node = lastMode==MODE_NEXT ? this.node.next : this.node.prev;
}

x.item = null;
x = null;
size--;
return element;
}

public int size() {
return size;
}

/**
* 节点类
* @param <E>
*/
private static class Node<E> {
E item;
Node<E> next;
Node<E> prev;

Node(Node<E> prev, E element, Node<E> next) {
this.item = element;
this.next = next;
this.prev = prev;
}
}

}

import java.util.concurrent.locks.ReentrantLock;

/**
* Created by giant039 on 2017/3/17.
*/
public class CircularBlockingQueue<E> extends CircularQueue<E> {
/** 对添加，删除，指针移动操作加锁 */
protected final ReentrantLock putLock = new ReentrantLock();

private QueueListener listener;

public CircularBlockingQueue() {
super();
}

public CircularBlockingQueue(QueueListener listener) {
super();
this.listener = listener;
}

public void setListener(QueueListener listener) {
this.listener = listener;
}

@Override
public boolean add(E e) {
final ReentrantLock putLock = this.putLock;
try {
putLock.lockInterruptibly();

if (listener != null) listener.afterAdd(e);

return true;
} catch (InterruptedException exp) {
exp.printStackTrace();
return false;
} finally {
putLock.unlock();
}

}

@Override
public E next() {
final ReentrantLock putLock = this.putLock;
try {
putLock.lockInterruptibly();
return super.next();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} finally {
putLock.unlock();
}

}

@Override
public E prev() {
final ReentrantLock putLock = this.putLock;
try {
putLock.lockInterruptibly();
return super.prev();
} catch (InterruptedException e) {
e.printStackTrace();
return null;
} finally {
putLock.unlock();
}
}

@Override
public boolean remove(E e) {
final ReentrantLock putLock = this.putLock;
try {
putLock.lockInterruptibly();

if (listener != null) listener.afterAdd(e);

return super.remove(e);
} catch (InterruptedException exp) {
exp.printStackTrace();
return false;
} finally {
putLock.unlock();
}
}

/**
* 监听器监听插入，删除，等操作之后需要实现的功能
*/
interface QueueListener<E> {
void afterRemove(E e);
}

}


02-21 2609
01-14 132
06-23 5318
07-20 2302
10-15 199
07-25 1576
05-31 1914
01-10 84
02-05 71
06-08 968
10-18
03-26 652
10-09 2581
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客