【问题标题】:StackOverFlowException IntNodeStackOverFlowException IntNode
【发布时间】:2016-11-03 18:07:31
【问题描述】:

首先,在问题的解释之前,你应该看到这个简单的类来理解我的问题:

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;
    }
}

好的,我希望你能理解这门课。 现在,这是我的程序:

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);
}

如果有 2 个或更多,程序的作用是删除重复项。例如,如果给定的列表是:

1,3,3,3,4,5,5,6,9,9,9,9。

输出列表应该是:

1,4,5,5,6.

当我执行我的代码时,我得到一个错误:

进程已终止 StackOverFlowException

(也许不是确切的词,但如果你知道 C#,你应该知道这个错误......)但我无法找到程序运行通过无限循环的原因。我什至关注了我在 Main 中创建的列表,但我不明白为什么......

【问题讨论】:

  • 粘贴您得到的确切错误(最好包括回溯,如果包含)。您不应该期望我们猜测您遇到了什么错误,因为这会给您和问题的回答者带来麻烦。

标签: c# stack-overflow


【解决方案1】:

问题出在这里:

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

this + " --> " 将自动尝试获取this 的字符串表示形式,这将调用this.toString(),这正是我们当前所在的函数。这种情况没有结束(ToString() 调用 ToString() 调用 ToString( ) 在同一个对象上) 直到触发 StackOverflowException。

您想打印节点的值。还要确保仅在不是null 时访问Next

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

【讨论】:

    猜你喜欢
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    • 2016-12-16
    • 2011-04-27
    • 2021-07-24
    相关资源
    最近更新 更多