public class MyStack<AnyType> {
private AnyType [] theItems;
private final int DEFAULT_CAPACITY = 100;
private int top; public MyStack(){ //构造函数,生成空表
clear();
} public void clear(){ //归为默认(清空)
top = 0;
ensureCapacity(DEFAULT_CAPACITY);
} public void ensureCapacity(int newCapacity ){ //重置表的容量
AnyType [] oldItems = theItems;
theItems = (AnyType []) new Object[newCapacity];//****重新分配空间 注意使用强制转换的方式进行定义
for (int i = 0 ; i < top; i++){
theItems[i] = oldItems[i];
}
} public boolean isEmpty(){ //判断是否为空
return top == 0;
} public boolean push( AnyType newVal){//末尾添加元素
if(theItems.length == top)
ensureCapacity(top * 2 + 1);
theItems[top++] = newVal;
return true;
} public AnyType pop(){
if(top == 0)
throw new NoSuchElementException();
return theItems[--top];
}
}

Stack的实现的更多相关文章

  1. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  2. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  3. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  4. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

  5. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  6. Stack操作,栈的操作。

    栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  9. Stack的三种含义

    作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...

  10. Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)

    写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...

随机推荐

  1. 用记事本编写C#程序并运行C#代码

    net framework自带有C#编译器 csc.exe,用它就好了 它在.NET框架目录下的<\Microsoft.NET\Framework\v**** (*号内容与版本有关) 不行你直接 ...

  2. Haproxy+PXC实现负载均衡

    软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第 三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载.HA ...

  3. mysql 的replace 和replace in to

    1.  mysql 的replace  批量替换 update candidate set education = replace(education,'科','学') where education ...

  4. sublime远程连接到linux主机

    sublime远程连接到linux主机 sublime远程连接到linux主机 微信开发,直接使用sublime的sftp功能修改wx_sample.php 1.为sublime安装安装包管理插件Pa ...

  5. shopnc数据库 批量修改商品价格

    1.商品价格统一上调50 2.商品价格个别上调50 UPDATE `nc_goods` SET `goods_price` = `goods_price` +50 where goods_id!=10 ...

  6. Yii系列总结:yii 标签用法

    yii 常用标签:label标签.文本标签.error标签.textarea标签.hidden标签.password标签.url标签.radio标签.file标签.button标签.checkBox标 ...

  7. Ubuntu14.04安装配置SVN及Trac

    还是个实习生的时候,项目管理十分欠缺,会出现很多问题,痛定思痛,决定要改变现状,养成良好的项目管理习惯,看网上工具很多,在这里尝试使用SVN作代码版本控制,使用trac作为项目管理追踪.本文采用的操作 ...

  8. Holding Bin-Laden Captive!(hdoj1085)代码并未完全看懂

    We all know that Bin-Laden is a notorious terrorist, and he has disappeared for a long time. But rec ...

  9. Apache+php+mysql win7 64位安装的几个注意事项

    网上一堆安装教程,所以不赘述具体安装过程,只说注意事项.新手推荐phpstudy 如果想单个安装,那么以下是我两三年内多次在win winserver环境下配置Apache环境的一点注意事项,下载连接 ...

  10. KNN(k-nearest neighbor的缩写)又叫最近邻算法

    KNN(k-nearest neighbor的缩写)又叫最近邻算法 机器学习笔记--KNN算法1 前言 Hello ,everyone. 我是小花.大四毕业,留在学校有点事情,就在这里和大家吹吹我们的 ...