相对比较简单,用到了之前写过的结点类,包含数据域和指针域
代码如下:
接口:
package queueTest;
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 queueTest;
import linearList.Node;
public class LinkQueue implements IQueue {
private Node front;
private Node rear;
public LinkQueue() {
front = rear = null;
}
@Override
public void clear() {
front = rear = null;
}
@Override
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;
}
@Override
public Object peek() {
if (front != null) {
return front.data;
} else
return null;
}
@Override
public void offer(Object x) throws Exception {
Node p=new Node(x);
if(front!=null){
rear.next=p;
rear=p;
}
else{
front=rear=p;
}
}
@Override
public Object poll() {
if (front != null) {
Node p = front;
front = front.next;
if (p == rear) {
rear = null;
}
return p.data;
} else
return null;
}
@Override
public void display() {
Node p=front;
while(p!=null){
System.out.printf(p.data+" ");
p=p.next;
}
}
}