算法与数据结构基础之顺序表

算法与数据结构基础之顺序表

顺序表就是按照顺序存储方式存储的线性表
简单来说顺序表就是一个数组

/*
 * 基本数据类型
 */
public class Data {
    String key;
    String name;
    int age;
    public Data(String key, String name, int age) {
        this.key = key;
        this.name = name;
        this.age = age;
    }
    public Data() {
    }
    public String toString() {
        return this.key + " " +this.name + " " + this.age;
    }
}
/*
 * 顺序表及其功能的具体实现
 */
public class SLType {
    private static final int MAXLENGTH = 100;//设置顺序表的最大长度
    Data[] data = new Data[MAXLENGTH];
    int length;

    public SLType() {
    }
    public SLType(Data[] data, int length) {
        this.data = data;
        this.length = length;
    }

    //初始化顺序表
    public void init(){
        this.length = 0;
    }

    //返回顺序表长度
    public int getLength(){
        return this.length;
    }

    //添加元素
    public int add(Data data){
        if(this.length > 100){
            System.out.println("顺序表已满");
            return -1;
        }else{      
            this.data[this.length] = data;
            return this.length++;
        }
    }

    //在指定位置添加元素
    public int insert(Data data,int n){
        if(this.length > 100){
            System.out.println("顺序表已满");
            return -1;
        }else if(n<0||n>this.length){
            System.out.println("插入位置错误");
            return -1;
        }else{
            for(int i = length++;i > n;i--){
                this.data[i] = this.data[i-1];
            }
            this.data[n] = data;
            return n;
        }
    }

    //删除
    public int delete(int n){
        if(n<0 ||n > this.length-1){
            System.out.println("删除位置错误");
            return -1;
        }else{
            for(int i=n;i<this.length;i++){
                this.data[i] = this.data[i+1];
            }
            this.length--;
            return n;
        }
    }

    //根据下标查询
    public Data get(int n){
        if(n < 0|| n > this.length){
            System.out.println("查询下标错误");
            return null;
        }else{
            return this.data[n];
        }
    }

    //出栈
    public Data Pop(){
        if(this.length < 0){
            return null;
        }else{
            return this.data[this.length--];
        }
    }

    //获取全部数组
    public Data[] getDataArr(){
        Data[] Arr = new Data[this.length];
        for(int i=0;i<this.length;i++){
            Arr[i] = this.data[i];
        }
        return Arr;
    }

    //打印全部数组
    public void printArr(){
        for(int i=0;i<this.length;i++){
            System.out.println(this.data[i]);
        }
    }

    //添加数组
    public int addAll(Data[] data){
        if(this.length + data.length > this.MAXLENGTH){
            System.out.println("数组超出");
            return -1;
        }else{
            for(int i = this.length,j = 0;i< this.length + data.length ;i++,j++){
                this.data[i] = data[j];
            }
            this.length += data.length;
            return data.length;
        }
    }
}
/*
 * 客户端
 */
public class Client {
    public static void main(String[] args) {
        Data d1 = new Data("k1","zs",15);
        Data d2 = new Data("k2","zw",15);
        Data d3 = new Data("k3","zq",16);
        Data d4 = new Data("k4","zr",15);

        SLType sl = new SLType();
        sl.init();
        sl.add(d1);
        sl.add(d2);
        sl.add(d3);
        sl.add(d4);
        sl.printArr();
        System.out.println("================");
        sl.delete(3);
        sl.printArr();
        System.out.println("================");
        sl.insert(d1, 0);
        sl.printArr();
        System.out.println("================");
        System.out.println(sl.length);
        System.out.println("================");
        Data d = sl.get(3);
        System.out.println(d);
        System.out.println("================");
        Data[] dArr = sl.getDataArr();
        for (Data data : dArr) {
            System.out.println(data);
        }
        System.out.println("================");
        sl.addAll(dArr);
        System.out.println(sl.length);
        sl.printArr();
    }
}

结果:

k1 zs 15
k2 zw 15
k3 zq 16
k4 zr 15
================
k1 zs 15
k2 zw 15
k3 zq 16
================
k1 zs 15
k1 zs 15
k2 zw 15
k3 zq 16
================
4
================
k3 zq 16
================
k1 zs 15
k1 zs 15
k2 zw 15
k3 zq 16
================
8
k1 zs 15
k1 zs 15
k2 zw 15
k3 zq 16
k1 zs 15
k1 zs 15
k2 zw 15
k3 zq 16
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 撸撸猫 设计师:C马雯娟 返回首页