方法实现:

package com.qiusongde;

import java.util.Iterator;
import java.util.NoSuchElementException; import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut; public class Stack<Item> implements Iterable<Item> { private Node first;
private int n; private class Node {
Item item;
Node next;
} //1.3.12
public static Stack<String> copy(Stack<String> stack) {
Stack<String> temp = new Stack<String>();
Stack<String> result = new Stack<String>(); for(String s : stack) {
temp.push(s);
}
for(String s : temp) {
result.push(s);
} return result;
} public Stack() {
first = null;
n = 0;
} public void push(Item item) {
Node oldfirst = first; first = new Node();
first.item = item;
first.next = oldfirst; n++;
} public Item pop() { if(isEmpty())
throw new NoSuchElementException("Stack is empty"); Item item = first.item;
first = first.next;
n--; return item;
} //1.3.7
public Item peek() {
if(isEmpty())
throw new NoSuchElementException("Stack is empty"); return first.item;
} public boolean isEmpty() {
return first == null;
} public int size() {
return n;
} @Override
public Iterator<Item> iterator() { return new StackIterator();
} private class StackIterator implements Iterator<Item> { private Node current = first; @Override
public boolean hasNext() {
return current != null;
} @Override
public Item next() {
if(!hasNext())
throw new NoSuchElementException("Stack is empty"); Item item = current.item;
current = current.next; return item;
} @Override
public void remove() {
throw new UnsupportedOperationException("Stack don't support remove");
} } public static void main(String[] args) { Stack<String> stack = new Stack<String>();
StdOut.println("Initialized size:" + stack.size()); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) { stack.push(item);
StdOut.println("push success:" + item + " size:" + stack.size()); StdOut.print("Left on stack: ");
for (String s : stack) {
StdOut.print(s + " ");
}
StdOut.println(); } else {
if(stack.isEmpty())
StdOut.println("pop error, stack empty");
else {
StdOut.println("pop success:" + stack.pop() + " size:" + stack.size()); StdOut.print("Left on stack: ");
for (String s : stack) {
StdOut.print(s + " ");
}
StdOut.println();
}
} } //1.3.12
Stack<String> copy = Stack.copy(stack);
StdOut.println("copy stack:");
for(String s : copy) {
StdOut.print(s + " ");
}
StdOut.println(); } }

结果输出:

Initialized size:0
to
push success:to size:1
Left on stack: to
be
push success:be size:2
Left on stack: be to
or
push success:or size:3
Left on stack: or be to
not
push success:not size:4
Left on stack: not or be to
to
push success:to size:5
Left on stack: to not or be to
copy stack:
to not or be to

算法(Algorithms)第4版 练习 1.3.12的更多相关文章

  1. 1.2 Data Abstraction(算法 Algorithms 第4版)

    1.2.1 package com.qiusongde; import edu.princeton.cs.algs4.Point2D; import edu.princeton.cs.algs4.St ...

  2. 1.1 BASIC PROGRAMMING MODEL(算法 Algorithms 第4版)

    1.1.1 private static void exercise111() { StdOut.println("1.1.1:"); StdOut.println((0+15)/ ...

  3. ubuntu命令行下java工程编辑与算法(第四版)环境配置

    ubuntu命令行下java工程编辑与算法(第四版)环境配置 java 命令行 javac java 在学习算法(第四版)中的实例时,因需要安装配套的java编译环境,可是在编译java文件的时候总是 ...

  4. 配置算法(第4版)的Java编译环境

    1. 下载 1.1 JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html选择“Windows x64 180.5 ...

  5. 算法(第四版)C# 习题题解——1.3.49 用 6 个栈实现一个 O(1) 队列

    因为这个解法有点复杂,因此单独开一贴介绍. 那么这里就使用六个栈来解决这个问题. 这个算法来自于这篇论文. 原文里用的是 Pure Lisp,不过语法很简单,还是很容易看懂的. 先导知识——用两个栈模 ...

  6. 在Eclipse下配置算法(第四版)运行环境

    第一步:配置Eclipse运行环境 Eclipse运行环境配置过程是很简单的,用过Eclipse进行java开发或学习的同学应该都很熟悉这个过程了. 配置过程: (1)系统环境:Windows7 64 ...

  7. 排序算法总结(C语言版)

    排序算法总结(C语言版) 1.    插入排序 1.1     直接插入排序 1.2     Shell排序 2.    交换排序 2.1     冒泡排序 2.2     快速排序 3.    选择 ...

  8. 算法(第四版)C#题解——2.1

    算法(第四版)C#题解——2.1   写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csh ...

  9. 《算法》第四版 IDEA 运行环境的搭建

    <算法>第四版 IDEA 运行环境的搭建 新建 模板 小书匠 在搭建之初,我是想不到会出现如此之多的问题.我看了网上的大部分教程,都是基于Eclipse搭建的,还没有使用IDEA搭建的教程 ...

  10. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

随机推荐

  1. haifeng

    [root@localhost 桌面]# yum list|grep wubi ibus-table-chinese-wubi-haifeng.noarch -.el7 base ibus-table ...

  2. 学习日记之享元模式和Effective C++

    享元模式(Flyweight):运用共享技术有效地支持大量细粒度的对象. (1),享元模式能够避免大量很相似的开销.在程序设计中,有时须要生成大量细粒度的类实例来表示数据.假设能发现这些实例除了几个參 ...

  3. PropertiesTest

    import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public clas ...

  4. VMware-Fusion-7.0.0-2103067 Pro SN:序列号+ 百度云下载地址

    VMware-Fusion-7.0.0-2103067Pro SN: 5CQE9-H5PY3-04ND5-4Z6EW-3QGDE JZCNC-2H9X9-44TD9-Y0X5W-2KGP5 8ZNTC ...

  5. Spring学习三----------注入方式

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring注入方式 本篇博客只讲最常用的两种注入方式:设值注入和构造器注入.代码为完整代码,复制即可使用. 1.目录结构 2.pom.xml < ...

  6. Synchronized修饰静态变量和普通变量的区别

    这里主要涉及到类对象(static方法),对象方法(非static方法) 我们知道,当synchronized修饰一个static方法时,多线程下,获取的是类锁(即Class本身,注意:不是实例): ...

  7. Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!

    using System; using System.Collections.Generic; using System.Text; using System.IO; using Newtonsoft ...

  8. 【selenium+python】之Python Flask 开发环境搭建(Windows)

    一.先安装python以及pip 二.其次, Python的虚拟环境安装: 在github上下载https://github.com/pypa/virtualenv/tree/master  zip文 ...

  9. linux crontab 定时任务解析

    -----------crontab定时任务---------------------- 检查crontab工具是否安装 crontab -l 检查crontab服务是否启动 service cron ...

  10. 模式匹配之surf----特征点检测学习_2(surf算法)

    在上篇博客特征点检测学习_1(sift算法) 中简单介绍了经典的sift算法,sift算法比较稳定,检测到的特征点也比较多,其最大的确定是计算复杂度较高.后面有不少学者对其进行了改进,其中比较出名的就 ...