Java 链表 链表反转

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zf3602135/article/details/50684061

链表:单向链表 双向链表 单向循环链表 双向循环链表 链表的反转.


定义了链表的基本使用, 对链表增加了索引, 使用两种方式(递归和循环)对链表进行反转操作.


单向链表:



public class SinglyChain<E> {
    private Object lock = new Object();


    private Node head;// 头节点
    private Node last;// 尾节点
    private int size;


    public int size() {
        return size;
    }


    /**
     * 允许添加的对象重复, 默认添加到末尾;<br/>
     * 不允许添加空对象
     */
    public boolean add(E data) {
        synchronized (lock) {
            if (data != null) {
                Node node = new Node(size, data);
                if (size == 0) {
                    head = node;
                } else {
                    last.next = node;
                }


                last = node;
                size++;
                return true;
            }
            return false;
        }
    }


    /**
     * 允许添加的对象重复, 默认添加到末尾;<br/>
     * 不允许添加空对象
     * 
     * @param index 添加的位置索引
     */
    public boolean add(int index, E data) {
        if (index < 0) {
            index = 0;
        } else if (index > size) {
            index = size;
        }
        synchronized (lock) {
            if (data != null) {
                if (index == size) {
                    return add(data);
                } else {
                    Node newNode = new Node(index, data);
                    Node node = head;
                    Node pre = null;
                    while (node != null) {
                        if (node.index == index) {


                            if (node == head) {
                                newNode.next = head;
                                head = newNode;
                            } else {
                                pre.next = newNode;
                                newNode.next = node;
                            }


                            moveToBack(node);
                            size++;
                            return true;
                        }


                        pre = node;
                        node = node.next;
                    }


                }
            }
            return false;
        }
    }


    /** 如果添加了重复对象,则只删除遍历到的第一个 */
    public boolean remove(E data) {
        synchronized (lock) {
            if (data != null) {
                Node currentNode = head;
                Node preNode = null;
                Node nextNode;


                while (currentNode != null) {
                    nextNode = currentNode.next;
                    if (currentNode.data == data) {
                        if (preNode == null) {
                            // 如果删除的是首节点, 重置首节点
                            head = nextNode;
                        } else {
                            // 将需要删除的对象 currentNode 从链表中剔除
                            preNode.next = nextNode;
                        }


                        // 如果删除的是尾节点, 重置尾节点
                        if (currentNode == last) {
                            last = preNode;
                        }


                        // 将node中的index进行移位操作
                        moveToFront(nextNode);
                        size--;
                        return true;
                    }


                    preNode = currentNode;
                    currentNode = currentNode.next;
                }
            }
            return false;
        }
    }


    /** 根据索引位置删除链表中的对象 */
    public boolean remove(int index) {


        synchronized (lock) {
            Node currentNode = head;
            Node preNode = null;
            Node nextNode;


            while (currentNode != null) {
                nextNode = currentNode.next;
                if (currentNode.index == index) {
                    if (preNode == null) {
                        // 如果删除的是首节点, 重置首节点
                        head = nextNode;
                    } else {
                        // 将需要删除的对象 currentNode 从链表中剔除
                        preNode.next = nextNode;
                    }


                    // 如果删除的是尾节点, 重置尾节点
                    if (currentNode == last) {
                        last = preNode;
                    }


                    // 将node中的index进行移位操作
                    moveToFront(nextNode);
                    size--;
                    return true;
                }


                preNode = currentNode;
                currentNode = currentNode.next;
            }
            return false;
        }
    }


    public E get(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException();
        }


