使用Java模拟线性表顺序存储结构

在数据结构中,线性表分两种结构
顺序结构和链表结构
下面使用Java模拟一下顺序结构
主要难点在顺序结构的插入和删除

import java.util.ArrayList;

//线性表顺序存储结构
public class LinearTable {
    ArrayList<Node> list;
    // 分配长度
    int maxLength = 0;
    // 实际长度
    int actualLength = 0;

    // 初始化
    public void initList(int i) {
        list = new ArrayList<Node>(100);
        maxLength = i;
    }

    // 判空
    public boolean isEmpty() {
        if (list.size() == 0) {
            return true;
        } else {
            return false;
        }
    }

    // 清空
    public void clear() {
        for (int i = 0; i < actualLength; i++) {
            list.get(i).item = "";
        }
        actualLength = 0;
    }

    // 定位指定元素 下标从0开始
    public int locate(Node node) {
        for (int i = 0; i < actualLength; i++) {
            if (node.item.equals(list.get(i).item)) {
                System.out.println("find!");
                return i;
            }
        }
        System.out.println("not find!");
        return -1;
    }

    // 返回实际长度
    public int getLenth() {
        return actualLength;
    }

    // 将元素插入到指定位置 下标从0开始
    public boolean insert(Node node, int index) {
        if (actualLength == maxLength) {
            return false;
        }
        if (index < 0 || index > actualLength) {
            return false;
        }
        for (int i = 0; i < (actualLength - index + 1); i++) {
            if (actualLength - i == actualLength) {
                // 最后的一个元素需要加进去 否则越界
                // list.get(actualLength);
                list.add(list.get(actualLength - 1));
            } else {
                // 元素后移 前一个元素值赋给后一个元素
                this.putNode(actualLength - i + 1, list.get(actualLength - i));
            }
        }
        putNode(index, node);
        actualLength++;
        return true;
    }

    public boolean remove(int index) {
        if (index < 0 || index > actualLength - 1) {
            return false;
        }
        // 直接后面覆盖前面即可 从前向后
        for (int i = 0; i < (actualLength - index - 1); i++) {
            this.putNode(index + i, list.get(index + i + 1));
        }
        actualLength--;
        return true;
    }

    public void addNode(Node node) {
        list.add(actualLength, node);
        actualLength++;
    }

    // 更换指定位置元素
    public void putNode(int index, Node node) {
        if (index > actualLength) {
            System.out.println("index error");
            return;
        }
        list.set(index, node);
    }

    public void show() {
        for (int i = 0; i < actualLength; i++) {
            System.out.println(list.get(i).item);
        }
    }

    public static void main(String[] args) {
        LinearTable table = new LinearTable();
        table.initList(10);
        System.out.println("is Empty? " + table.isEmpty());
        table.addNode(new Node("4"));
        table.addNode(new Node("3"));
        System.out.println("is Empty? " + table.isEmpty());
        table.addNode(new Node("2"));
        table.addNode(new Node("1"));
        table.addNode(new Node("0"));
        table.insert(new Node("1]ss"), 2);
        table.show();
        table.remove(0);
        table.remove(0);
        table.remove(0);
        System.out.println("after remove");
        System.out.println("locate" + table.locate(new Node("1")));
        System.out.println("lenght==" + table.getLenth());
        table.show();

        System.out.println("clear");
        table.clear();
        table.show();
        System.out.println("lenght==" + table.getLenth());
    }

}

猜你喜欢

转载自blog.csdn.net/u011109881/article/details/80035217
今日推荐