数据结构:java顺序表的实现

定义一个List接口:

package com.edu.dataStructure.list;

 interface List {
    //获取线性表的长度
    public int getLength() ;
    //判断顺序表是不是为空
    public boolean isEmpty();
    //为顺序表添加元素
    public void insert(int index,Object x) throws  Exception;
    //为顺序表删除元素
    public Object delete(int index) throws Exception;
    //获取指定位置上的元素
    public Object get(int index)throws Exception;
    //查找顺序表中元素的位置
    public int search(Object x)throws Exception;
    //遍历元素
    public void input();
}

定义一个Sq类实现List中的方法:

package com.edu.dataStructure.list;

public class Sq implements List {
    private final int defaultsize = 10;
    private int size;
    private int max;
    Object listElem[];

    Sq() {
        init(defaultsize);
    }

    Sq(int num) {
        init(num);
    }

    public void init(int size) {
        this.max = size;
        this.size = 0;
        listElem = new Object[size];


    }

    @Override
    public int getLength() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        if (size > 0) {
            return false;
        } else {
            return true;
        }
        //或者可以这样写return size==0
    }

    @Override
    public void insert(int index, Object x) throws Exception {
        if (size == max) {
            throw new Exception("顺序表已满");
        } else if (index < 0 && index > max) {
            throw new Exception("参数错误");
        } else {
            for (int i = index; i < size; i++) {
                listElem[i + 1] = listElem[i];
            }
            listElem[index] = x;
            size++;
        }

    }

    @Override
    public Object delete(int index) throws Exception {
        if (size == 0) {
            throw new Exception("该顺序表为空");
        } else if (index < 0 || index >= size) {
            throw new Exception("参数错误");
        } else {
            Object x = listElem[index];
            for (int i = index; i < size - 1; i++) {
                listElem[i] = listElem[i + 1];
            }
            size--;
            return x;
        }
    }

    @Override
    public Object get(int index) throws Exception {
        if (index > size || index < 0) {
            throw new Exception("参数错误");
        } else {
            return listElem[index];
        }
    }

    @Override
    public int search(Object x) throws Exception {
        for (int i = 0; i < size; i++) {
            if (x.equals(listElem[i])) {
                return i;
            }
        }
        return -1;
    }

    @Override
    public void input() {
        for(Object list:listElem){
            System.out.print(list+" ");
        }
    }
}

测试类:

package com.edu.dataStructure.list;

public class Test {
    public static void main(String[] args) throws Exception {
        Sq s=new Sq(10);
        s.insert(0,5);
        s.insert(1,6);
        s.insert(2,5);
        s.insert(3,9);
        s.insert(4,7);
        System.out.println("顺序表遍历");
        s.input();
        s.delete(3);
        System.out.println();
        System.out.println("删除后的顺序表");
        s.input();


    }
}

运行结果:

顺序表遍历
5 6 5 9 7 null null null null null 
删除后的顺序表
5 6 5 7 7 null null null null null 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MamBa2488@@@

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

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

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

打赏作者

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

抵扣说明:

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

余额充值