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+" ");
           }
    }

  • 相关阅读:
    python中 __new__和__init__
    生成器
    边缘与轮廓
    霍夫变换
    高级滤波
    基本形态学滤波
    基本图形的绘制(基于skimage)
    图像的自动阈值分割
    图像的简单滤波
    直方图与均衡化
  • 原文地址:https://www.cnblogs.com/longjin2018/p/9854327.html
Copyright © 2011-2022 走看看