zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【Python】python模块加载
    【Python】Django学习一:第一个Django程序
    【Python】Python PYQT4 GUI编程与exe打包
    XGBoost 输出特征重要性以及筛选特征
    数据挖掘博客收集
    python 多线程
    python 统计学的各种检验
    数据挖掘-逻辑回归解析
    Centos7 教程收集ing
    数据挖掘比赛优秀经验贴-收集ing
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849277.html
Copyright © 2011-2022 走看看