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

     图片
    import java.util.Iterator;
    public class Stack<Item> implements Iterable<Item>
    {
        private int N;
        private Node first;
        private class Node
        {
            Item item;
            Node next;
        }
        public boolean isEmpty()
        {return N==0;}
       
        public int size()
        {return N;}
       
        public void push(Item item)
        {
            Node oldfirst=first;
            first=new Node();
            first.item=item;
            first.next=oldfirst;
            N++;
        }
       
        public Item pop()
        {
            Item item=first.item;
            first=first.next;
            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)
        {
            Stack<String> s=new Stack<String>();
            while(!StdIn.isEmpty())
            {
                String item=StdIn.readString();
                s.push(item);
            } //end while
            for(String item:s)
            StdOut.println(item+" ");
        }
    }
  • 相关阅读:
    ASP.NET MVC中你必须知道的13个扩展点
    ASP.NET MVC扩展库
    AutoFac简介
    中小型研发团队架构实践十:应用监控怎么做?
    IDEA+Mybatis-generator代码生成工具
    IDEA+EasyCode实现代码生成
    myeclipse中导入的js文件报错(出现红叉叉,提示语法错误)
    Ibatis中常见错误解决方案
    注解
    structs常见错误
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9849274.html
Copyright © 2011-2022 走看看