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

简介:
主类
None.gif package DataStructures;
None.gif
ExpandedBlockStart.gif public  class ArrayStack  {
InBlock.gif    private Object[] theArray;
InBlock.gif
InBlock.gif    private int topOfStack;
InBlock.gif
InBlock.gif    static final int DEFAULT_CAPACITY = 10;
InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Construct the stack.
InBlock.gif     * 
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public ArrayStack() {
InBlock.gif        this(DEFAULT_CAPACITY);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Construct the stack.
InBlock.gif     * 
InBlock.gif     * 
@param capacity
InBlock.gif     *            teh capacity.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public ArrayStack(int capacity) {
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public boolean IsEmpty() {
InBlock.gif        return topOfStack == -1;
ExpandedSubBlockEnd.gif    }

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

InBlock.gif
ExpandedSubBlockStart.gif    public void MakeEmpty() {
InBlock.gif        topOfStack = -1;
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockStart.gif    public void Push(Object x) throws Overflow {
InBlock.gif        if (IsFull())
InBlock.gif            throw new Overflow();
InBlock.gif
InBlock.gif        theArray[++topOfStack] = x;
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockStart.gif    public Object Top() {
InBlock.gif        if (IsEmpty())
InBlock.gif            return null;
InBlock.gif        return theArray[topOfStack];
ExpandedSubBlockEnd.gif    }

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

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

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

ExpandedBlockEnd.gif}

None.gif
异常类
None.gif package DataStructures;
None.gif
ExpandedBlockStart.gif /**
InBlock.gif * Exception class for access in full containers such as stacks, queues, and
InBlock.gif * priority queues.
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gif public  class Overflow  extends Exception  {
ExpandedBlockEnd.gif}

本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/08/23/484641.html ,如需转载请自行联系原作者
相关文章
|
8天前
|
算法 安全 Java
性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
【4月更文挑战第28天】性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
24 1
性能工具之 JMeter 自定义 Java Sampler 支持国密 SM2 算法
|
1天前
|
算法 测试技术 C++
【栈 最小公倍数 最大公约数】2197. 替换数组中的非互质数
【栈 最小公倍数 最大公约数】2197. 替换数组中的非互质数
【栈 最小公倍数 最大公约数】2197. 替换数组中的非互质数
|
1天前
|
机器学习/深度学习 算法 测试技术
【单调栈】3113. 边界元素是最大值的子数组数目
【单调栈】3113. 边界元素是最大值的子数组数目
|
2天前
|
C语言
猿创征文|栈和队列OJ刷题
猿创征文|栈和队列OJ刷题
[数据结构]~栈和队列(0-1)
[数据结构]~栈和队列(0-1)
|
6天前
|
机器学习/深度学习 人工智能 算法
【AI 初识】描述遗传算法概念
【5月更文挑战第2天】【AI 初识】描述遗传算法概念
|
8天前
|
存储
栈与队列练习题
栈与队列练习题
|
8天前
|
存储 索引
操作数栈的字节码指令执行分析
操作数栈的字节码指令执行分析
|
8天前
|
算法 C++
D1. Range Sorting (Easy Version)(单调栈+思维)
D1. Range Sorting (Easy Version)(单调栈+思维)
|
8天前
|
人工智能
线段树最大连续子段板子😂单调栈
线段树最大连续子段板子😂单调栈