线性表的游标实现_JAVA描述《数据结构与算法分析》

简介:
结点类
package DataStructures;

class CursorNode  {
    // Friently data;accessible by other package routines
    Object element;

    int next;

    // Constructers
    CursorNode(Object theElement) {
        this(theElement, 0);
    }


    CursorNode(Object theElement, int n) {
        element = theElement;
        next = n;
    }

}


迭代器
package DataStructures;

public  class CursorListItr  {
    int current; // Current position

    CursorListItr(int theNode) {
        current = theNode;
    }


    public boolean IsPastEnd() {
        return current == 0;
    }


    public Object Retrieve() {
        return IsPastEnd() ? null : CursorList.cursorSpace[current].element;
    }


    public void Advance() {
        if (!IsPastEnd()) {
            current = CursorList.cursorSpace[current].next;
        }

    }

}


主类
package DataStructures;

public  class CursorList  {
    private int header;

    static CursorNode[] cursorSpace;

    private static final int SPACE_SIZE = 100;

    static {
        cursorSpace = new CursorNode[SPACE_SIZE];
        for (int i = 0; i < SPACE_SIZE; i++) {
            cursorSpace[i] = new CursorNode(null, i + 1);
        }

        cursorSpace[SPACE_SIZE - 1].next = 0;
    }


    private static int alloc() {
        int p = cursorSpace[0].next;
        cursorSpace[0].next = cursorSpace[p].next;
        if (p == 0)
            throw new OutOfMemoryError();
        return p;
    }


    private static void free(int p) {
        cursorSpace[p].element = null;
        cursorSpace[p].next = cursorSpace[0].next;
        cursorSpace[0].next = p;
    }


    public CursorList() {
        header = alloc();
        cursorSpace[header].next = 0;
    }


    public boolean IsEmpty() {
        return cursorSpace[header].next == 0;
    }


    /**
     * Make the list logically empty.
     
*/

    public void MakeEmpty() {
        while (!IsEmpty())
            Remove(First().Retrieve());
    }


    public CursorListItr Zeroth() {
        return new CursorListItr(header);
    }


    public CursorListItr First() {
        return new CursorListItr(cursorSpace[header].next);
    }


    /**
     * Return iterator corresponding to the first node containing an tiem.
     * 
     * 
@param x
     *            the item to search for
     * 
@return an iterator;iterator IsPastEnd if item is not found.
     
*/

    public CursorListItr Find(Object x) {
        int itr = cursorSpace[header].next;

        while (itr != 0 && cursorSpace[itr].element.equals(x))
            itr = cursorSpace[itr].next;

        return new CursorListItr(itr);
    }


    /**
     * Insert after p.
     * 
     * 
@param x
     *            the item to insert.
     * 
@param p
     *            the position prior to the newly inserted item.
     
*/

    public void Insert(Object x, CursorListItr p) {
        if (p != null && p.current != 0) {
            int pos = p.current;
            int tmp = alloc();

            cursorSpace[tmp].element = x;
            cursorSpace[tmp].next = cursorSpace[pos].next;
            cursorSpace[pos].next = tmp;
        }

    }


    /**
     * Remove the first occurence of an item.
     * 
     * 
@param x
     *            the item to remove.
     
*/

    public void Remove(Object x) {
        CursorListItr p = FindPrevious(x);
        int pos = p.current;

        if (cursorSpace[pos].next != 0) {
            int tmp = cursorSpace[pos].next;
            cursorSpace[pos].next = cursorSpace[tmp].next;
            free(tmp);
        }

    }


    /**
     * Return iterator prior to the first node containing an item.
     * 
     * 
@param x
     *            the item to search for.
     * 
@return appropriate iterator if the item is found. Otherwise, the
     *         iterator corresponding to the last element in the list is
     *         returned.
     
*/

    public CursorListItr FindPrevious(Object x) {
        int itr = header;

        while (cursorSpace[itr].next != 0
                && !cursorSpace[cursorSpace[itr].next].element.equals(x))
            itr = cursorSpace[itr].next;

        return new CursorListItr(itr);        
    }

}

