Java实现顺序线性表

线性表

线性表是最简单和最常用的一种数据结构,它是有n个体数据元素(节点)组成的有限序列。其中,数据元素的个数n为表的长度,当n为零时成为空表,非空的线性表通常记为:

(a1,a2,… ,ai-1,ai, ai+1,…,an)

线性表的顺序存储

线性表的顺序存储指的是将线性表的数据元素按其逻辑次序依次存入一组地址连续的存储单元里,用这种方法存储的线性表称为顺序表。

数据类:ElemType.java

//数据
public class ElemType {
    private int no;
    private String name;
    private String sex;
    private int age;

    //无参构造方法
    public ElemType() {
    }
    public ElemType(int no, String name, String sex, int age) {
        this.no = no;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "ElemType{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

线性表之顺序表:SqList.java

import java.util.Arrays;

//线性表之顺序表
public class SqList {
    private final int maxLength;//最大长度
    private int curLength;//当前长度
    private ElemType[] list;//存储数据元素的空间

    public SqList(int maxSize) {//构造方法,以参数maxSize作为顺序表的最大容量
        maxLength = maxSize;
        list = new ElemType[maxSize];
    }

    //清空
    public void clear() {
        curLength = 0;
    }

    //是否为空
    public boolean isEmpty() {
        return curLength == 0;
    }

    //是否满
    public boolean isFull() {
        return curLength == maxLength;
    }

    //获取当前表的长度
    public int getCurLength() {
        return curLength;
    }

    //根据下标返回对应的数据
    public ElemType getElem(int i) throws Exception {
        if (i < 0 || i > curLength) {
            throw new Exception("第" + i + "个元素不存在");
        }
        return list[i];
    }

    //根据数据元素找到对应的所在位置下标
    public int getIndex(ElemType e) throws Exception {
        for (int i = 0; i < curLength; i++) {
            if (list[i].toString().equals(e.toString())) {//内容全部相同
                return i;
            }
        }
        //抛异常不用返回值
        throw new Exception("不存在你要查找的元素");
    }

    //插入数据
    public void insert(int i, ElemType e) {
        if (isFull()) // 判断顺序表是否已满
        {//进行扩容
            System.out.println("顺序表已满,已扩容");
            list = Arrays.copyOf(list, 2 * curLength);
        }
        if (i < 0 || i > curLength) // i小于0或者大于表长
            System.out.println("插入位置不合理,插入失败");// 输出错误提示
        else {
            //把在list[i]之前的数据往后移一,以便插入
            for (int j = curLength - 1; j >= i; j--) {
                list[j + 1] = list[j];
            }

            list[i] = e;
            curLength++;//当前长度必须增加1
            System.out.println("插入成功");
        }
    }

    //删除
    public void delete(int i) {
        if (i < 0 || i > curLength - 1) // i小于0或者大于表长
            System.out.println("删除位置不合理");
        else {

            for (int j = i; j < curLength - 1; j++) {
                list[j] = list[j + 1];
            }
            curLength--;//当前长度必须减少1
            System.out.println("list[" + i + "]的数据删除成功");
        }

    }

    //修改数据
    public void update(int i, ElemType reUp) throws Exception {
        for (int j = 0; j < curLength; j++) {
            //遍历顺序表,找到与list[i](即getElem(i))对应的数据所在的位置
            //将其修改为用户想修改的ElemType数据对象
            if (list[j].toString().equals(getElem(i).toString())) {
                list[j]=reUp;//修改对象
            }
        }
        System.out.println("修改成功");
    }

    //遍历查询
    public void show() {
        for (int i = 0; i < curLength; i++) {
            System.out.println(list[i].toString());
        }
    }

}

测试类:SqListDemo.java

//测试类
public class SqListDemo {
    public static void main(String[] args) throws Exception {
        //长度为10
        SqList sqList = new SqList(8);

        ElemType elem1 = new ElemType(1, "鸢一折纸", "女", 19);
        ElemType elem2 = new ElemType(2, "本条二亚", "女", 32);
        ElemType elem3 = new ElemType(3, "时崎狂三", "女", 18);
        ElemType elem4 = new ElemType(4, "四糸乃", "女", 16);
        ElemType elem5 = new ElemType(6, "星宫六飱", "女", 17);
        sqList.clear();
        System.out.println("是否为空:" + sqList.isEmpty());
        System.out.println("插入操作:");
        try {
            sqList.insert(0, elem1);
            sqList.insert(1, elem2);
            sqList.insert(2, elem3);

            sqList.insert(2, elem4);
            sqList.insert(3,elem5);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        System.out.println("是否为空:" + sqList.isEmpty());
        sqList.show();
        System.out.println("删除操作:");
        sqList.delete(1);
        sqList.show();
        System.out.println("修改操作:");
        sqList.update(4,new ElemType(1,"1","1",1));
        sqList.show();
        System.out.println("查询操作:");
        System.out.println("对象"+elem1+"在顺序表的下标为"+sqList.getIndex(elem1)+"位置");

    }
}

运行结果

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值