How to do a cyclic doubly link list add method in java

后端 未结 3 588
孤城傲影
孤城傲影 2021-01-28 16:24

I am implementing the add(E) method in the cyclic DoublyLinkedList class as well as the Node inner class.

Node should be implemented as a private inner class.

D

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-28 17:01

    Code fixed with minimal change (just the else case in add):

    class DoublyLinkedList
    {
        private Node first;
        private int size;
    
        public void add(E value)
        {
            if(first == null)
            {
                first = new Node(value, null, null);
                first.next = first;
                first.prev = first;
            }
            else
            {
                first.prev.next = new Node(value, first, first.prev);
                first.prev = first.prev.next;
            }
            size++;
        }
    
        private class Node
        {
            private E data;
            private Node next;
            private Node prev;
    
            public Node(E data, Node next, Node prev)
            {
                this.data = data;
                this.next = next;
                this.prev = prev;
            }
        }
    }
    

提交回复
热议问题