zoukankan      html  css  js  c++  java
  • Algs4-1.3.42复制栈

    1.3.42复制栈。为基于链表产现的栈编写一个新的构造函数,使以下代码Stack<Item> t=new Stack<Item>(s);得到的t指向栈s的一个新的独立的副本。
    答:
    图片
    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 Stack()
        {
        }
       
        public Stack(Stack s)
        {
            Node right=new Node();
            Node oldright;
            for(Object i:s)
            {
                oldright=right;
                right=new Node();
                right.item=(Item) i;
                right.next=null;
                if(isEmpty())
                   first=right;
                else
                   oldright.next=right;
                N++;
             }
        }
        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 Item peek()
        {
            Item item=first.item;
            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
            StdOut.println("---Stack s is:");
            for(String i:s)
                StdOut.print(i+" ");
           
                   
            Stack<String> t=new Stack<String>(s);
            StdOut.println();
            StdOut.println("---Stack after new Stack<String>(s) is:");
            for(String i:s)
                StdOut.print(i+" ");
            //
            s.pop();
            StdOut.println();
            StdOut.println("---Stack after s pop, s is:");
            for(String i:s)
                StdOut.print(i+ " ");
            //
            StdOut.println();
            StdOut.println("---Stack t is:");
            for(String i:t)
                StdOut.print(i+" ");
           }
    }

  • 相关阅读:
    windows设置文件夹显示缩略图
    windows合并文件夹窗口
    crm高速开发之Entity
    在Eclipse中搭建Dagger和Dagger2使用环境
    CCEditBox/CCEditBoxImpl
    failed to sync branch You might need to open a shell and debug the state of this repo.
    五个方法:让站点用心服务于每一位客户
    Mobile first! Wijmo 5 + Ionic Framework之:Hello World!
    ST Nucleo mbed套件开发 一 MBED环境使用 以Nucleo-F401为例 (二)
    关于Windows通过远程桌面訪问Ubuntu
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9854327.html
Copyright © 2011-2022 走看看