代码改变世界

数据结构学习----链式队列(Java实现)

  雪夜&流星  阅读(395)  评论(0编辑  收藏  举报

单链表结点类,T指定结点的元素类型:

复制代码
package com.clarck.datastructure.queue;

/**
 * 单链表结点类,T指定结点的元素类型
 * 
 * @author clarck
 *
 * @param <T>
 */
public class Node<T> {
    /**
     * 数据域,保存数据元素
     */
    public T data;
    
    /**
     * 地址域,引用后继结点
     */
    public Node<T> next;

    /**
     * 构造结点,data指定数据元素,next指定后继结点
     * 
     * @param data
     * @param next
     */
    public Node(T data, Node<T> next) {
        this.data = data;
        this.next = next;
    }

    /**
     * 构造节点
     */
    public Node() {
        this(null, null);
    }

    /**
     * 返回结点元素值对应的字符串
     */
    @Override
    public String toString() {
        return this.data.toString();
    }

    /**
     * 比较两个结点值是否相等,覆盖Object类的equals(obj)方法
     */
    @SuppressWarnings("unchecked")
    @Override
    public boolean equals(Object obj) {
        return obj == this || obj instanceof Node && this.data.equals(((Node<T>)obj).data);
    }
    
}
复制代码

链式队列类,实现队列接口:

复制代码
package com.clarck.datastructure.queue;

/**
 * 链式队列类,实现队列接口
 * 
 * @author clarck
 * 
 * @param <T>
 */
public class LinkedQueue<T> implements QQueue<T> {
    /**
     * front和rear分别指向队头和队尾结点
     */
    private Node<T> front, rear;

    /**
     * 构造空队列
     */
    public LinkedQueue() {
        this.front = this.rear = null;
    }

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

    /**
     * 元素x入队,空对象不能入队
     */
    @Override
    public void enqueue(T x) {
        if (x == null)
            return;
        Node<T> q = new Node<T>(x, null);
        if (this.front == null) {
            this.front = q;
        } else {
            // 插入在队列之尾
            this.rear.next = q;
        }
        this.rear = q;
    }

    /**
     * 出队,返回队头元素,若队列空返回null
     */
    @Override
    public T dequeue() {
        if (isEmpty())
            return null;
        // 取得队头元素
        T temp = this.front.data;
        // 删除队头节点
        this.front = this.front.next;
        if (this.front == null)
            this.rear = null;
        return temp;
    }

    /**
     * 返回队列所有元素的描述字符串,形式为“(,)” 算法同不带头结点的单链表
     */
    @Override
    public String toString() {
        String str = "(";
        for (Node<T> p = this.front; p != null; p = p.next) {
            str += p.data.toString();
            if (p.next != null) {
                // 不是最后一个结点时后加分隔符
                str += ", ";
            }
        }
        // 空表返回()
        return str + ")";
    }

}
复制代码

队列测试类:

复制代码
package com.clarck.datastructure.queue;

/**
 * 队列测试类
 * 
 * @author clarck
 * 
 */
public class Queue_test {
    public static void main(String args[]) {
        LinkedQueue<Integer> q = new LinkedQueue<Integer>();
        System.out.print("enqueue: ");
        for (int i = 0; i <= 5; i++) {
            Integer intobj = new Integer(i);
            q.enqueue(intobj);
            System.out.print(intobj + " ");
        }
        System.out.println("\n" + q.toString());

        System.out.print("dequeue: ");
        while (!q.isEmpty()) {
            System.out.print(q.dequeue().toString() + " ");
        }
        System.out.println();
    }
}
复制代码

测试结果:

enqueue: 0 1 2 3 4 5 
(0, 1, 2, 3, 4, 5)
dequeue: 0 1 2 3 4 5 

 

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2013-10-21 设计模式——简单工厂模式(SimpleFactory Pattern)
点击右上角即可分享
微信分享提示