简体   繁体   中英

Why does stack overflow occur when adding second elements to a double-linked list?

I created a double-linked list and it can run without any error.But it will occur java.lang.StackOverflowError at adding second element when i use debug to examine this program.If i don't override toString(),the program will be normal.But i want to know why don't override toString()? package com.study.testcollection.com.study.testlinkedlist;

public class Node {
    private Node per;
    private Object obj;
    private Node next;
    public Node getPer() {
        return per;
    }
    public Object getObj() {
        return obj;
    }
    public Node getNext() {
        return next;
    }
    public void setPer(Node per) {
        this.per = per;
    }
    public void setObj(Object obj) {
        this.obj = obj;
    }
    public void setNext(Node next) {
        this.next = next;
    }
    @Override
   //if don't write this function,the program will be normal.Why?
    public String toString() {
        return "Node{" +
                "per=" + per +
                ", obj=" + obj +
                ", next=" + next +
                '}';
    }
}
package com.study.testcollection.com.study.testlinkedlist;
public class Mylinkedlist {
    Node first = null;
    Node last = null;
    public void add(Object e){
        if (first == null){
            Node n = new Node();
            n.setObj(e);
            n.setPer(null);
            n.setNext(null);
            first = n;
            last = n;
        }
        else{
            Node n = new Node();
            n.setObj(e);
            last.setNext(n);
            n.setPer(last);
            n.setNext(null);
            last = n;
        }
    }
    public static void main(String[] args) {
        Mylinkedlist a = new Mylinkedlist();
        a.add("hello");
        a.add("Bob");//occur error when it is executed
    }
}

Your "next" field is pointing to a Node and thus Node.toString() is called infinitely resulting in stackoverflow. If you need to use toString() method, you can modify it as follows:

public String toString() {
        String n = next != null ? next.obj.toString():"null";
        String p = per != null ? per.obj.toString():"null";
        return "Node{" +
                "per=" + p +
                ", obj=" + obj +
                ", next=" + n +
                '}';
    }

在此处输入图像描述

This is how it looks. When you do:

System.out.println(a.first.toString());

And when toString is defined as:

public String toString() {
    return "Node{" +
            "per=" + per +
            ", obj=" + obj +
            ", next=" + next +
            '}';
}
  • You attempt in printing the next which is n
  • n attempts in printing the previous per
  • Previous per again attempts in printing next
  • next makes a call to previous per and so on...

Resulting in stackoverflow as the call never ends. You are again and again iterating in the forward arrow and previous arrow as shown in image above.

To fix it, you can remove toString from Node and replace with:

public String forward() {
    return "Node{" +
            ", obj=" + obj +
            ", next=" + next +
            '}';
}

public String backward() {
    return "Node{" +
            ", obj=" + obj +
            ", prev=" + per +
            '}';
}

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