全站最帅😎
发布于 2020-05-11 / 1225 阅读
0
0

数据结构:单链表简单实现

通过自己实现的LinkedList类实现单链表的增、删、改、查。

/**
 * 单链表
 * @author lichen
 * @version 1.0.0
 * @date 2020-05-11 15:15
 */
public class LinkedList<T> {

    Node node;
    int size;

    /**
     * 在头部添加节点
     * @param data 数据
     */
    protected void addHead(T data) {
        Node cur = node;
        node = new Node(data, cur);
        size++;
    }

    /**
     * 在尾部添加节点
     * @param data 数据
     */
    protected void addTail(T data) {
        for (int i = 0; i < size; i++) {
            node = node.next;
        }
        node.next = new Node(data, null);
        size++;
    }

    /**
     * 指定下标插入data
     * @param index 下标
     * @param data 数据
     */
    protected void add(int index, T data) {
        checkPositionIndex(index);
        Node cur = node;
        Node head = null;
        for(int i = 0; i < index; i++) {
            head = cur;
            cur = cur.next;
        }
        Node insert = new Node(data, cur);
        if (head == null) {
            addHead(data);
        } else {
            head.next = insert;
        }
        size++;
    }

    /**
     * 删除头节点
     * @return T
     */
    protected T removeHead() {
        Node head = node;
        Node next = node.next;
        node = null; //GC
        node = next;
        size--;
        return head.data;
    }

    /**
     * 删除尾节点
     * @return T
     */
    protected T removeTail() {
        Node cur = node;
        Node pre = node;
        while (cur.next != null) {
            pre = cur;
            cur = cur.next;
        }
        pre.next = null;
        size--;
        return cur.data;
    }

    /**
     * 删除index节点
     * @param index 下标
     * @return T
     */
    protected T remove(int index) {
        Node cur = node;
        Node pre = null;
        for (int i = 0; i < index; i++) {
            pre = cur;
            cur = cur.next;
        }
        if (pre == null) {
            return removeHead();
        } else {
            pre.next = cur.next;
            T data = cur.data;
            cur = null;
            return data;
        }
    }

    /**
     *
     * @param index
     * @param data
     */
    protected void set(int index, T data) {
        checkPositionIndex(index);
        Node cur = node;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        cur.data = data;
    }


    /**
     * 获得头部节点
     */
    protected T getHead() {
        if (node != null) {
            return node.data;
        }
        return null;
    }

    /**
     * 获得尾部的节点
     */
    protected T getTail() {
        Node cur = node;
        while (cur.next != null) {
            cur = cur.next;
        }
        return cur.data;
    }

    /**
     * 获得指定下标的节点
     * @param index 下标
     * @return T
     */
    protected T get(int index) {
        checkPositionIndex(index);
        Node cur = node;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }
        return cur.data;
    }

    protected void checkPositionIndex(int index) {
        if(!(index >= 0 && index <= size - 1)) {
            throw new IndexOutOfBoundsException("index: " + index + ", size: " + size);
        }
    }

    @Override
    public String toString() {
        Node cur = node;
        for (int i = 0; i < size; i++) {
            System.out.print(cur.data + " ");
            cur = cur.next;
        }
        System.out.println();
        return super.toString();
    }

    class Node {

        T data;
        Node next;

        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "data=" + data +
                    ", next=" + next +
                    '}';
        }
    }

    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.addHead(3);
        linkedList.addHead(9);
        linkedList.addHead(7);
        linkedList.addHead(6);
        System.out.println(linkedList.getHead());
        System.out.println(linkedList.getTail());
        linkedList.toString();
        System.out.println(linkedList.get(1));
        linkedList.add(3, 5);
        linkedList.toString();
        System.out.println(linkedList.removeHead());
        linkedList.toString();
        System.out.println(linkedList.removeTail());
        linkedList.toString();
        System.out.println(linkedList.remove(0));
        linkedList.toString();
        linkedList.set(0, 56);
        linkedList.toString();
    }

}


评论