linked list endless loop

  • Thread starter Thread starter finer
  • Start date Start date
F

finer

i'm new to C#, but i'm trying to write a circular doubly linked list.
theres a bug in my code somewhere (i think in my find() method or
ToGo() method) that causes an endless loop after i insert a node in
the middle of the list.

anyone care to take a look? i'm stumped. i appreciate any help.

source code:
http://www.cse.buffalo.edu/~dhfine/linkedlist.txt


thanks.
-dave
 
i'm new to C#, but i'm trying to write a circular doubly linked list.
theres a bug in my code somewhere (i think in my find() method or
ToGo() method) that causes an endless loop after i insert a node in
the middle of the list.

anyone care to take a look? i'm stumped. i appreciate any help.

source code:http://www.cse.buffalo.edu/~dhfine/linkedlist.txt

thanks.
-dave

Hmm didn't post. You're missing the new back link.

public void Add(int i)
{
//GoTo(length-1);
if(current.Next == null)
{
current.Next = new Node(current, null, i);
current = current.Next;
}
else
{
current.Next.Previous = new Node(current, current.Next,i);
current.Next = current.Next.Previous; //<---new
current = current.Next;
}
length++;
index++;
}
 
Hmm didn't post. You're missing the new back link.

public void Add(int i)
{
//GoTo(length-1);
if(current.Next == null)
{
current.Next = new Node(current, null, i);
current = current.Next;
}
else
{
current.Next.Previous = new Node(current, current.Next,i);
current.Next = current.Next.Previous; //<---new
current = current.Next;
}
length++;
index++;

}

your additional line didnt seem to change anything for me :(
any other ideas?
 
your additional line didnt seem to change anything for me :(
any other ideas?

My bad,

public void Add(int i)
{
//GoTo(length-1);
if (current.Next == null)
{
current.Next = new Node(current, null, i);
current = current.Next;
}
else
{
current.Next = new Node(current, current.Next, i);
current = current.Next;
current.Next.Previous = current; //<--- new
}
length++;
index++;
}

As mentioned you hadn't connected 5.previous to 7 which is what was
causing the problem. Silly typo on my part.
 
My bad,

public void Add(int i)
{
//GoTo(length-1);
if (current.Next == null)
{
current.Next = new Node(current, null, i);
current = current.Next;
}
else
{
current.Next = new Node(current, current.Next, i);
current = current.Next;
current.Next.Previous = current; //<--- new
}
length++;
index++;

}

As mentioned you hadn't connected 5.previous to 7 which is what was
causing the problem. Silly typo on my part.

@developerX: thank you. this seems to have fixed the problem :)


@PFC sadr: ***** you*. im sorry you dont appreciate someone who is
taking an effort to learn something above and beyond programming
basics.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top