zoukankan      html  css  js  c++  java
  • Algs4-1.3链表实现科泛型可迭代Bag

     图片
    import java.util.Iterator;
    public class Bag<Item> implements Iterable<Item>
    {
        private Node first;
        private int N;
        private class Node
        {
            Item item;
            Node next;
        }//end class Node
        public boolean isEmpty(){return N==0;}
        public int size(){return N;}
        public void add(Item item)
        {
            Node oldfirst=first;
            first=new Node();
            first.item=item;
            first.next=oldfirst;
            N++;
        }//end add
       
        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)
        {
            Bag<String> b=new Bag<String>();
            while(!StdIn.isEmpty())
            {
                String item=StdIn.readString();
                b.add(item);
            }//end while
            for(String item:b)
                StdOut.print(item+" ");
        }//end main
       
       
    }

  • 相关阅读:
    .NET Core 服务调用 RPC
    从Docker 到 Kubernatetes 的跃迁之路
    同步异步-多线程梳理
    Net的微服务选型之路
    Visual Studio 2019安装SSIS
    HL7协议的基本语法
    vue学习笔记
    开发常用的部分sql语句总结
    VSPD虚拟串口来调试通信接口程序
    SSRS报表工具之合并行数据
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849270.html
Copyright © 2011-2022 走看看