Search code examples
c#stack-overflow

StackOverFlowException IntNode


first of all and before the Explanation of the issue, You should see this simple class to understand my issue:

class IntNode
{
    public int value { get; set; }
    public IntNode next { get; set; }

    public IntNode(int value)
    {
        this.value = value;
        this.next = null;
    }

    public IntNode(int value, IntNode next)
    {
        this.value = value;
        this.next = next;
    }

    public bool HasNext ()
    {
        return (this.next != null);
    }

    public override string ToString()
    {
        return this + " --> " + this.next;
    }
}

Ok, so I hope you understood the class. Now, this is my program:

static IntNode RemoveDuplicate (IntNode head)
{
    int num = head.value;//the number for repeating check
    IntNode pos = head.next;//current position - start from the second
    IntNode prev = head;//node before the current position
    IntNode bprev = head;//ndoe before the preverious of the current postition
    IntNode bbprev = head;// node before the preverious of the preverious of the current position

    int counter = 1;//for repeating count

    while (pos != null)//as long as there is what to check
    {

        if (pos.value == num) counter++;//if the current value is the number count + 1
        else//if there is another number
        {
            counter = 1;//set counter for 1 (the number already counts)
            num = pos.value;//set the new num
        }

        if (counter == 3)//if count has reached 3
        {
            if (bbprev != head)//if the bbprev is not the head
                bbprev.next = pos.next;//set its next to the current position next node
            else//if the bbprev is the head
                head = pos.next;//delete the third first nodes
        }
        else if (counter > 3) prev.next = pos.next;//if count is over 3, delete pos node

        bbprev = bprev;
        bprev = prev;
        prev = pos;
        pos = pos.next;
    }
    return head;
}

static void Main(string[] args)
{
    IntNode p5 = new IntNode (13);
    IntNode p4 = new IntNode (13, p5);
    IntNode p3 = new IntNode (13, p4);
    IntNode p2 = new IntNode (2, p3);
    IntNode p1 = new IntNode (2, p2);

    IntNode head = RemoveDuplicate(p1);
    Console.WriteLine(head);
}

The program`s role is to delete duplicates if there are 2 or more. For example, if the given list is:

1,3,3,3,4,5,5,6,9,9,9,9.

The output list should be:

1,4,5,5,6.

When I execute my code I get an error:

Process has been terminated StackOverFlowException

(maybe not the exact words, but if you know C# you should know this error...) But I cant find why the program runs trough an endless loop. I have even followed it for the list I created in the Main but I can`t realize why...


Solution

  • The problem lies here:

    public override string ToString()
    {
        return this + " --> " + this.next;
    }
    

    this + " --> " will automatically try to get a string representation of this, which will call this.toString() which is the exact function we are currently in. This happens without end (ToString() calling ToString() calling ToString() on the same object) until the StackOverflowException is triggered.

    You want to print the value of the node instead. Also make sure to only access Next when it is not null.

    public override string ToString()
    {
        if(this.HasNext())
        {
            return this.value + " --> " + this.next;
        }
        else
        { 
            return this.value.ToString();
        }
    }