furzoom

为Stack添加一个方法peek(),返回栈中最近添加的元素(而不是弹出)。


算法-第四版-练习1.3.3解答中的top()方法重构为peek()方法即可。


即将

    public Item top()
    {
        return first.item;
    }
改名为

    public Item peek()
    {
        return first.item;
    }


测试用例:

/**
 * Description : 
 * Author      : mn@furzoom.com
 * Date        : Sep 29, 2016 9:04:11 AM
 * Copyright (c) 2013-2016, http://furzoom.com All Rights Reserved.
 */
package com.furzoom.lab.algs.ch103;

/**
 * ClassName    : E10307 <br>
 * Function     : TODO ADD FUNCTION. <br>
 * date         : Sep 29, 2016 9:04:11 AM <br>
 * 
 * @version 
 */
public class E10307
{
    public static void main(String[] args)
    {
        Stack<String> s = new Stack<String>();
        
        s.push("Welcome");
        s.push("to");
        s.push("furzoom.com");
        
        System.out.println(s.peek());
        System.out.println(s.peek());
        s.pop();
        System.out.println(s.peek());
        System.out.println(s.peek());
    }

}

输出:

furzoom.com
furzoom.com
to
to

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

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


分类:

技术点:

相关文章:

  • 2021-12-13
  • 2021-12-05
  • 2021-12-27
  • 2022-02-06
  • 2021-12-04
  • 2022-01-26
  • 2022-01-20
  • 2021-12-26
猜你喜欢
  • 2022-12-23
  • 2021-04-08
  • 2021-04-04
  • 2021-04-09
  • 2021-12-17
  • 2022-01-27
  • 2021-11-13
相关资源
相似解决方案