patterncsharpMajor
Linked List in C#
Viewed 0 times
listlinkedstackoverflow
Problem
I was searching for a implementation of a Linked List with the same power as a native
I wanted functionality closer to a
I didn't find one so I made one of my own:
I'm searching for mistakes, performance improvements or missing functionality.
Note: This is not about a circular or doubly linked list; it's just about a singly linked list.
```
public class Node
{
public T data;
public Node next;
}
public class MyLinkedList : IEnumerable, System.Collections.IEnumerable
{
private Node _headNode;
private int _count;
public int Count { get { { return _count; } } }
private IEnumerable> Nodes
{
get
{
Node node = _headNode;
while (node != null)
{
yield return node;
node = node.next;
}
}
}
private Node Pop()
{
Node tResult = null;
if (_headNode != null)
{
tResult = _headNode;
_headNode = _headNode.next;
_count--;
}
return tResult;
}
private void Push(Node item)
{
item.next = _headNode;
_headNode = item;
_count++;
}
private Node NodeAt(int index)
{
if (index _count)
{
throw new IndexOutOfRangeException("Index");
}
int counter = 0;
foreach (Node item in Nodes)
{
if (counter == index)
{
return item;
}
counter++;
}
return null;
}
public MyLinkedList()
{
_headNode = null;
_cou
List.I wanted functionality closer to a
List. The System.Collections.Generic.LinkedList misses some methods I'm used to work with, like Add Sort or access by index mylist[0]I didn't find one so I made one of my own:
- I added the same constructors as
List
- I used a merge sort which should be the most performant for this case
- I added
IEnumberableto be able to use the power of Linq
- I added all native methods of a
List
I'm searching for mistakes, performance improvements or missing functionality.
Note: This is not about a circular or doubly linked list; it's just about a singly linked list.
```
public class Node
{
public T data;
public Node next;
}
public class MyLinkedList : IEnumerable, System.Collections.IEnumerable
{
private Node _headNode;
private int _count;
public int Count { get { { return _count; } } }
private IEnumerable> Nodes
{
get
{
Node node = _headNode;
while (node != null)
{
yield return node;
node = node.next;
}
}
}
private Node Pop()
{
Node tResult = null;
if (_headNode != null)
{
tResult = _headNode;
_headNode = _headNode.next;
_count--;
}
return tResult;
}
private void Push(Node item)
{
item.next = _headNode;
_headNode = item;
_count++;
}
private Node NodeAt(int index)
{
if (index _count)
{
throw new IndexOutOfRangeException("Index");
}
int counter = 0;
foreach (Node item in Nodes)
{
if (counter == index)
{
return item;
}
counter++;
}
return null;
}
public MyLinkedList()
{
_headNode = null;
_cou
Solution
Just a few quick remarks:
- Creating a linked list from another enumerable stores items in reverse order. That's probably not what most people would expect. Note that
System.Collections.Generic.LinkedListdoes preserve the original order.
- A similar case can be made for
Add: it adds items to the front. People familiar withListwould expect it to add at the end. Note thatLinkedListexplicitly usesAddFirstandAddLast, likely to prevent such confusion. Granted, not all collections are ordered, but if you do want to stick to this behavior, then at least document it.
- Implement
ICollection()andIList()while you're at it. You already implemented most of their functionality anyway (sometimes with different method names, or methods with different argument order).
- Keeping track of the tail node allows you to improve the performance of adding items at the end (at the expense of making removing and inserting nodes a bit more complicated).
- There are several methods that don't really need to be member methods, such as
Find,DistinctandReverse. Linq already provides extension methods for these (using mostly the same names, except thatFindis calledWhere).
- The same can be said for
ForEach: it can easily be implemented as an extension method, which would make it available for everything that implementsIEnumerable(). Or you could just useforeach.
- Some methods appear to be unused, such as
PopandPush.
Context
StackExchange Code Review Q#138142, answer score: 22
Revisions (0)
No revisions yet.