zoukankan      html  css  js  c++  java
  • 算法-第四版-练习1.3.2解答

    给定以下输入,java Stack的输出是什么?

    it was - the best - of times - - - it was - the - -

    Stack:

    /**
     * Description : 
     * Author      : mn@furzoom.com
     * Date        : Sep 27, 2016 5:07:34 PM
     * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
     */
    package com.furzoom.lab.algs.ch103;
    
    import java.util.Iterator;
    
    import edu.princeton.cs.algs4.StdIn;
    import edu.princeton.cs.algs4.StdOut;
    
    /**
     * ClassName    : Stack <br>
     * Function     : TODO ADD FUNCTION. <br>
     * date         : Sep 27, 2016 5:07:34 PM <br>
     * 
     * @version 
     */
    public class Stack<Item> implements Iterable<Item>
    {
        private Node first;
        private int n;
        private class Node
        {
            Item item;
            Node next;
        }
            
        public boolean isEmpty() 
        {
            return first == null;
        }
        
        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;
        }
           
        @Override
        public Iterator<Item> iterator()
        {
            return new ReverseArrayIterator();
        }
        
        private class ReverseArrayIterator implements Iterator<Item>
        {
            private Node p = first;
            @Override
            public boolean hasNext()
            {
                return p != null;
            }
    
           @Override
            public Item next()
            {
               Item item = p.item;
               p = p.next;
               return item;
            }
            
        }
    
        public static void main(String[] args)
        {
            Stack<String> s;
            s = new Stack<String>();
            while (!StdIn.isEmpty())
            {
                String item = StdIn.readString();
                if (!item.equals("-"))
                    s.push(item);
                else if (!s.isEmpty())
                    StdOut.print(s.pop() + " ");
            }
            StdOut.println("(" + s.size() + " left on stack)");
        }
    }
    


    E10302.txt

    it was - the best - of times - - - it was - the - -



    结果如下:

    >java -cp ".;../lib/algs4.jar" com
    .furzoom.lab.algs.ch103.Stack < com/furzoom/lab/algs/ch103/E10302.txt
    was best times of the was the it (1 left on stack)


    算法-第四版-1.3 背包、队列和栈-习题索引汇总

    算法-第四版习题索引汇总

  • 相关阅读:
    Version
    Windows Server Protocols (WSPP)
    Tomcat启动问题jvm访问拒绝的解决方法
    vue3中使用draggable插件实现元素的拖拽,排序,克隆
    消息队列的思考
    jenkins_ssh
    jenkins_构建配置
    minikube清理sh
    stream源码导读
    源码rabbit_3_消息链路追踪
  • 原文地址:https://www.cnblogs.com/furzoom/p/7710208.html
Copyright © 2011-2022 走看看