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

简介:

 

主类

None.gif package DataStructures;
None.gif
ExpandedBlockStart.gif public  class LinkedStack  {
InBlock.gif    private ListNode topOfStack;
InBlock.gif
ExpandedSubBlockStart.gif    public LinkedStack() {
InBlock.gif        topOfStack = null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    public boolean IsFull() {
InBlock.gif        return false;
ExpandedSubBlockEnd.gif    }

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

InBlock.gif
ExpandedSubBlockStart.gif    public void makeEmpty() {
InBlock.gif        topOfStack = null;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Insert a new item into the stack.
InBlock.gif     * 
InBlock.gif     * 
@param x
InBlock.gif     *            the item to insert.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public void Push(Object x) {
InBlock.gif        topOfStack = new ListNode(x, topOfStack);
ExpandedSubBlockEnd.gif    }

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

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

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Remove the most recenly inserted item from the stack.
InBlock.gif     * 
InBlock.gif     * 
@throws Underflow
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public void Pop() throws Underflow {
InBlock.gif        if (IsEmpty())
InBlock.gif            throw new Underflow();
InBlock.gif
InBlock.gif        topOfStack = topOfStack.next;
ExpandedSubBlockEnd.gif    }

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

ExpandedSubBlockStart.gif    public Object TopAndPop() {
InBlock.gif        if (IsEmpty())
InBlock.gif            return null;
InBlock.gif
InBlock.gif        Object topItem = topOfStack.element;
InBlock.gif        topOfStack = topOfStack.next;
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 empty containers such as stacks, queues, and
InBlock.gif * priority queues.
ExpandedBlockEnd.gif 
*/

ExpandedBlockStart.gif public  class Underflow  extends Exception  {
ExpandedBlockEnd.gif}
本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/08/23/484554.html ,如需转载请自行联系原作者
相关文章
|
2月前
|
负载均衡 NoSQL 算法
一天五道Java面试题----第十天(简述Redis事务实现--------->负载均衡算法、类型)
这篇文章是关于Java面试中Redis相关问题的笔记,包括Redis事务实现、集群方案、主从复制原理、CAP和BASE理论以及负载均衡算法和类型。
一天五道Java面试题----第十天(简述Redis事务实现--------->负载均衡算法、类型)
|
2月前
|
搜索推荐 算法 Java
手写快排:教你用Java写出高效排序算法!
快速排序(QuickSort)是经典的排序算法之一,基于分治思想,平均时间复杂度为O(n log n),广泛应用于各种场合。在这篇文章中,我们将手写一个Java版本的快速排序,从基础实现到优化策略,并逐步解析代码背后的逻辑。
79 1
|
2月前
|
设计模式 缓存 算法
揭秘策略模式:如何用Java设计模式轻松切换算法?
【8月更文挑战第30天】设计模式是解决软件开发中特定问题的可重用方案。其中,策略模式是一种常用的行为型模式,允许在运行时选择算法行为。它通过定义一系列可互换的算法来封装具体的实现,使算法的变化与客户端分离。例如,在电商系统中,可以通过定义 `DiscountStrategy` 接口和多种折扣策略类(如 `FidelityDiscount`、`BulkDiscount` 和 `NoDiscount`),在运行时动态切换不同的折扣逻辑。这样,`ShoppingCart` 类无需关心具体折扣计算细节,只需设置不同的策略即可实现灵活的价格计算,符合开闭原则并提高代码的可维护性和扩展性。
40 2
|
2月前
|
安全 算法 Java
java系列之~~网络通信安全 非对称加密算法的介绍说明
这篇文章介绍了非对称加密算法,包括其定义、加密解密过程、数字签名功能,以及与对称加密算法的比较,并解释了非对称加密在网络安全中的应用,特别是在公钥基础设施和信任网络中的重要性。
|
2月前
|
算法 Java 索引
【Java集合类面试四】、 描述一下Map put的过程
这篇文章详细描述了HashMap中put操作的过程,包括首次扩容、计算索引、插入数据以及链表转红黑树和可能的再次扩容。
【Java集合类面试四】、 描述一下Map put的过程
|
2月前
|
Java
描述 Java 中的重载和重写
【8月更文挑战第22天】
14 0
|
2月前
|
数据采集 搜索推荐 算法
【高手进阶】Java排序算法:从零到精通——揭秘冒泡、快速、归并排序的原理与实战应用,让你的代码效率飙升!
【8月更文挑战第21天】Java排序算法是编程基础的重要部分,在算法设计与分析及实际开发中不可或缺。本文介绍内部排序算法,包括简单的冒泡排序及其逐步优化至高效的快速排序和稳定的归并排序,并提供了每种算法的Java实现示例。此外,还探讨了排序算法在电子商务、搜索引擎和数据分析等领域的广泛应用,帮助读者更好地理解和应用这些算法。
24 0
|
2月前
|
存储 算法
【初阶数据结构篇】顺序表和链表算法题
此题可以先找到中间节点,然后把后半部分逆置,最近前后两部分一一比对,如果节点的值全部相同,则即为回文。
|
4月前
|
存储 SQL 算法
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
LeetCode力扣第114题:多种算法实现 将二叉树展开为链表
|
4月前
|
存储 SQL 算法
LeetCode 题目 86:分隔链表
LeetCode 题目 86:分隔链表
下一篇
无影云桌面