1. 程式人生 > 實用技巧 >順序表

順序表

package SequenceList;

import java.util.Objects;
import java.util.Scanner;

/**
 * Description 順序表
 * Author cloudr
 * Date 2020/7/25 13:24
 * Version 1.0
 **/
class Data {
    String id;
    String name;
    int age;

    public Data(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Data{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Data data = (Data) o;
        return age == data.age &&
                Objects.equals(id, data.id) &&
                Objects.equals(name, data.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age);
    }
}

class SList {
    final int maxLen = 10;
    Data[] list = new Data[maxLen];
    int listLen;

    //初始化
    void listInit(SList sList, int listLen) {
        this.listLen = listLen;
        Scanner input = new Scanner(System.in);
        int i = 0;
        System.out.println("請輸入" + listLen + "個結點資訊:(id name age)");
        do {
            Data data = new Data(input.next(), input.next(), input.nextInt());
            list[i] = data;
            i++;
        } while (i < listLen);
        input.close();
    }

    //返回順序表長度
    int SLLength(SList sList) {
        return sList.listLen;
    }

    //插入資料
    public int sListInsert(int n, Data data) {
        if (listLen >= maxLen) {
            System.out.println("順序表已滿");
            return 0;
        }
        if (n < 0 || n >= listLen) {
            System.out.println("插入位置非法");
            return 0;
        }
        for (int i = listLen; i >= n; i--) {
            list[i + 1] = list[i];
        }
        list[n] = data;
        listLen++;
        return 1;
    }


    //刪除資料
    void SListDelete(Data data) {
        int k = 0;
        for (int i = 0; i < listLen; i++) {
            if (list[i].equals(data)) {
                k = 1;
                for (int j = i; j < listLen; j++)
                    list[i] = list[j + 1];
                listLen--;
            }
        }
        if (k == 0)
            System.out.println("未找到該結點");
    }

    //返回節點的的序號
    int sListFind(Data data) {
        int i = 0;
        while (i < listLen) {
            if (list[i].equals(data))
                return i;
            i++;
        }
        return -1;
    }

    //顯示所有元素
    void sListShow() {
        for (int i = 0; i < listLen; i++) {
            System.out.println(list[i]);
        }
    }

}

public class SequenceList {
    public static void main(String[] args) {
        SList sList = new SList();
        //初始化
        sList.listInit(sList, 3);
        //插入結點
        sList.sListInsert(1, new Data("0", "0", 123));
        sList.sListShow();
        //尋找結點序號
        System.out.println(sList.sListFind(new Data("1", "1", 1)));
        //刪除結點
        sList.SListDelete(new Data("2", "2", 2));
        sList.sListShow();
    }
}