数据结构学习----顺序循环队列(Java实现)

 队列抽象数据类型, 队列接口,描述队列抽象数据类型:

package com.clarck.datastructure.queue;

/**
 * 队列抽象数据类型, 队列接口,描述队列抽象数据类型
 * 
 * @author clarck
 * 
 * @param <T>
 */
public interface QQueue<T> {
    /**
     * 判断队列是否为空
     * 
     * @return
     */
    boolean isEmpty();

    /**
     * 元素x入队
     * 
     * @param x
     */
    void enqueue(T x);

    /**
     * 出队,返回队头元素
     * 
     * @return
     */
    T dequeue();
}
顺序循环队列类,实现队列接口:

package com.clarck.datastructure.queue;

/**
 * 顺序循环队列类,实现队列接口
 * 
 * @author clarck
 * 
 * @param <T>
 */
public class SeqQueue<T> implements QQueue<T> {
    /**
     * 存储队列数据元素
     */
    private Object element[];

    /**
     * front、rear分别为队列头尾下标
     */
    private int front, rear;

    /**
     * 构造容量为length的空队列
     * 
     * @param length
     */
    public SeqQueue(int length) {
        // 设置队列数组容量最小值
        if (length < 64) {
            length = 64;
        }
        // 设置空队列
        this.element = new Object[Math.abs(length)];
        this.front = this.rear = 0;
    }

    /**
     * 构造默认容量的空队列
     */
    public SeqQueue() {
        this(64);
    }

    /**
     * 判断队列是否空,若空返回true
     */
    @Override
    public boolean isEmpty() {
        return this.front == this.rear;
    }

    /**
     * 元素x入队,空对象不能入队
     */
    @Override
    public void enqueue(T x) {
        if (x == null)
            return;

        // 当队列满时,扩充容量
        if (this.front == (this.rear + 1) % this.element.length) {
            Object[] temp = this.element;
            // 重新申请一个容量更大的数组
            this.element = new Object[temp.length * 2];
            int j = 0;
            // 按照队列元素次序复制数组元素
            for (int i = this.front; i != this.rear; i = (i + 1) % temp.length) {
                this.element[j++] = temp[i];
            }
            this.front = 0;
            this.rear = j;
        }

        this.element[this.rear] = x;
        this.rear = (this.rear + 1) % this.element.length;
    }

    /**
     * 出队,返回队头元素,若队列空返回null
     */
    @SuppressWarnings("unchecked")
    @Override
    public T dequeue() {
        // 若队列空返回null
        if (isEmpty())
            return null;
        // 取得队头元素
        T temp = (T) this.element[this.front];
        this.front = (this.front + 1) % this.element.length;
        return temp;
    }

    /**
     * 返回队列所有元素的描述字符串,形式为“(,)”,按照队列元素次序
     */
    @Override
    public String toString() {
        String str = "(";
        if (!isEmpty()) {
            str += this.element[this.front].toString();
            int i = (this.front + 1) % this.element.length;
            while (i != this.rear) {
                str += ", " + this.element[i].toString();
                i = (i + 1) % this.element.length;
            }
        }
        return str + ")";
    }

}
队列测试类:
package com.clarck.datastructure.queue;

/**
 * 队列测试类
 * 
 * @author clarck
 * 
 */
public class Queue_test {
    public static void main(String args[]) {
        SeqQueue<Integer> que = new SeqQueue<Integer>(5);
        que.enqueue(new Integer(10));
        que.enqueue(new Integer(20));
        System.out.println("dequeue: " + que.dequeue().toString() + " "
                + que.dequeue().toString() + " ");
        System.out.println(que.toString());
        que.enqueue(new Integer(30));
        que.enqueue(new Integer(40));
        que.enqueue(new Integer(50));
        que.enqueue(new Integer(60));
        System.out.println(que.toString());
        que.enqueue(new Integer(70));
        System.out.println(que.toString());
        
        
    }
}

测试结果:

dequeue: 10 20 
()
(30, 40, 50, 60)
(30, 40, 50, 60, 70)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值