先看队列接口和结点类
package com.lovely.queue;
public interface IQueue {
public void clear();
public boolean isEmpty();
public int length();
public Object peek();
public void offer(Object x) throws Exception;
public Object poll();
public void display();
}
package com.lovely.linearList;
public class Node {
public Object data;
public Node next;
public Node() {
this(null, null);
}
public Node(Object data) {
this(data, null);
}
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
}
1. 顺序队列
package com.lovely.queue;
public class SeqQueue implements IQueue {
private Object[] queueElem;
private int maxSize;
private int front;
private int rear;
public SeqQueue(int maxSize) {
this.maxSize = maxSize;
front = rear = 0;
queueElem = new Object[maxSize];
}
@Override
public void clear() {
front = rear = 0;
}
@Override
public boolean isEmpty() {
return front == rear;
}
@Override
public int length() {
return rear - front;
}
@Override
public Object peek() {
if (isEmpty())
return null;
return queueElem[front];
}
@Override
public void offer(Object x) throws Exception {
if (rear == maxSize)
throw new Exception("队列已满");
queueElem[rear] = x;
rear ++;
}
@Override
public Object poll() {
if (isEmpty())
return null;
front ++;
return queueElem[front];
}
@Override
public void display() {
if (!isEmpty()) {
for (int i = front; i < rear; i++) {
System.out.print(queueElem[i] + "\t");
}
} else
System.out.println("队列为空!");
}
}
package com.lovely.queue;
public class TestSeqQueue {
public static void main(String[] args) {
SeqQueue sq = new SeqQueue(6);
try {
sq.offer(1);
sq.offer(2);
sq.offer(3);
sq.offer(4);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("队列长度 " + sq.length());
Object obj = sq.poll();
System.out.println(obj + "出队后,长度 " + sq.length());
System.out.println("队首元素:" + sq.peek());
sq.display();
}
}
2. 循环队列
package com.lovely.queue;
public class CircleSeqQueue {
private Object[] queueElem;
private int front;
private int rear;
private int maxSize;
public CircleSeqQueue(int maxSize) {
front = rear = 0;
queueElem = new Object[maxSize];
this.maxSize = maxSize;
}
public void clear() {
front = rear = 0;
}
public boolean isEmpty() {
return front == rear;
}
public int length() {
return (rear - front + maxSize) % maxSize;
}
public Object peek() {
if (isEmpty())
return null;
return queueElem[front];
}
public void offer(Object x) throws Exception {
if ((rear + 1) % maxSize == front)
throw new Exception("队列已满");
queueElem[rear] = x;
rear = (rear + 1) % maxSize;
}
public Object poll() {
if (rear == front)
return null;
Object p = queueElem[front];
front = (front + 1) % maxSize;
return p;
}
public void display() {
if (!isEmpty()) {
for (int i = 0; i < rear; i = (i + 1) % maxSize) {
System.out.print(queueElem[i] + " ");
}
} else
System.out.println("此队列为空!");
}
}
package com.lovely.queue;
public class TestCircleSeqQueue {
public static void main(String[] args) {
CircleSeqQueue csq = new CircleSeqQueue(10);
try {
csq.offer(1);
csq.offer(2);
csq.offer(3);
csq.offer(4);
csq.offer(5);
csq.offer(6);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("队首元素:" + csq.peek());
System.out.println("出队:" + csq.poll());
csq.display();
}
}
3. 链队列
package com.lovely.queue;
import com.lovely.linearList.Node;
public class LinkQueue implements IQueue {
private Node front;
private Node rear;
public LinkQueue() {
front = rear = null;
}
public void clear() {
front = rear = null;
}
public boolean isEmpty() {
return front == null;
}
@Override
public int length() {
Node p = front;
int length = 0;
while (p != null) {
p = p.next;
length ++;
}
return length;
}
public Object peek() {
if (isEmpty())
return null;
return front.data;
}
@Override
public void offer(Object x) throws Exception {
Node s = new Node(x);
if (!isEmpty()) {
rear.next = s;
rear = s;
} else
front = rear = s;
}
public Object poll() {
if (front == null)
return null;
Node p = front;
front = front.next;
if (p == rear) {
rear = null;
}
return p.data;
}
public void display() {
if (!isEmpty()) {
for(Node p = front;p != null;p = p.next) {
System.out.print(p.data + "\t");
}
System.out.println();
} else {
System.out.println("此队列为空");
}
}
}
package com.lovely.queue;
public class TestLinkQueue {
public static void main(String[] args) {
LinkQueue lq = new LinkQueue();
try {
lq.offer(2);
lq.offer(3);
lq.offer(5);
lq.offer(7);
lq.offer(11);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("入队的队列:");
lq.display();
System.out.println("队首元素:" + lq.peek());
System.out.println("队首出队:" + lq.poll());
lq.display();
System.out.println("队列的长度为:" + lq.length());
}
}