The MSDN provides some naming conventions, the relevant bit being that
public members should be
Public Members, and types, should ideally have at least minimal inline documentation (
\\\) to make it perfectly clear how to consume the API.
It's usually for the best to strip out old (commented) code. It has a habit of not being maintained and becoming incompatable with the code that is there. It also adds clutter.
For 'export' code, it's rarely useful to have methods which explicitly write to the console: much more useful to have something that writes to an abstract
TextWriter (which can take
Console.Out or a
StreamReader), or that just returns a string. These give the consumer many more options.
deleteOdd(ref MyLinkedList) probably can't cope with empty lists (being passed them or returning them).
printLL isn't a great variable name: what is "LL"? It's a member of
MyLinkedList, so the "LL" bit is redundant; I spect a while trying to work out if it was something to do with printing left to right or something.
mmm isn't a great variable name either: I can only assume it was meant to be
Below are some methods which just about tally with yours, but using
LinkedList<T>, and incorporating some of the suggestions above.
I don't know if you have a particular reason for needing a custom LinkedList implementation (not suggested by the code), but you can implement your methods tidily with .NET's built in
LinkedList<T>, which could be argued to provide a nicer API. Crucially, it wraps up a linked list of
LinkedListNode<T>s under a
LinkedList<T>, which allows some (illusion of) separation of concerns, and spares you having to keep track of the head of the list explicitly. It also allows you to have an 'empty' list, which is good.
This method is considerably shorter than before, and doesn't require a
ref parameter. This works with empty lists. This is a much more understandable implementation, and much harder to 'get wrong', because of the API for
/// Removes odd elements from a LinkedList of integers
public static void DeleteOdd(LinkedList<int> ll)
LinkedListNode<int> cur = ll.First; // grab first node
while (cur != null)
var next = cur.Next; // make a note of the next node (will be null if cur is the last element)
if (cur.Value % 2 != 0)
ll.Remove(cur); // remove the current node if odd
cur = next; // advance to the next node
Here are two possible alternatives, which don't depend explicitly on
Console: one just produces a string, the other prints to a
TextWriter (sadly there is no better interface). These are both extension methods for
LinkedList<T>, but there is no particular need for this.
/// Renders a linked list as a string
public static string Render<T>(this LinkedList<T> ll)
StringBuilder sb = new StringBuilder();
foreach (var e in ll)
/// Prints a linked list to a TextWriter, optionally appending a NewLine
public static void Print<T>(this LinkedList<T> ll, TextWriter textWriter, bool newLine = true)
foreach (var e in ll)
I wouldn't necessarily condone having the
newLine parameter being optional, or indeed being there at all: this is just for illustration of some options. It would probably be better to have both
PrintLine if you need both.
Note the nicer API for building a
private static void ExampleUsage()
LinkedList<int> ll = new LinkedList<int>();