Java实现静态链表

2019-03-24  本文已影响0人  糖醋排骨盐酥鸡

今天复习到静态链表。自己简单实现了静态链表的基本操作,记录一下


/**
 * @DESCRIPTION TODO
 * @DATE 2019年3月23日 下午9:08:11
 * @AUTHOR tmooming
 */
public class SNode<T> {
    public T data;
    private int cursor;

    public SNode() {

    }

    public SNode(T data, int cursor) {
        this.data = data;
        this.cursor = cursor;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public int getCursor() {
        return cursor;
    }

    public void setCursor(int cursor) {
        this.cursor = cursor;
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(getData());
        sb.append(" ");
        sb.append(getCursor() + " ");
        return sb.toString();

    }
}

/**
 * @DESCRIPTION TODO
 * @DATE 2019年3月23日 下午9:08:23
 * @AUTHOR tmooming
 */
public class SList<T> {
    SNode<T>[] node;
    private static final int MAX_SIZE = 15;
    private int length;

    public SList() {
        node = new SNode[MAX_SIZE];
        for (int i = 0; i < MAX_SIZE - 1; i++) {
            node[i] = new SNode<T>(null, i + 1);
        }
        node[MAX_SIZE - 1] = new SNode<>(null, 0);
        this.length = 0;
    }

    // 实际数组长度
    public int Length() {
        return length;
    }

    // 判断使用数组是否为空
    public boolean isEmpty() {
        return length == 0;
    }

    // 判断备用链表是否为空
    public boolean isFull() {
        return length == MAX_SIZE;
    }

    // 插入一个节点
    public boolean addTo(T data, int index) {
        if (isFull() || index > MAX_SIZE || index < -1 || data == null)
            return false;
        if (index == 0) {
            insert(data);
            return true;
        }
        if (index > Length()) {
            index = Length();
        }
        // 获取第一个使用节点的下标
        int firstUse = node[MAX_SIZE - 1].getCursor();
        // 获取备用数组第一个节点的下标
        int firstNull = node[0].getCursor();
        for (int i = 1; i < index; i++) {
            firstUse = node[firstUse].getCursor();
        }
        // 获取目标节点的后续节点
        int nextUse = node[firstUse].getCursor();
        int nextNull = node[firstNull].getCursor();
        node[0].setCursor(nextNull);
        node[firstUse].setCursor(firstNull);
        node[firstNull].setCursor(nextUse);
        node[firstNull].setData(data);
        length++;
        return true;

    }

    public void insert(T data) {
        int t = node[MAX_SIZE - 1].getCursor();
        int firstNull = node[0].getCursor();
        node[MAX_SIZE - 1].setCursor(firstNull);
        node[0].setCursor(node[firstNull].getCursor());
        node[firstNull].setCursor(t);
        node[firstNull].setData(data);
        length++;
    }

    public void print() {
        int first = node[MAX_SIZE - 1].getCursor();
        System.out.println("链表:");
        for (int i = first; i != 0;) {
            System.out.print(node[i].getData() + " ");
            i = node[i].getCursor();
        }

    }

    // 删除指定节点
    public boolean delete(T data) {
        if (isEmpty()) {
            return false;
        }
        int temp = MAX_SIZE - 1;
        while (temp != 0) {
            if (node[node[temp].getCursor()].getData() == data) {
                int p = node[temp].getCursor();
                node[temp].setCursor(node[p].getCursor());
                node[p].setCursor(node[0].getCursor());
                node[0].setCursor(p);
                node[p].setData(null);
                length--;
                return true;
            }
            temp = node[temp].getCursor();
        }
        return false;
    }

    // 删除所有节点
    public boolean deleteAll() {
        if (isEmpty()) {
            return true;
        }
        for (int i = 0; i < MAX_SIZE - 1; i++) {
            node[i].setCursor(i + 1);
            node[i].setData(null);
        }
        node[MAX_SIZE - 1].setCursor(0);
        node[MAX_SIZE - 1].setData(null);
        length = 0;
        return true;
    }

    public void printAll() {

        System.out.println("链表:");
        for (int i = 0; i < MAX_SIZE; i++) {
            System.out.print("[" + i + " " + node[i].getData() + " " + node[i].getCursor() + "]");
            if (i % 5 == 0 && i != 0) {
                System.out.println();
            }

        }

    }

    public static void main(String[] args) {
        SList<String> list = new SList<String>();
        list.insert("赵");
        list.insert("钱");
        list.insert("李");
        // list.printAll();
        // list.addTo("孙", 5);
        list.deleteAll();
        System.out.println(list.Length());
        list.printAll();

    }
}

上一篇 下一篇

猜你喜欢

热点阅读