        Node node = head;
        while (node != null) {
            if (node.index == index) {
                return node.data;
            }
        }
        return null;
    }


    public E getFirst() {
        return head.data;
    }


    public E getLast() {
        return last.data;
    }


    /** 将当前节点以后的所有节点索引前移一位 */
    private void moveToFront(Node node) {
        synchronized (lock) {
            while (node != null) {
                node.setIndex(node.getIndex() - 1);
                node = node.next;
            }
        }
    }


    /** 将当前节点以后的所有节点索引后移一位 */
    private void moveToBack(Node node) {
        while (node != null) {
            node.setIndex(node.getIndex() + 1);
            node = node.next;
        }
    }


    @Override
    public String toString() {
        if (head == null && head == last) {
            return "NodeChain {}";
        }


        StringBuilder sb = new StringBuilder();
        // 将所有的子节点递归处理
        Node node = head;
        sb.append("NodeChain {");
        while (node != null) {
            sb.append("\n")
                    .append('[')
                    .append(node.toString())
                    .append(']')
                    .append(',')
                    .append(' ');
            node = node.next;
        }
        sb.append('}');
        return sb.toString();
    }


    /** 翻转链表 */
    public void reverseChain() {
        Node reverse = reverseCycle(head);


        // 首尾置换
        head = last;
        last = reverse;


        last.next = null;


        last.index = size - 1;
    }


    /** 递归反转链表 */
    private Node reverseRecursion(Node node) {
        if (node.next == null) {// 尾节点
            return node;
        }
        Node temp = reverseRecursion(node.next);
        temp.next = node;
        temp.index = size - 1 - temp.index;


        return node;
    }


    /** 循环反转链表 */
    private Node reverseCycle(Node head) {
        if (head == null) {
            return null;
        }


        Node pre = head;
        Node cur = pre.next;
        Node next;


        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            cur.index = size - 1 - cur.index;
            pre = cur;
            cur = next;
        }


        return head;
    }


    class Node {
        int index;
        Node next;
        E data;


        public Node(int index, E data) {
            super();
            this.index = index;
            this.data = data;
        }


        public int getIndex() {
            return index;
        }


        public void setIndex(int index) {
            this.index = index;
        }


        @Override
        public String toString() {
            String str = "Node {index:" + index + ", data : " + data.toString() + '}';
            if (next != null) {
                str += " -> next = " + next.data + ", next.index = " + next.index;
            }
            return str;
        }
    }


}

单向循环链表:



public class SinglyCircleChain<E> {
    private Object lock = new Object();


    private Node head;// 头节点
    private Node last;// 尾节点
    private int size;


    public int size() {
        return size;
    }


    /**
     * 允许添加的对象重复, 默认添加到末尾;<br/>
     * 不允许添加空对象
     */
    public boolean add(E data) {
        synchronized (lock) {
            if (data != null) {
                Node node = new Node(size, data);
                if (size == 0) {
                    head = node;
                } else {
                    last.next = node;
                }


                last = node;
                last.next = head;
                size++;
                return true;
            }
            return false;
        }
    }


    /**
     * 允许添加的对象重复, 默认添加到末尾;<br/>
     * 不允许添加空对象
     * 
     * @param index 添加的位置索引
     */
    public boolean add(int index, E data) {
        synchronized (lock) {
            if (index < 0 || index > size) {
                throw new IndexOutOfBoundsException();
            }


            if (data != null) {
                if (index == size) {
                    return add(data);
                } else {
                    Node newNode = new Node(index, data);
                    Node node = head;
                    Node pre = last;


                    while (node != null) {
                        if (node.index == index) {
                            if (node == head) {// 首节点
                                newNode.next = node;
                                head = newNode;
                                last.next = newNode;
                            } else {
                                pre.next = newNode;
                                newNode.next = node;
                            }


                            moveToBack(node);
                            size++;
                            return true;
                        }


                        pre = node;
                        node = node.next;


                        if (node == head) {
                            break;
                        }
                    }


                }
            }
            return false;
        }
    }


    /** 如果添加了重复对象,则只删除遍历到的第一个 */
    public boolean remove(E data) {
        synchronized (lock) {
            if (data != null && head != null) {
                Node currentNode = head;
                Node preNode = null;
                Node nextNode;


                while (currentNode != null) {
                    nextNode = currentNode.next;
                    if (currentNode.data == data) {
                        if (preNode == null) {
                            // 如果删除的是首节点, 重置首节点
                            head = nextNode;
                        } else {
                            // 将需要删除的对象 currentNode 从链表中剔除
                            preNode.next = nextNode;
                        }


                        // 如果删除的是尾节点, 重置尾节点
                        if (currentNode == last) {
                            last = preNode;
                        }


                        // 将node中的index进行移位操作
                        moveToFront(nextNode);
                        size--;
                        return true;
                    }


                    preNode = currentNode;
                    currentNode = currentNode.next;


                    if (currentNode == head) {
                        break;
                    }
                }
            }
            return false;
        }
    }


