Permalink
Browse files

Fix bug #311: On LinkedList.Clear, detach each node instead of droppi…

…ng them en masse.
  • Loading branch information...
1 parent 57bfe64 commit e787a12ab23487ace39af0744c700fb0fd6e5716 Todd Foster committed Aug 17, 2011
@@ -182,9 +182,8 @@ public void AddLast (LinkedListNode <T> node)
public void Clear ()
{
- count = 0;
- first = null;
- version++;
+ while (first != null)
+ RemoveLast();
}
public bool Contains (T value)
@@ -84,8 +84,13 @@ public void NonCircularNodeTest ()
[Test]
public void ClearTest ()
{
+ LinkedListNode <int> node = intlist.First;
intlist.Clear ();
+
Assert.AreEqual (0, intlist.Count);
+ Assert.AreEqual (2, node.Value);
+ Assert.IsNull (node.Next);
+ Assert.IsNull (node.Previous);
}
[Test]

4 comments on commit e787a12

Any particular reason you used RemoveLast() instead of RemoveFirst()?

It seems that you should be using Remove() instead of either RemoveFirst() or RemoveLast(), since you've already checked that first != null.

Looks like the most performant solution would be:

while (first != null)
Remove (first.back);

I'm guessing you chose RemoveLast() due to RemoveFirst() incurring an extra assignment per loop than RemoveLast().

Correct: I used RemoveLast to possibly avoid an extra assignment.

I chose to use RemoveLast() instead of Remove(first.back) because it was there (DRY). Let it take care of any housekeeping or optimization, including any future code changes.

Please sign in to comment.