2016-11-03 9 views
0

すべての最初と問題の説明の前に、あなたは私の問題を理解するために、この単純なクラスが表示されます。StackOverflowExceptionがIntNode

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

program`sの役割が2以上ある場合は、重複を削除するには、次のとおりです。 は今、これは私のプログラムです。例えば、与えられたリストがある場合:

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

出力リストは次のようになります。多分(

Process has been terminated StackOverFlowException

ない:私は私のコードを実行すると

1,4,5,5,6.

が、私はエラーを取得します正確な言葉ですが、C#を知っていればこのエラーを知るはずです...)しかし、なぜこのプログラムは無限ループで実行されているのかわかりません。私も、私がメインで作成したリストのためのそれを踏襲しているが、私は、問題はここにある...なぜ

+0

*正確なエラー*を貼り付けます(含まれている場合はトレースバックを含むことをお勧めします)。あなたとあなたの質問の回答者の両方に問題を引き起こすので、あなたが得ているエラーを推測することを期待するべきではありません。 – Aurora0001

答えて

3

を実現can`t:

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

this + " --> "自動的thisの文字列表現を取得しようとしますこれは、現在の正確な関数であるthis.toString()を呼び出します。これは、StackOverflowExceptionがトリガーされるまで、ToString()がToString()を呼び出してToString()を呼び出すと、同じオブジェクトでToString()が呼び出されることなく発生します。

代わりにノードの値を印刷したいとします。 nullでない場合は、Nextにのみアクセスしてください。

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

ありがとう!出来た! – user6839594

関連する問題