栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现链队列

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现链队列

一、代码如下

public class LinkQueue{
    private Node front,rear;
    private int length;

    //构造一个空的队列
    public LinkQueue() {
        length = 0;
        front = rear = new Node<>(null);
    }

    //在队列的队尾插入一个新元素
    public void EnQueue(T obj)  {
        rear = rear.next = new Node(obj,null);
        length++;
    }

    //删除队列队头元素
    public T DeQueue( ) {
        if (isEmpty()){
            System.out.println("链队列为空,无法执行删除操作");
            return null;
        }
        Node p = front.next;
        T x = p.data;
        front.next = p.next;
        length--;
        if (front.next == null){
            rear = front;
        }
        return x;
    }

    //取队列队头元素
    public T getHead( ){
        if (isEmpty()){
            System.out.println("链队列为空,无法取对头元素");
            return null;
        }
        return front.next.data;
    }

    //求出队列中数据元素的个数
    public int size( ){
        return length;
    }

    //判断当前队列是否为空
    public boolean isEmpty(){
        return length == 0;
    }

    //依次访问队列中每个元素并输出
    public void nextOrder(){
        Node p = front.next;
        while (p != null){
            System.out.println(p.data);
            p = p.next;
        }
    }

    //销毁一个已存在的队列
    public void clear(){
        front.next = rear.next = null;
    }
}

二、测试代码如下

public class testLinkQueue {
    public static void main(String[] args) {
        LinkQueue queue = new LinkQueue<>();
        int a[] = {23,45,2,6,9,4,87,20};
        for(int i = 0;i

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/831136.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号