Java线性表之顺序表

/**
 * 线性表的接口
 */
public interface IList {
    public void clear(); // 将一个已经存在的线性表置成空表

    public boolean isEmpty(); // 判断当前线性表中的数据元素个数是否为0,若为0则函数返回true,否则返回false

    public int length(); // 求线性表中的数据元素个数并由函数返回其值

    public Object get(int i) throws Exception;// 读取到线性表中的第i个数据元素并由函数返回其值。其中i取值范围为:0≤i≤length()-1,如果i值不在此范围则抛出异常

    public void insert(int i, Object x) throws Exception;// 在线性表的第i个数据元素之前插入一个值为x的数据元素。其中i取值范围为:0≤i≤length()。如果i值不在此范围则抛出异常,i=0时表示在表头插入一个数据元素x,i=length()时表示在表尾插入一个数据元素x

    public void remove(int i) throws Exception;// 将线性表中第i个数据元素删除。其中i取值范围为:0≤i≤length()- 1,如果i值不在此范围则抛出异常

    public int indexOf(Object x);// 返回线性表中首次出现指定元素的索引,如果列表不包含此元素,则返回 -1

    public void display();// 输出线性表中的数据元素

}
 
/**
 * 顺序线性表及其基本操作
 */
public class SqList implements IList {
    private Object[] listElem; // 线性表存储空间

    private int curLen; // 当前长度

    // 顺序表的构造函数,构造一个存储空间容量为maxSize的线性表
    public SqList(int maxSize) {
        curLen = 0; // 置顺序表的当前长度为0
        listElem = new Object[maxSize];// 为顺序表分配maxSize个存储单元
    }

    // 将一个已经存在的线性表置成空表
    public void clear() {
        curLen = 0; // 置顺序表的当前长度为0

    }

    // 判断当前线性表中数据元素个数是否为0,若为0则函数返回true,否则返回false
    public boolean isEmpty() {
        return curLen == 0; //布尔表达式
    }

    // 求线性表中的数据元素个数并由函数返回其值
    public int length() {
        return curLen; // 返回顺序表的当前长度
    }

    // 读取到线性表中的第i个数据元素并由函数返回其值。其中i取值范围为:0≤i≤length()-1,如果i值不在此范围则抛出异常
    //    在计算机科学中,随机存取(有时亦称直接访问)代表同一时间访问一组序列中的一个随意组件
    public Object get(int i) throws Exception {
        if (i < 0 || i > curLen - 1) // i小于0或者大于表长减1
            throw new Exception("" + i + "个元素不存在"); // 输出异常

        return listElem[i]; // 返回顺序表中第i个数据元素
    }

    // 在线性表的第i个数据元素之前插入一个值为x的数据元素。其中i取值范围为:0≤i≤length()。如果i值不在此范围则抛出异常,i=0时表示在表头插入一个数据元素x,i=length()时表示在表尾插入一个数据元素x
    public void insert(int i, Object x) throws Exception {
        if (curLen == listElem.length) // 判断顺序表是否已满
            throw new Exception("顺序表已满");// 输出异常

        if (i < 0 || i > curLen) // i小于0或者大于表长
            throw new Exception("插入位置不合理");// 输出异常

        for (int j = curLen; j > i; j--)
            listElem[j] = listElem[j - 1];// 插入位置及之后的元素后移

        listElem[i] = x; // 插入x
        curLen++;// 表长度增1
    }

    public void remove1(int index) throws  Exception{
        if (index < 0 || index > curLen) // i小于0或者大于表长
            throw new Exception("插入位置不合理");// 输出异常
        //i = index index赋值给i,这样可以避免index被修改
//        if (index == curLen-1)
//           curLen--;
//        for (int i = index; i < curLen; i++)
//            listElem[i] = listElem[i+1];
//        curLen--;
        for (int i = 0; i < curLen - 1; i++) {
            listElem[i] = listElem[i+1];
        }
        curLen--;


    }


    // 将线性表中第i个数据元素删除。其中i取值范围为:0≤i≤length()- 1,如果i值不在此范围则抛出异常
    public void remove(int i) throws Exception {
        if (i < 0 || i > curLen - 1) // i小于1或者大于表长减1
            throw new Exception("删除位置不合理");// 输出异常

        for (int j = i; j < curLen - 1; j++)
            listElem[j] = listElem[j + 1];// 被删除元素之后的元素左移

        curLen--; // 表长度减1
    }

    public int indexOf1(Object x){
        int i = 0;
        for (; i < curLen; i++) {
            if (listElem[i].equals(x)) {
                break;
            }
        }
        if (i == curLen){
            return -1;
        }
        return i;
    }


    // 返回线性表中首次出现指定元素的索引,如果列表不包含此元素,则返回 -1
    public int indexOf(Object x) {
        int j = 0;// j为计数器
        while (j < curLen && !listElem[j].equals(x))
            // 从顺序表中的首结点开始查找,直到listElem[j]指向元素x或到达顺序表的表尾
            j++;
        if (j < curLen)// 判断j的位置是否位于表中
            return j; // 返回x元素在顺序表中的位置
        else
            return -1;// x元素不在顺序表中
    }

    // 输出线性表中的数据元素
    public void display() {
        for (int j = 0; j < curLen; j++)
            System.out.print(listElem[j] + " ");
        System.out.println();// 换行

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值