zoukankan      html  css  js  c++  java
  • 队列(Java实现)

    队列的特点是先进先出

    基于链表的队列

    public class LinkedListQueue<Item>
    {
        private Node first; // 指向最早添加进队列的元素
        private Node last; // 指向最后添加进队列的元素
        private int N; // 队列中的元素
    
        private class Node
        {
            Item item;
            Node next;
        }
    
        public boolean isEmpty()
        {
            return first == null;
        }
    
        public int size()
        {
            return N;
        }
    
        public void enQueue(Item item)
        {
            // 入队,即向链表尾部添加结点
            Node oldlast = last;
            last = new Node();
            last.item = item;
            last.next = null;
            if (isEmpty())
            {
                first = last;
            } else
            {
                oldlast.next = last;
                N++;
            }
        }
    
        public Item deQueue()
        {
            // 出队,即从链表头部删除结点
            Item item = first.item;
            first = first.next;
            if (isEmpty())
            {
                last = null;
            }
            N--;
            return item;
        }
    }
  • 相关阅读:
    关于centos防火墙
    linux基础命令
    mysql经典语句
    异常处理
    抽象类
    特性-多态
    特性-继承
    特性-封装
    python模块/文件/日期时间
    python函数3-函数嵌套/递归/匿名函数
  • 原文地址:https://www.cnblogs.com/yahuian/p/10745023.html
Copyright © 2011-2022 走看看