单链表的实现-Java

余生颓废 提交于 2020-02-17 09:01:39
  • 建立结点类
/**
 * @author cnkeysky
 * @date 2020-02-15 22:31
 */
public class Node<T> {

    protected T data;
    protected Node<T> next;

    public Node() {

    }

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

    @Override
    public String toString() {
        return this.data + "";
    }
}

  • 单链表
import java.util.Stack;

/**
 * @author cnkeysky
 * @date 2020-02-15 22:33
 */
public class SingleLinkedList<T> {

    protected Node<T> header;
    protected Node<T> rear;
    private int count;

    public SingleLinkedList() {
        header = new Node<>();
        rear = header;
    }

    /**
     * 拷贝构造
     *
     * @param list SingleLinkedList
     */
    public SingleLinkedList(SingleLinkedList<T> list) {
        this();
        Node<T> p = list.header;
        count = list.size();
        while (p.next != null) {
            p = p.next;
            rear.next = new Node<>(p.data);
            rear = rear.next;
        }
    }

    /**
     * 复制一个单链表
     *
     * @param header 头指针
     * @param count  单链表长度
     * @return SingleLinkedList<T>
     */
    private SingleLinkedList<T> copySingleLinkedList(Node<T> header, int count) {
        SingleLinkedList<T> linkedList = new SingleLinkedList<>();
        linkedList.count = count;
        Node<T> p = header;
        while (p.next != null) {
            p = p.next;
            linkedList.rear.next = new Node<>(p.data);
            linkedList.rear = linkedList.rear.next;
        }
        return linkedList;
    }

    /**
     * 连接两个单链表并返回一个新的单链表
     *
     * @param list SingleLinkedList<T>
     * @return SingleLinkedList<T>
     */
    public SingleLinkedList<T> concat(SingleLinkedList<T> list) {
        SingleLinkedList<T> linkedList = copySingleLinkedList(header, count);
        Node<T> p = list.header;
        while (p.next != null) {
            p = p.next;
            linkedList.rear.next = new Node<>(p.data);
            linkedList.rear = linkedList.rear.next;
        }
        return linkedList;
    }

    /**
     * 指定位置插入元素
     * index <= 0 时在位置0进行插入,index >= size() 时在尾部插入
     *
     * @param index int
     * @param val   T
     */
    public void insert(int index, T val) {
        if (index <= 0) {
            Node<T> node = new Node<>(val);
            node.next = header.next;
            header.next = node;
            ++count;
        } else if (index >= count) {
            add(val);
        } else {
            Node<T> p = header;
            for (int i = 0; i < index; ++i) {
                p = p.next;
            }
            Node<T> node = new Node<>(val);
            node.next = p.next;
            p.next = node;
            ++count;
        }
    }

    /**
     * 获取指定位置的元素
     *
     * @param index int
     * @return T
     */
    public T get(int index) {
        if (index < 0 || index >= count) {
            throw new RuntimeException("out of range");
        }
        if (empty()) {
            throw new RuntimeException("链表为空");
        }
        Node<T> p = header.next;
        for (int i = 0; i < index; ++i) {
            p = p.next;
        }
        return p.data;
    }

    /**
     * 删除链表中最后一个元素
     *
     * @return boolean
     */
    public boolean remove() {
        boolean flag = false;
        if (!empty()) {
            flag = true;
            Node<T> p = header;
            for (int i = 0; i < count - 1; ++i) {
                p = p.next;
            }
            rear = p;
            rear.next = null;
            --count;
        }
        return flag;
    }

    /**
     * 翻转单链表
     */
    public void reverseList() {
        if (header.next == null || header.next.next == null) {
            return;
        }
        Node<T> auxHeader = new Node<>();
        Node<T> auxNode;
        Node<T> auxRear = header.next;
        Node<T> p = header.next;
        while (p != null) {
            auxNode = p.next;
            p.next = auxHeader.next;
            auxHeader.next = p;
            p = auxNode;
        }
        header = auxHeader;
        rear = auxRear;
        rear.next = null;
    }

    /**
     * 逆序输出单链表
     */
    public void reversePrint() {
        if (!empty()) {
            Stack<T> stack = new Stack<>();
            Node<T> p = header.next;
            while (p != null) {
                stack.push(p.data);
                p = p.next;
            }
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < count; ++i) {
                builder.append(stack.pop());
                if (i < count - 1) {
                    builder.append(",");
                }
            }
            System.out.println(builder.toString());
        }
    }

    /**
     * 删除指定位置的元素, 从0开始
     *
     * @param index int
     * @return T
     */
    public T remove(int index) {
        if (empty()) {
            throw new RuntimeException("空链表");
        }
        if (index < 0 || index >= count) {
            throw new RuntimeException("out of range");
        }
        Node<T> p = header;
        for (int i = 0; i < index; ++i) {
            p = p.next;
        }
        T val = p.next.data;
        p.next = p.next.next;
        --count;
        return val;
    }

    /**
     * 判断是否为空链表
     *
     * @return boolean
     */
    public boolean empty() {
        return header.next == null;
    }

    /**
     * 添加元素
     *
     * @param val T
     */
    public void add(T val) {
        rear.next = new Node<>(val);
        rear = rear.next;
        ++count;
    }

    /**
     * 链表的个数
     *
     * @return int
     */
    public int size() {
        return count;
    }

    @Override
    public String toString() {
        Node<T> p = header;
        StringBuilder builder = new StringBuilder();
        while (p.next != null) {
            p = p.next;
            builder.append(p.data);
            if (p.next != null) {
                builder.append(",");
            }
        }
        return builder.toString();
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!