具体有难度的地方已在文中注释
队列的三种实现
1.数组队列
/**
* @author Jay
* @date 2020/7/8 19:39
* @Description:
*/
public class ArrayQueue<E> {
private Array<E> array;
public ArrayQueue(int capcity){
array=new Array<>(capcity);
}
public ArrayQueue(){
array=new Array<>();
}
public int getsize(){
return array.getSize();
}
public boolean isEmpty (){
return array.isEmpty();
}
public void enqueue(E e){
array.addLast(e);
}
public E dequeue(){
return array.removeFirst();
}
public E getFront(){
return array.getFirst();
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Queue: ");
res.append('[');
for (int i = 0; i < array.getSize(); ++i) {
res.append(array.get(i));
if (i != array.getSize() - 1)
res.append(", ");
}
res.append("] tail");
return res.toString();
}
}
2.循环队列
/**
* @author Jay
* @date 2020/7/9 21:46
* @Description:
*/
public class LoopQueue<E> {
private E[] data;
private int tail,front;
private int size;
public LoopQueue(){
this(10);
}
@SuppressWarnings("unchecked")
public LoopQueue(int capcity){
data= (E[]) new Objects[capcity+1];
front=0;
tail=0;
size=0;
}
public int getCapcity(){
return data.length-1;
}
public boolean isEmpty(){
return size==0;
}
public int getSize(){
return size;
}
public void enqueue(E e){
if ((tail+1)%data.length==front)
resize(getCapcity()*2);
data[tail]=e;
tail=(tail+1)%data.length;
++size;
}
public E dequeue(){
if (isEmpty())
throw new IllegalArgumentException("Cannot dequeeu from an empty queue.");
E ret=data[front];
data[front]=null;
front=(front+1)%data.length;
--size;
if (size == getCapacity() / 4 && getCapacity() / 2 != 0) //缩容
resize(getCapacity() / 2);
return ret;
}
public E getFront(){
if (isEmpty())
throw new IllegalArgumentException("Cannot dequeeu from an empty queue.");
return data[front];
}
@SuppressWarnings("unchecked")
public void resize(int newCapacity){
E[] newdata= (E[]) new Object[newCapacity+1];
for (int i = 0; i <size ; ++i)
newdata[i]=data[(front+i)%data.length];
data=newdata;
front=0;
tail=size;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Queue: size = %d, capacity = %d\n", size, getCapacity()));
res.append("front [");
for (int i = front; i != tail; i = (i + 1) % data.length) {
res.append(data[i]);
if ((i + 1) % data.length != tail)
res.append(", ");
}
res.append(']');
return res.toString();
}
}
3.链队
/**
* @author Jay
* @date 2020/7/9 22:18
* @Description:
*/
public class LinkQueue<E> {
private class Node { //链表结点不变
public E e;
public Node next;
public Node(E e, Node next) {
this.e = e;
this.next = next;
}
public Node(E e) {
this(e, null);
}
public Node() {
this(null, null);
}
@Override
public String toString() {
return e.toString();
}
}
private Node head;
private Node tail;
private int size;
public LinkQueue(){
head=null;
tail=null;
size=0;
}
public int getSize(){
return size;
}
public boolean isEmpty(){
return size==0;
}
public void enqueue(E e){
if (tail==null){
tail=new Node(e);
head=tail;
}else {
tail.next = new Node(e);
tail = tail.next;
}
++size;
}
public E dequeue(){
if (isEmpty())
throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
Node ret=head;
head=head.next;
if (head==null)
tail=null;
ret.next=null;
--size;
return ret.e;
}
public E getFront(){
if (isEmpty())
throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
return head.e;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Queue: front ");
Node cur = head;
while (cur != null) {
res.append(cur.e + "->");
cur = cur.next;
}
res.append("NULL tail");
return res.toString();
}
}