本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/08/20/481986.html ,如需转载请自行联系原作者
相关文章
|
存储 缓存 监控
上网行为监控系统剖析:基于 Java LinkedHashMap 算法的时间序列追踪机制探究
数字化办公蓬勃发展的背景下,上网行为监控系统已成为企业维护信息安全、提升工作效能的关键手段。该系统需实时记录并深入分析员工的网络访问行为,如何高效存储和管理这些处于动态变化中的数据,便成为亟待解决的核心问题。Java 语言中的LinkedHashMap数据结构,凭借其独有的有序性特征以及可灵活配置的淘汰策略,为上网行为监控系统提供了一种兼顾性能与功能需求的数据管理方案。本文将对LinkedHashMap在上网行为监控系统中的应用原理、实现路径及其应用价值展开深入探究。
334 3
|
10月前
|
设计模式 算法 搜索推荐
Java 设计模式之策略模式:灵活切换算法的艺术
策略模式通过封装不同算法并实现灵活切换,将算法与使用解耦。以支付为例,微信、支付宝等支付方式作为独立策略,购物车根据选择调用对应支付逻辑,提升代码可维护性与扩展性,避免冗长条件判断,符合开闭原则。
2697 35
|
人工智能 算法 NoSQL
LRU算法的Java实现
LRU(Least Recently Used)算法用于淘汰最近最少使用的数据,常应用于内存管理策略中。在Redis中,通过`maxmemory-policy`配置实现不同淘汰策略,如`allkeys-lru`和`volatile-lru`等,采用采样方式近似LRU以优化性能。Java中可通过`LinkedHashMap`轻松实现LRUCache,利用其`accessOrder`特性和`removeEldestEntry`方法完成缓存淘汰逻辑,代码简洁高效。
636 0
|
10月前
|
存储 算法 搜索推荐
《数据之美》:Java数据结构与算法精要
本系列深入探讨数据结构与算法的核心原理及Java实现,涵盖线性与非线性结构、常用算法分类、复杂度分析及集合框架应用,助你提升程序效率,掌握编程底层逻辑。
|
存储 算法 安全
Java中的对称加密算法的原理与实现
本文详细解析了Java中三种常用对称加密算法(AES、DES、3DES)的实现原理及应用。对称加密使用相同密钥进行加解密,适合数据安全传输与存储。AES作为现代标准,支持128/192/256位密钥,安全性高;DES采用56位密钥,现已不够安全;3DES通过三重加密增强安全性,但性能较低。文章提供了各算法的具体Java代码示例,便于快速上手实现加密解密操作,帮助用户根据需求选择合适的加密方案保护数据安全。
944 58
|
10月前
|
存储 人工智能 算法
从零掌握贪心算法Java版:LeetCode 10题实战解析(上)
在算法世界里,有一种思想如同生活中的"见好就收"——每次做出当前看来最优的选择,寄希望于通过局部最优达成全局最优。这种思想就是贪心算法,它以其简洁高效的特点,成为解决最优问题的利器。今天我们就来系统学习贪心算法的核心思想,并通过10道LeetCode经典题目实战演练,带你掌握这种"步步为营"的解题思维。
|
机器学习/深度学习 算法 Java
Java实现林火蔓延路径算法
记录正在进行的森林防火项目中林火蔓延功能,本篇文章可以较好的实现森林防火蔓延,但还存在很多不足,如:很多参数只能使用默认值,所以蔓延范围仅供参考。(如果底层设备获取的数据充足,那当我没说)。注:因林火蔓延涉及因素太多,如静可燃物载量、矿质阻尼系数等存在估值,所以得出的结果仅供参考。
618 5
|
存储 负载均衡 算法
我们来说一说 Java 的一致性 Hash 算法
我是小假 期待与你的下一次相遇 ~
729 1
|
运维 监控 算法
基于 Java 滑动窗口算法的局域网内部监控软件流量异常检测技术研究
本文探讨了滑动窗口算法在局域网流量监控中的应用,分析其在实时性、资源控制和多维分析等方面的优势,并提出优化策略,结合Java编程实现高效流量异常检测。
501 0
|
存储 监控 算法
企业上网监控场景下布隆过滤器的 Java 算法构建及其性能优化研究
布隆过滤器是一种高效的数据结构,广泛应用于企业上网监控系统中,用于快速判断员工访问的网址是否为违规站点。相比传统哈希表,它具有更低的内存占用和更快的查询速度,支持实时拦截、动态更新和资源压缩,有效提升系统性能并降低成本。
639 0

热门文章

最新文章