线性表的游标实现_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 ,如需转载请自行联系原作者
相关文章
|
10月前
|
安全 Java 编译器
new出来的对象,不一定在堆上?聊聊Java虚拟机的优化技术:逃逸分析
逃逸分析是一种静态程序分析技术,用于判断对象的可见性与生命周期。它帮助即时编译器优化内存使用、降低同步开销。根据对象是否逃逸出方法或线程,分析结果分为未逃逸、方法逃逸和线程逃逸三种。基于分析结果,编译器可进行同步锁消除、标量替换和栈上分配等优化,从而提升程序性能。尽管逃逸分析计算复杂度较高,但其在热点代码中的应用为Java虚拟机带来了显著的优化效果。
303 4
|
8月前
|
设计模式 算法 搜索推荐
Java 设计模式之策略模式:灵活切换算法的艺术
策略模式通过封装不同算法并实现灵活切换,将算法与使用解耦。以支付为例,微信、支付宝等支付方式作为独立策略,购物车根据选择调用对应支付逻辑,提升代码可维护性与扩展性,避免冗长条件判断,符合开闭原则。
2093 35
|
8月前
|
存储 算法 搜索推荐
《数据之美》:Java数据结构与算法精要
本系列深入探讨数据结构与算法的核心原理及Java实现,涵盖线性与非线性结构、常用算法分类、复杂度分析及集合框架应用,助你提升程序效率,掌握编程底层逻辑。
|
8月前
|
存储 Java Go
【Java】(3)8种基本数据类型的分析、数据类型转换规则、转义字符的列举
牢记类型转换规则在脑海中将编译和运行两个阶段分开,这是两个不同的阶段,不要弄混!
362 2
|
8月前
|
Java Go 开发工具
【Java】(9)抽象类、接口、内部的运用与作用分析,枚举类型的使用
抽象类必须使用abstract修饰符来修饰,抽象方法也必须使用abstract修饰符来修饰,抽象方法不能有方法体。抽象类不能被实例化,无法使用new关键字来调用抽象类的构造器创建抽象类的实例。抽象类可以包含成员变量、方法(普通方法和抽象方法都可以)、构造器、初始化块、内部类(接 口、枚举)5种成分。抽象类的构造器不能用于创建实例,主要是用于被其子类调用。抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类abstract static不能同时修饰一个方法。
329 1
|
9月前
|
数据采集 存储 弹性计算
高并发Java爬虫的瓶颈分析与动态线程优化方案
高并发Java爬虫的瓶颈分析与动态线程优化方案
|
8月前
|
存储 人工智能 算法
从零掌握贪心算法Java版:LeetCode 10题实战解析(上)
在算法世界里,有一种思想如同生活中的"见好就收"——每次做出当前看来最优的选择,寄希望于通过局部最优达成全局最优。这种思想就是贪心算法,它以其简洁高效的特点,成为解决最优问题的利器。今天我们就来系统学习贪心算法的核心思想,并通过10道LeetCode经典题目实战演练,带你掌握这种"步步为营"的解题思维。
|
11月前
|
机器学习/深度学习 算法 Java
Java实现林火蔓延路径算法
记录正在进行的森林防火项目中林火蔓延功能,本篇文章可以较好的实现森林防火蔓延,但还存在很多不足,如:很多参数只能使用默认值,所以蔓延范围仅供参考。(如果底层设备获取的数据充足,那当我没说)。注:因林火蔓延涉及因素太多,如静可燃物载量、矿质阻尼系数等存在估值,所以得出的结果仅供参考。
491 5
|
11月前
|
存储 负载均衡 算法
我们来说一说 Java 的一致性 Hash 算法
我是小假 期待与你的下一次相遇 ~
580 1
|
10月前
|
机器学习/深度学习 安全 Java
Java 大视界 -- Java 大数据在智能金融反洗钱监测与交易异常分析中的应用(224)
本文探讨 Java 大数据在智能金融反洗钱监测与交易异常分析中的应用,介绍其在数据处理、机器学习建模、实战案例及安全隐私等方面的技术方案与挑战,展现 Java 在金融风控中的强大能力。