• Algs4-1.3链表实现泛型可迭代Queue


     图片
    import java.util.Iterator;
    public class Queue<Item> implements Iterable<Item>
    {
        private int N;
        private Node first;
        private Node last;
       
        private class Node
        {
            Item item;
            Node next;
        }
       
        public boolean isEmpty()
        {return N==0;}
       
        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;
       }
       
        public Iterator<Item> iterator()  {return new ListIterator();}
       
        private class ListIterator implements Iterator<Item>
        {
            private Node current=first;
            public boolean hasNext(){return current!=null;}
            public void remove(){}
            public Item next()
            {
                Item item=current.item;
                current=current.next;
                return item;
            }//end next
          }//end class ListIterator
        public static void main(String[] args)
        {
            Queue<String> q=new Queue<String>();
            while(!StdIn.isEmpty())
            {
                String item=StdIn.readString();
                q.enqueue(item);
            }//end while
            for(String item:q)
            StdOut.println(item+" ");
        }//end main
    }//end class

  • 相关阅读:
    基于NFS共享存储实现KVM虚拟主机动态迁移
    基于mysqld_multi实现MySQL 5.7.24多实例多进程配置
    LVS负载均衡实现双向设备
    基于Haproxy构建负载均衡集群
    基于Haproxy+Keepalived构建高可用负载均衡集群
    nginx与keepalived实现高可用
    直接路由模式(LVS-DR)
    Tomcat多实例配置
    Tomcat 安全优化
    基于 Jenkins + Git 项目 中Git主机的 安装配置
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849277.html
Copyright © 2020-2023  润新知