    /** 根据索引位置删除链表中的对象 */
    public boolean remove(int index) {


        synchronized (lock) {
            Node currentNode = head;
            Node preNode = null;
            Node nextNode;


            while (currentNode != null) {
                nextNode = currentNode.next;
                if (currentNode.index == index) {
                    if (preNode == null) {
                        // 如果删除的是首节点, 重置首节点
                        head = nextNode;
                    } else {
                        // 将需要删除的对象 currentNode 从链表中剔除
                        preNode.next = nextNode;
                    }


                    // 如果删除的是尾节点, 重置尾节点
                    if (currentNode == last) {
                        last = preNode;
                        last.next = head;
                    }


                    // 将node中的index进行移位操作
                    moveToFront(nextNode);
                    size--;
                    return true;
                }


                preNode = currentNode;
                currentNode = currentNode.next;


                if (currentNode == head) {
                    break;
                }
            }
            return false;
        }
    }


    public E get(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException();
        }


        Node node = head;
        while (node != null) {
            if (node.index == index) {
                return node.data;
            }
        }
        return null;
    }


    /** 将当前节点以后的所有节点索引前移一位 */
    private void moveToFront(Node node) {
        synchronized (lock) {
            while (node != null) {
                node.setIndex(node.getIndex() - 1);
                node = node.next;
                if (node == head) {
                    break;
                }
            }
        }
    }


    /** 将当前节点以后的所有节点索引后移一位 */
    private void moveToBack(Node node) {
        while (node != null) {
            node.setIndex(node.getIndex() + 1);
            node = node.next;
            if (node == head) {
                break;
            }
        }
    }


    /** 翻转链表 */
    public void reverseChain() {
        Node reverse = reverseCycle(head);


        // 首尾置换
        head = last;
        last = reverse;


        last.next = head;


        last.index = size - 1;
    }


    /** 递归反转链表 */
    private Node reverseRecursion(Node node) {
        if (node.next == head) {// 尾节点
            return node;
        }


        Node temp = reverseRecursion(node.next);
        temp.next = node;
        temp.index = size - 1 - temp.index;


        return node;
    }


    /** 循环反转链表 */
    private Node reverseCycle(Node head) {
        if (head == null) {
            return null;
        }


        Node pre = head;
        Node cur = pre.next;
        Node next;


        while (cur != head) {
            next = cur.next;
            cur.next = pre;
            cur.index = size - 1 - cur.index;
            pre = cur;
            cur = next;
        }


        return head;
    }


    @Override
    public String toString() {
        if (head == null && head == last) {
            return "NodeChain {}";
        }


        StringBuilder sb = new StringBuilder();
        // 将所有的子节点递归处理
        Node node = head;
        sb.append("NodeChain {");
        while (node != null) {
            sb.append("\n")
                    .append('[')
                    .append(node.toString())
                    .append(']')
                    .append(',')
                    .append(' ');
            node = node.next;
            if (node == head) {
                break;
            }
        }
        sb.append('}');
        return sb.toString();
    }


    class Node {
        int index;
        Node next;
        E data;


        public Node(int index, E data) {
            super();
            this.index = index;
            this.data = data;
        }


        public int getIndex() {
            return index;
        }


        public void setIndex(int index) {
            this.index = index;
        }


        @Override
        public String toString() {
            String str = "Node {index:" + index + ", data : " + data.toString() + '}';
            str += " -> next = " + next.data + ", next.index = " + next.index;
            return str;
        }
    }


}

双向链表:

public class DoublyChain<E> {
    private Object lock = new Object();


    private Node head;// 头节点
    private Node last;// 尾节点
    private int size;


    public int gsize() {
        return size;
    }


    /**
     * 允许添加的对象重复, 默认添加到末尾;<br/>
     * 不允许添加空对象
     */
    public boolean add(E data) {
        synchronized (lock) {
            if (data != null) {
                Node node = new Node(size, data);
                if (size == 0) {
                    head = node;
                } else {
                    last.next = node;
                    node.previous = last;
                }


                last = node;
                size++;
                return true;
            }
            return false;
        }
    }


