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

来源:互联网 发布:纯语音直播软件 编辑:程序博客网 时间:2024/06/05 00:26

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

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)


0 0
原创粉丝点击