первой очереди и до Разъяснения вопроса, Вы должны увидеть это простой класс, чтобы понять мой вопрос: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.
Выходной список должен быть:
1,4,5,5,6.
Когда я исполню мой код, я получаю сообщение об ошибке:
Process has been terminated StackOverFlowException
(может быть, не точные слова, но если вы знаете C#, вы должны знать эту ошибку ...) Но я не могу найти, почему программа работает через бесконечный цикл. Я даже последовал за ним в списке, я создал в главном, но я не могу понять, почему ...
Вставить * точную ошибку * вы получаете (предпочтительно включая трассировку, если она включена). Вы не должны ожидать, что мы угадаем, какую ошибку вы получаете, потому что это вызовет проблемы как для вас, так и для ответчиков на вопрос. – Aurora0001