    /**
     * 允许添加的对象重复, 默认添加到末尾;<br/>
     * 不允许添加空对象
     * 
     * @param index 添加的位置索引
     */
    public boolean add(int index, E data) {
        if (index < 0) {
            index = 0;
        } else if (index > size) {
            index = size;
        }
        synchronized (lock) {
            if (data != null) {
                if (index == size) {
                    return add(data);
                } else {
                    Node newNode = new Node(index, data);
                    Node nextNode = head;// 相对于新插入位置后一个
                    Node preNode = null;// 相对于新插入位置前一个
                    while (nextNode != null) {
                        if (nextNode.index == index) {
                            if (head == nextNode) {
                                head = newNode;
                            }


                            if (preNode != null) {
                                preNode.next = newNode;
                            }


                            if (nextNode != null) {
                                nextNode.previous = newNode;
                            }


                            newNode.previous = preNode;
                            newNode.next = nextNode;


                            moveToBack(nextNode);
                            size++;
                            return true;
                        }
                        preNode = nextNode;
                        nextNode = nextNode.next;
                    }


                }
            }
            return false;
        }
    }


    /** 如果添加了重复对象,则只删除遍历到的第一个 */
    public boolean remove(E data) {
        synchronized (lock) {
            if (data != null) {
                Node currentNode = head;
                Node preNode = null;
                Node nextNode;


                while (currentNode != null) {
                    nextNode = currentNode.next;
                    if (currentNode.data == data) {
                        if (preNode == null) {
                            // 如果删除的是首节点, 重置首节点
                            head = nextNode;
                            head.previous = null;
                        } else {
                            // 将需要删除的对象 currentNode 从链表中剔除
                            preNode.next = nextNode;
                            if (nextNode != null) {// 非尾节点
                                nextNode.previous = preNode;
                            }
                        }


                        // 如果删除的是尾节点, 重置尾节点
                        if (currentNode == last) {
                            last = preNode;
                            last.next = null;
                        }


                        // 将node中的index进行移位操作
                        moveToFront(nextNode);
                        size--;
                        return true;
                    }


                    preNode = currentNode;
                    currentNode = currentNode.next;
                }
            }
            return false;
        }
    }


    /**
     * 根据索引位置删除链表中的对象<br>
     * 如果索引超出界限将删除失败
     */
    public boolean remove(int index) {
        synchronized (lock) {
            Node currentNode = head;
            Node preNode = null;
            Node nextNode;


            while (currentNode != null) {
                nextNode = currentNode.next;
                if (currentNode.index == index) {
                    if (preNode == null) {
                        // 如果删除的是首节点, 重置首节点
                        head = nextNode;
                        head.previous = null;
                    } else {
                        // 将需要删除的对象 currentNode 从链表中剔除
                        preNode.next = nextNode;
                        if (nextNode != null) {
                            nextNode.previous = preNode;
                        }
                    }


                    // 如果删除的是尾节点, 重置尾节点
                    if (currentNode == last) {
                        last = preNode;
                        last.next = null;
                    }


                    // 将node中的index进行移位操作
                    moveToFront(nextNode);
                    size--;
                    return true;
                }


                preNode = currentNode;
                currentNode = currentNode.next;
            }
            return false;
        }
    }


    public E get(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException();
        }


