Created
March 1, 2013 23:57
-
-
Save amcgowanca/5068900 to your computer and use it in GitHub Desktop.
A sloppy LinkedList<TVal>.Remove(ILinkedNode<int, TVal> node) method.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public bool Remove(ILinkedNode<int, TVal> node) | |
{ | |
if (this.Count.Equals(0)) | |
{ | |
return false; | |
} | |
bool removed = false; | |
if (this.First.Equals(node)) | |
{ | |
this.First = this.First.Next; | |
} | |
else if (this.Last.Equals(node)) | |
{ | |
this.Last = this[this.Count - 2]; | |
this.Last.Next = null; | |
} | |
else | |
{ | |
ILinkedNode<int, TVal> previous = null; | |
ILinkedNode<int, TVal> current = this.First; // skip the first, its not needed, already tested | |
while (null != current && !removed) | |
{ | |
if (current.Equals(node)) | |
{ | |
current = current.Next; | |
if (null != previous) | |
{ | |
previous.Next = current; | |
} | |
removed = true; | |
break; | |
} | |
previous = current; | |
current = current.Next; | |
} | |
} | |
this.Count = 0; | |
ILinkedNode<int, TVal> n = this.First; | |
while (null != n) | |
{ | |
n.Key = this.Count; | |
n = n.Next; | |
this.Count++; | |
} | |
return removed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment