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 FixedCapacityStackOfStrings implements Iterable<String> { private String[] a;
private int n; public FixedCapacityStackOfStrings(int cap) {
a = new String[cap];
n = 0;
} public void push(String item) {
if(isFull())
throw new IndexOutOfBoundsException("FiexdCapcityStackOfString is Full");
a[n++] = item;
} public String pop() {
if(isEmpty())
throw new NoSuchElementException("FiexdCapcityStackOfString is empty");
return a[--n];
}   //1.3.1
public boolean isFull() {
return n == a.length;
} public boolean isEmpty() {
return n == 0;
} public int size() {
return n;
} @Override
public Iterator<String> iterator() {
return new ReverseArrayIterator();
} private class ReverseArrayIterator implements Iterator<String> { private int i = n; @Override
public boolean hasNext() {
return i > 0;
} @Override
public String next() {
return a[--i];
} } public static void main(String[] args) { int max = 5;
FixedCapacityStackOfStrings stack = new FixedCapacityStackOfStrings(max);
StdOut.println("stack initialized max size is:" + max); while (!StdIn.isEmpty()) { String item = StdIn.readString(); if (!item.equals("-")) {
if(stack.isFull())
StdOut.println("push error, stack full");
else {
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();
}
} } } }
stack initialized max size is:5
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
be
push error, stack full
-
pop success:to size:4
Left on stack: not or be to
-
pop success:not size:3
Left on stack: or be to
-
pop success:or size:2
Left on stack: be to
-
pop success:be size:1
Left on stack: to
-
pop success:to size:0
Left on stack:
-
pop error, stack empty

最新文章

  1. MyEclipse:各种提示图标的含义
  2. Ubuntu14.04安装OpenCV2.4.9
  3. plain framework 1 pak插件说明(资源压缩加密)
  4. java.lang.UnsupportedOperationException: Not supported by BasicDataSource
  5. 学习rabbitmq
  6. WCF并发控制与实例模式
  7. 将XML文件中的内容转换为Json对象
  8. jsp页面el表达式不起作用
  9. Linux之一次性安装开发工具:yum groupinstall Development tools
  10. thinkphp解决表单令牌问题
  11. jquery easyui+layer后台框架
  12. Rectangle and Square
  13. MAC中在eclipse luna上搭建移动平台自己主动化測试框架(UIAutomator/Appium/Robotium/MonkeyRunner)关键点记录
  14. ThinkPHP 常用配置项列表
  15. ASP.NET开发大杂烩
  16. Java8函数之旅 (五) -- Java8中的排序
  17. Hive 多分隔符的使用 (转载)
  18. SpringMVC(五):@RequestMapping下使用@RequestParam绑定请求参数值
  19. [LeetCode] Binary Trees With Factors 带因子的二叉树
  20. bootgrid 刷新保持当前排序

热门文章

  1. 利用Bootstrap简单实现一个文件上传进度条
  2. double check 解决单例模式的多线程并发问题
  3. linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现
  4. _DataStructure_C_Impl:图的最小生成树
  5. C语言基础知识【C语言教程】
  6. postman是如何使用的?
  7. phpStorm pycharm编辑器主题修改,自定义颜色
  8. ASIHTTPRequest中文入门教程全集 http://www.zpluz.com/thread-3284-1-1.html
  9. vue项目在APP禁止页面缩放
  10. 【python】-- 继承式多线程、守护线程