        Node node = head;
        while (node != null) {
            if (node.index == index) {
                return node.data;
            }
        }
        return null;
    }


    /** 将当前节点以后的所有节点索引前移一位 */
    private void moveToFront(Node node) {
        synchronized (lock) {
            while (node != null) {
                node.setIndex(node.getIndex() - 1);
                node = node.next;
            }
        }
    }


    /** 将当前节点以后的所有节点索引后移一位 */
    private void moveToBack(Node node) {
        while (node != null) {
            node.setIndex(node.getIndex() + 1);
            node = node.next;
        }
    }


    /** 翻转链表 */
    public void reverseChain() {
        Node reverse = reverseCycle(head);


        // 首尾置换
        head = last;


        last = reverse;


        head.previous = null;
        last.next = null;


        last.index = size - 1;
    }


    /** 递归反转链表 */
    private Node reverseRecursion(Node node) {
        if (node.next == null) {// 尾节点
            return node;
        }
        Node temp = reverseRecursion(node.next);
        temp.next = node;
        node.previous = temp;
        temp.index = size - 1 - temp.index;


        return node;
    }


    /** 循环反转链表 */
    private Node reverseCycle(Node head) {
        if (head == null) {
            return null;
        }


        Node pre = head;
        Node cur = pre.next;
        Node next;


        while (cur != null) {
            next = cur.next;
            cur.next = pre;
            pre.previous = cur;


            cur.index = size - 1 - cur.index;
            pre = cur;
            cur = next;
        }


        return head;
    }


    @Override
    public String toString() {
        if (head == null && head == last) {
            return "NodeChain {}";
        }


        StringBuilder sb = new StringBuilder();
        // 将所有的子节点递归处理
        Node node = head;
        sb.append("NodeChain {");
        while (node != null) {
            sb.append('\n')
                    .append('[')
                    .append(node.toString())
                    .append(']')
                    .append(',');
            node = node.next;
        }
        sb.append('}');
        return sb.toString();
    }


    class Node {
        int index;
        E data;


        Node next;
        Node previous;


        public Node(int index, E data) {
            super();
            this.index = index;
            this.data = data;
        }


        public int getIndex() {
            return index;
        }


        public void setIndex(int index) {
            this.index = index;
        }


        @Override
        public String toString() {
            String str = "Node {index:" + index + ", data : " + data.toString() + '}';


            if (next != null) {
                str += ", next = " + next.index;
            } else {
                str += ", next is empty";
            }


            if (previous != null) {
                str += ", previous = " + previous.index;
            } else {
                str += ", previous is empty";
            }
            return str;
        }
    }


}

双向循环链表:



public class DoublyCircleChain<E> {
    private Object lock = new Object();


    private Node head;// 头节点
    private Node last;// 尾节点
    private int size;


    public int gsize() {
        return size;
    }


    /**
     * 允许添加的对象重复, 默认添加到末尾;<br/>
     * 不允许添加空对象
     */
    public boolean add(E data) {
        synchronized (lock) {
            if (data != null) {
                Node node = new Node(size, data);
                if (size == 0) {
                    head = node;
                } else {
                    last.next = node;
                    node.previous = last;
                }


                last = node;


                last.next = head;
                head.previous = last;


                size++;
                return true;
            }
            return false;
        }
    }


    /**
     * 允许添加的对象重复, 默认添加到末尾;<br/>
     * 不允许添加空对象
     * 
     * @param index 添加的位置索引
     */
    public boolean add(int index, E data) {
        if (index < 0) {
            index = 0;
        } else if (index > size) {
            index = size;
        }
        synchronized (lock) {
            if (data != null) {
                if (index == size) {
                    return add(data);
                } else {
                    Node newNode = new Node(index, data);
                    Node nextNode = head;// 相对于新插入位置后一个
                    Node preNode = last;// 相对于新插入位置前一个
                    while (nextNode != null) {
                        if (nextNode.index == index) {
                            if (head == nextNode) {
                                head = newNode;
                            }


                            if (preNode != null) {
                                preNode.next = newNode;
                            }


                            if (nextNode != null) {
                                nextNode.previous = newNode;
                            }


                            newNode.previous = preNode;
                            newNode.next = nextNode;


                            moveToBack(nextNode);
                            size++;
                            return true;
                        }
                        preNode = nextNode;
                        nextNode = nextNode.next;


                        if (nextNode == head) {
                            break;
                        }
                    }


                }
            }
            return false;
        }
    }


    /** 如果添加了重复对象,则只删除遍历到的第一个 */
    public boolean remove(E data) {
        synchronized (lock) {
            Node currentNode = head;
            Node preNode = last;
            Node nextNode;


            int index = 0;
            while (currentNode != null) {
                index++;
                nextNode = currentNode.next;
                if (currentNode.data == data) {
                    if (preNode == last) {
                        // 如果删除的是首节点, 重置首节点
                        head = nextNode;
                        head.previous = last;
                    } else {
                        // 将需要删除的对象 currentNode 从链表中剔除
                        preNode.next = nextNode;
                        nextNode.previous = preNode;
                    }


                    // 如果删除的是尾节点, 重置尾节点
                    if (currentNode == last) {
                        last = preNode;
                        last.next = head;
                    }


                    last.next = head;
                    head.previous = last;


                    if (index != size) {// 如果删除的是最后一个,则不进行索引移动
                        // 将node中的index进行移位操作
                        moveToFront(nextNode);
                    }
                    size--;
                    return true;
                }


                preNode = currentNode;
                currentNode = currentNode.next;


                if (currentNode == head) {
                    break;
                }
            }
            return false;
        }
    }


