数据结构(java版)SortedSeqList(排序顺序表)

SortedSeqList(排序顺序表)

代码部分:

public class SortedSeqList<T extends Comparable<? super T>> extends SeqList<T> {

    //构造函数2,默认长度,构造方法重载
    public SortedSeqList() {
        super();
    }

    //构造函数1,固定长度
    public SortedSeqList(int size) {
        super(size);
    }

    //构造函数3,由values数组提供元素
    public SortedSeqList(T[] values) {
        super(values.length); // 调用父类方法,创建固定长度的空排序顺序表
        for (int i = 0; i < values.length; i++)
            this.insert(values[i]); // 这里的插入是调用子类插入方法,可以有序插入
    }

    //有序插入
    public int insert(T x) {
        int i = 0;
        if (!this.isEmpty() && x.compareTo(this.get(this.len - 1)) <= 0) {
            while(i < this.len && x.compareTo(this.get(i)) > 0) {
                ++i;
            }
        } else {
            i = this.len;
        }
        super.insert(i, x);
        return i;
    }

    // 冲突重写报异常
    public void set(int i, T x) {
        throw new java.lang.UnsupportedOperationException("set(int i,T x)");
    }

    public int insert(int i, T x){
        throw new UnsupportedOperationException("insert(int i,T x)");
    }

    public int search(T key) {
        for (int i = 0; i < this.len&& key.compareTo(this.get(i)) >= 0 ; i++) {
            if(element[i]==null){
                return -1;
            }
            if (key.compareTo(this.get(i)) == 0)
                return i+1; // 如果相等,就返回元素序号
        }
        return -1; // 未查找成功
    }

      //覆盖父类的删除方法
    public T remove(T key) {
        return this.remove(this.search(key));
    }

}

写在最后:

仅为学习阶段记录写过的代码,方便自己查找,初出茅庐,有不完善的地方烦请指正,如有继承类可查阅数据结构其他博文。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AntyRia

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值