0
点赞
收藏
分享

微信扫一扫

Java语言——顺序表的实现

b91bff6ffdb5 2022-03-11 阅读 63

顺序表是线性表的顺序存储结构,它使用一堆数组依次存放线性表中的数据元素

以下为线性表的设计代码可实现数据元素增、删、减、查

代码如下:

/**
*author 小鱼骨头
*data 2022-3-6
*/
//顺序表类:T 表示数据元素的数据类型。默认继承Object类
public class SequenceList<T> extends Object {
    //顺序表元素个数(长度)
    protected int n;
    //对象数组存储顺序表的数据元素,保护成员
    protected Object[] element;
    //常量,指定element数组容量的最小值
    private static final int MIN_CAPACITY = 16;

    //构建空表,length指定数组容量,若length < MIN_CAPACITY,取最小值
    public SequenceList(int length) {
        if (length < MIN_CAPACITY)
            length = MIN_CAPACITY;
        this.element = new Object[length];
        //申请数组空间,元素为null
        this.n = 0;
    }

    //创建默认容量的空表,构造方法重载
    public SequenceList() {
        this(MIN_CAPACITY);
    }

    //构建顺序表,由values数组提供元素并忽略其中空值
    public SequenceList(T[] values) {
        //创建2倍values数组容量,若values==null,则抛出NullPointerException空对象异常
        this(values.length * 2);
        //复制非null的数组元素
        for (int i = 0; i < values.length; i++) {
            if (values[i] != null)
                //对象引用赋值
                this.element[this.n++] = values[i];
            throw new NullPointerException();
        }
    }

    //判断是否为空,若为空返回true
    public boolean isEmpty() {
        return this.n == 0;
    }

    //返回元素个数
    public int size() {
        return this.n;
    }

    //若 0 <= i < n则返回第i个元素,否则返回null
    public T get(int i) {
        if (i >= 0 && i < this.n)
            return (T) this.element[i];
        return null;
    }

    //若 0 <= i < n且x != null,则设置第i个元素为x,否则抛出序号越界异常或空对象异常
    public void set(int i, T x) {
        if (x == null)
            throw new NullPointerException("x==null");
        if (i >= 0 && i < this.n)
            this.element[i] = x;
        else
            throw new java.lang.IndexOutOfBoundsException(i + "");
    }

    //返回所有元素的描述字符串,形式为“(,)”。覆盖Object类的toString方法。
    public String toString() {
        //返回类名
        String str = this.getClass().getName() + "(";
        if (this.n > 0)
            str += this.element[0].toString();
        for (int i = 0; i < this.n; i++)
            str += "," + this.element[i].toString();
        return str + ")";
    }

    //返回所有元素的描述字符串,次序从后向前
    public String toPreviousString() {
        //返回类名
        String str = this.getClass().getName() + "(";
        if (this.n > 0)
            str += this.element[n].toString();
        for (int i = this.n; i > 0; i--)
            str += "," + this.element[i].toString();
        return str + ")";
    }

    //插入x为第i个元素,x!=null,返回插入序号。对i进行容错,若 i<0,则头插入;若i>数组长度,则尾插入。 
    public int insert(int i, T x) {
        if (x == null)
            return -1;
        //插入位置i容错,插入在最前面(头插入)
        if (i < 0)
            i = 0;
        //尾插入
        if (i > this.n)
            i = this.n;
        //数组变量引用赋值,source也引用element数组
        Object[] source = this.element;
        //若数组数据溢出,则扩充顺序表的数组容量
        if (this.n == element.length) {
            //申请一个更大的数组
            this.element = new Object[source.length * 2];
            //复制当前数组元素,并传递对象引用
            for (int j = 0; j < i; j++)
                this.element[j] = source[j];
        }
        //从i开始至表尾的元素向后移动,次序从后向前
        for (int j = this.n - 1; j >= i; j--)
            //复制数组元素传递对象引用
            this.element[j + 1] = source[j];
        this.element[i] = x;
        this.n++;
        //返回插入元素序号
        return i;
    }

    //成员方法重载,顺序表尾插入元素x
    public int insert(T x) {
        //调用insert(i,x)方法
        return this.insert(this.n, x);
    }

    //删除第i个元素,0<=i<n,返回被删除元素,若i越界,则返回null
    public T remove(int i) {
        if (0 <= i && i < this.n) {
            //x中存储被删除元素
            T x = (T) this.element[i];
            for (int j = i; j < this.n; j++)
                //元素向前移动一个位置
                this.element[j] = this.element[j + 1];
            //设置数组元素对象为null,释放原引用实例
            this.element[this.n - 1] = null;
            this.n--;
            //返回x局部变量引用的对象,传递对象引用
            return x;
        }
        return null;
    }

    //清除所有元素
    public void clear() {
        //将数组长度设置为0,未释放数组空间
        this.n = 0;
    }

    //顺序表的查找
    //在this引用的顺序表中,顺序查找首个与key相等的元素,返回元素序号i,0<=i<n;若查找不成功,返回-1
    //key元素包含作为查找依据的数据项,由T类的equals()方法确定对像是否相等
    //若key==null,则Java抛出NullPointerException空对象异常
    public int search(T key) {
        for (int i = 0; i < this.n; i++)
            if (key.equals(this.element[i]))
                return i;
        //当未找到或表为空时,则返回-1
        return -1;
    }

    //顺序查找并删除首个与key相同的元素,返回被删除元素;若查找不成功,则返回null
    public T remove(T key) {
        for (int i = 0; i < this.n; i++) {
            if (key.equals(this.element[i])) {
                //temp中存储查找到的待删除元素
                T temp = (T) this.element[i];
                for (int j = i; j < this.n - 1; j++)
                    //元素向前移动一个位置
                    this.element[j] = this.element[j + 1];
                //设置数组元素对象为空,释放原引用实例
                this.element[this.n - 1] = null;
                this.n--;
            }
            return null;
        }
        return key;
    }

    //若this和obj对象引用同一个实例,则返回true
    public boolean equals(Object obj) {
        return this == obj;
    }


}

由于时间有限,上述功能尚未进行测试验证,若读者测试发现问题还望指正。

举报

相关推荐

0 条评论