    /**
     * 根据索引位置删除链表中的对象<br>
     * 如果索引超出界限将删除失败
     */
    public boolean remove(int index) {
        synchronized (lock) {
            Node currentNode = head;
            Node preNode = last;
            Node nextNode;


            while (currentNode != null) {
                nextNode = currentNode.next;
                if (currentNode.index == index) {
                    if (preNode == last) {
                        // 如果删除的是首节点, 重置首节点
                        head = nextNode;
                        head.previous = last;
                    } else {
                        // 将需要删除的对象 currentNode 从链表中剔除
                        preNode.next = nextNode;
                        nextNode.previous = preNode;
                    }


                    // 如果删除的是尾节点, 重置尾节点
                    if (currentNode == last) {
                        last = preNode;
                        last.next = head;
                    }


                    last.next = head;
                    head.previous = last;


                    // 将node中的index进行移位操作
                    size--;
                    if (index != size) {
                        // 如果删除的是最后一个,则不进行索引移动
                        moveToFront(nextNode);
                    }
                    return true;
                }


                preNode = currentNode;
                currentNode = currentNode.next;


                if (currentNode == head) {
                    break;
                }
            }
            return false;
        }
    }


    public E get(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException();
        }


        Node node = head;
        while (node != null) {
            if (node.index == index) {
                return node.data;
            }
        }
        return null;
    }


    /** 将当前节点以后的所有节点索引前移一位 */
    private void moveToFront(Node node) {
        synchronized (lock) {
            while (node != null) {
                node.setIndex(node.getIndex() - 1);
                node = node.next;
                if (node == head) {
                    break;
                }
            }
        }
    }


    /** 将当前节点以后的所有节点索引后移一位 */
    private void moveToBack(Node node) {
        while (node != null) {
            node.setIndex(node.getIndex() + 1);
            node = node.next;
            if (node == head) {
                break;
            }
        }
    }


    /** 翻转链表 */
    public void reverseChain() {
        Node reverse = reverseCycle(head);


        // 首尾置换
        head = last;
        last = reverse;


        last.next = head;
        head.previous = last;


        last.index = size - 1;
    }


    /** 递归反转链表 */
    private Node reverseRecursion(Node node) {
        if (node.next == head) {// 尾节点
            return node;
        }
        Node temp = reverseRecursion(node.next);
        temp.next = node;
        node.previous = temp;
        temp.index = size - 1 - temp.index;


        return node;
    }


    /** 循环反转链表 */
    private Node reverseCycle(Node head) {
        if (head == null) {
            return null;
        }


        Node pre = head;
        Node cur = pre.next;
        Node next;


        while (cur != head) {
            next = cur.next;
            cur.next = pre;
            pre.previous = cur;


            cur.index = size - 1 - cur.index;
            pre = cur;
            cur = next;
        }


        return head;
    }


    @Override
    public String toString() {
        if (head == null && head == last) {
            return "NodeChain {}";
        }


        StringBuilder sb = new StringBuilder();
        // 将所有的子节点递归处理
        Node node = head;
        sb.append("NodeChain {");
        while (node != null) {
            sb.append('\n')
                    .append('[')
                    .append(node.toString())
                    .append(']')
                    .append(',');
            node = node.next;


            if (node == head) {
                break;
            }
        }
        sb.append('}');
        return sb.toString();
    }


    class Node {
        int index;
        E data;


        Node next;
        Node previous;


        public Node(int index, E data) {
            super();
            this.index = index;
            this.data = data;
        }


        public int getIndex() {
            return index;
        }


        public void setIndex(int index) {
            this.index = index;
        }


        @Override
        public String toString() {
            String str = "Node {index:" + index + ", data : " + data.toString() + '}';


            if (next != null) {
                str += ", next = " + next.index;
            } else {
                str += ", next is empty";
            }


            if (previous != null) {
                str += ", previous = " + previous.index;
            } else {
                str += ", previous is empty";
            }
            return str;
        }
    }


}

猜你喜欢

转载自blog.csdn.net/zf3602135/article/details/50684061