1.顺序队:
queue接口:
package Queue; public interface Queue <T>{ public abstract boolean isEmpty(); //判空 public abstract boolean add(T x); //x入队 public abstract T peek(); //返回队头,没有删除 public abstract T poll(); //出队,返回队头 }
SeqList:
package Queue;
public class SeqQueue <T> implements Queue<T>{
private Object elements[];//存储的数据
private int front ,rear;//标记开头结果
public SeqQueue(int length) {
if(length<64) {
length=64;
}
this.elements=new Object[length];
this.front=this.rear=0;//初始化
}
public SeqQueue() {
this(64);//初始化
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return this.front==this.rear;
}
@Override
public boolean add(T x) {
if (x==null) {
return false;
}
//队列满了,扩容
if(this.front==(this.rear+1)%this.elements.length) {
Object[]temp=this.elements;
this.elements=new Object[temp.length*2];
int j=0;
for(int i=this.front;i!=this.rear;i=(i+1)%temp.length) {
this.elements[j++]=temp[i];//循环拷贝
}
this.front=0;//复位
this.rear=j;//队尾
}
this.elements[this.rear]=x;
this.rear=(this.rear+1)%this.elements.length;//循环
return true;
}
@Override
public T peek() {
// TODO Auto-generated method stub
return this.isEmpty()?null:(T)this.elements[this.front];
}
@Override
public T poll() {
// TODO Auto-generated method stub
if(this.isEmpty()) {
return null;
}
T temp=(T)this.elements[this.front];//取出数据
this.front=(this.front+1)%this.elements.length;//取出数据,
return temp;
}
public String toString() {
StringBuffer strbuf=new StringBuffer(this.getClass().getName()+"->");
for(int i=this.front;i!=this.rear;i=(i+1)%this.elements.length) {
strbuf.append(this.elements[i].toString()+" ");
}
return new String(strbuf);
}
}
2.链队:
node接口:
package Queue;
public class Node<T> //单链表结点类,T指定结点的元素类型
{
public T data; //数据域,存储数据元素
public Node<T> next; //地址域,引用后继结点
public Node(T data, Node<T> next) //构造结点,data指定数据元素,next指定后继结点
{
this.data = data; //T对象引用赋值
this.next = next; //Node<T>对象引用赋值
}
public Node()
{
this(null, null);
}
public String toString() //返回结点数据域的描述字符串
{
return this.data.toString();
}
}
LinkdeQueue:
package Queue;
public final class LinkedQueue<T> implements Queue<T> {
private Node<T>front,rear;//分别指向队列头,队列尾巴
public LinkedQueue() {
this.front=this.rear=null;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return this.front==null && this.rear==null; //队列为空
}
@Override
public boolean add(T x) {
// TODO Auto-generated method stub
if(x==null) {
return false;
}
Node<T> q=new Node<T>(x,null);//初始化
if(this.front==null) {
this.front=q; //第一个
}else {
this.rear.next=q;//后续
}
this.rear=q;
return true;//链表的插入
}
//返回队头元素,
@Override
public T peek() {
// TODO Auto-generated method stub
return this.isEmpty()?null:this.front.data;
}
@Override
public T poll() {
// TODO Auto-generated method stub
if(this.isEmpty()) {
return null ;
}
T x=this.front.data;//取得数据
this.front=this.front.next;//删除front
if(this.front==null) {
this.rear=null;//标记为空
}
return x;
}
public String toString() {
StringBuffer strbuf=new StringBuffer(this.getClass().getName()+"->");
for(Node<T>p=this.front;p!=null;p=p.next) {
strbuf.append(p.data.toString()+" ");
}
return new String(strbuf);
}
}