栈的数组实现_JAVA描述《数据结构与算法分析》

简介:
主类
package DataStructures;

public  class ArrayStack  {
    private Object[] theArray;

    private int topOfStack;

    static final int DEFAULT_CAPACITY = 10;

    /**
     * Construct the stack.
     * 
     
*/

    public ArrayStack() {
        this(DEFAULT_CAPACITY);
    }


    /**
     * Construct the stack.
     * 
     * 
@param capacity
     *            teh capacity.
     
*/

    public ArrayStack(int capacity) {

    }


    public boolean IsEmpty() {
        return topOfStack == -1;
    }


    public boolean IsFull() {
        return topOfStack == theArray.length - 1;
    }


    public void MakeEmpty() {
        topOfStack = -1;
    }


    /**
     * Insert a new item into the stack, if not already full.
     * 
     * 
@param x
     *            th item to insert.
     * 
@throws Overflow
     *             if stack is already full.
     
*/

    public void Push(Object x) throws Overflow {
        if (IsFull())
            throw new Overflow();

        theArray[++topOfStack] = x;
    }


    /**
     * Get the most recently inserted item in the stack. Dos not alter the
     * stack.
     * 
     * 
@return the most recently inserted item, or null, if empty.
     
*/

    public Object Top() {
        if (IsEmpty())
            return null;
        return theArray[topOfStack];
    }


    public void Pop() throws Underflow {
        if (IsEmpty())
            throw new Underflow();
        theArray[topOfStack--] = null;
    }


    /**
     * Return and remove most recently inserted item form the stack.
     * 
     * 
@return most recently inserted item, or null, if stack is empty.
     
*/

    public Object TopAndPop() {
        if (IsEmpty())
            return null;
        Object topItem = Top();
        theArray[topOfStack--] = null;
        return topItem;
    }

}

异常类
package DataStructures;

/**
 * Exception class for access in full containers such as stacks, queues, and
 * priority queues.
 
*/

public  class Overflow  extends Exception  {
}

本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/08/23/484641.html ,如需转载请自行联系原作者
相关文章
|
8天前
|
存储 Java
【编程基础知识】 分析学生成绩:用Java二维数组存储与输出
本文介绍如何使用Java二维数组存储和处理多个学生的各科成绩,包括成绩的输入、存储及格式化输出,适合初学者实践Java基础知识。
36 1
|
5天前
数据结构(栈与列队)
数据结构(栈与列队)
11 1
|
6天前
|
存储 Java
Java“(array) <X> Not Initialized” (数组未初始化)错误解决
在Java中,遇到“(array) &lt;X&gt; Not Initialized”(数组未初始化)错误时,表示数组变量已被声明但尚未初始化。解决方法是在使用数组之前,通过指定数组的大小和类型来初始化数组,例如:`int[] arr = new int[5];` 或 `String[] strArr = new String[10];`。
|
8天前
|
Java
让星星⭐月亮告诉你,Java synchronized(*.class) synchronized 方法 synchronized(this)分析
本文通过Java代码示例,介绍了`synchronized`关键字在类和实例方法上的使用。总结了三种情况:1) 类级别的锁,多个实例对象在同一时刻只能有一个获取锁;2) 实例方法级别的锁,多个实例对象可以同时执行;3) 同一实例对象的多个线程,同一时刻只能有一个线程执行同步方法。
9 1
|
8天前
|
小程序 Oracle Java
JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
这篇文章是关于JVM基础知识的介绍,包括JVM的跨平台和跨语言特性、Class文件格式的详细解析,以及如何使用javap和jclasslib工具来分析Class文件。
21 0
JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
|
9天前
|
存储 JavaScript 前端开发
为什么基础数据类型存放在栈中,而引用数据类型存放在堆中?
为什么基础数据类型存放在栈中,而引用数据类型存放在堆中?
37 1
|
6天前
【数据结构】-- 栈和队列
【数据结构】-- 栈和队列
9 0
|
6天前
|
存储 算法 Java
带你学习java的数组军队列
带你学习java的数组军队列
24 0
|
11天前
探索顺序结构:栈的实现方式
探索顺序结构:栈的实现方式
|
17天前
|
机器学习/深度学习 算法 数据安全/隐私保护
基于MSER和HOG特征提取的SVM交通标志检测和识别算法matlab仿真
### 算法简介 1. **算法运行效果图预览**:展示算法效果,完整程序运行后无水印。 2. **算法运行软件版本**:Matlab 2017b。 3. **部分核心程序**:完整版代码包含中文注释及操作步骤视频。 4. **算法理论概述**: - **MSER**:用于检测显著区域,提取图像中稳定区域,适用于光照变化下的交通标志检测。 - **HOG特征提取**:通过计算图像小区域的梯度直方图捕捉局部纹理信息,用于物体检测。 - **SVM**:寻找最大化间隔的超平面以分类样本。 整个算法流程图见下图。