简体   繁体   中英

Printing Doubly Linked List in Java is throwing Exception(Stack Overflow Exception)

I have a doubly linked list implementation as below. But I am getting a stack overflow exception when I am trying to print the list. I am not sure but it looks something related to overriding toString() method. Could someone please explain what is really going underneath, that is causing this issue.

        // Main function class
        public class Main {
            public static void main(String[] args) {
                DoublyLinkedList list = new DoublyLinkedList();
                list.insertAtStart(2);
                list.insertAtStart(3);
                list.insertAtStart(4);
                list.insertAtStart(5);
                list.insertAtStart(6);
                list.insertAtStart(7);
                list.print();
            }
        }
        // Doubly node class
        public class DoublyLinkedListNode {
            private int data;
            private DoublyLinkedListNode next;
            private DoublyLinkedListNode prev;
            public DoublyLinkedListNode(int data)
            {
                this.data = data;
            }

            public int getData() {
                return data;
            }

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

            public DoublyLinkedListNode getNext() {
                return next;
            }

            public void setNext(DoublyLinkedListNode next) {
                this.next = next;
            }

            public DoublyLinkedListNode getPrev() {
                return prev;
            }

            public void setPrev(DoublyLinkedListNode prev) {
                this.prev = prev;
            }

            @Override
            public String toString() {
                return "DoublyLinkedListNode{" +
                        "data=" + data +
                        ", next=" + next +
                        ", prev=" + prev +
                        '}';
            }
        }
        // Here I have created function for inserting and printing the elemnts in doubly linked list
        public class DoublyLinkedList {
            public DoublyLinkedListNode head;
            public void insertAtStart(int data)
            {
                DoublyLinkedListNode newNode = new DoublyLinkedListNode(data);
                if(head == null)
                {
                    head = newNode;
                }
                else
                {
                    newNode.setNext(head);
                    head.setPrev(newNode);
                    head = newNode;
                }
            }
           @Override
          public String toString() {
               return "DoublyLinkedList{" +
                       "head=" + head +
                       '}';
           }
            public void print()
            {
                DoublyLinkedListNode currentNode = head;
                while(currentNode != null)
                {
                    System.out.print(currentNode);
                    System.out.print("<=>");
                    currentNode = currentNode.getNext();
                }
                System.out.println("null");
            }
        }

This code leads to infinite recursion....

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

Step through it.

  • When converting the head node to string it wants to convert the next node to string.

  • When converting the second node to string, it wants to convert the previous node (the head node again) to string.

Infinite Recursion leads to infinitely nested method calls growing the stack for each call until you run out of stack and get a Stack Overflow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM