Using Linked List in C#

What we going to make is a linked list. Yeah I know there is a class which does the same as a linked list called ArrayList, but we (as a diehard C# programmer) want absolute control and knowledge of what we use.

What we going to make is a linked list. Yeah I know there is a class which does the same as a linked list called ArrayList, but we (as a diehard C# programmer) want absolute control and knowledge of what we use.

First what is a linked list?

A linked list is a dynamically sized array. An array has a size and you have to deal with it you cant just resize an array. Unlike an array a linked list has no absolute size. It can hold as many variables as you want it to.

When to use?

In circumstances where you dont know how many variables you want to add. For example it could be just 1 or it could be 1000. In such a situation its stupid to make an array of 10000 it would be a waist of memory.

Now a design of our classes LinkedList and Node.

 

Note: Prefix i is for an integer and a prefix n is for a Node.

In the Node class there is a Previous and a Next Node. Because of that all Nodes are linked to eachother (thats why they called it a linkedlist).

Here is the Node class

  1. public class Node  
  2. {  
  3.     protected Node nPrevious;  
  4.     protected Node nNext;  
  5.     protected object Object;  
  6.     #region Properties  
  7.     public object Value  
  8.     {  
  9.         get  
  10.         {  
  11.             return Object;  
  12.         }  
  13.         set  
  14.         {  
  15.             Object = value;  
  16.         }  
  17.     }  
  18.     public Node Previous  
  19.     {  
  20.         get  
  21.         {  
  22.             return nPrevious;  
  23.         }  
  24.         set  
  25.         {  
  26.             nPrevious = value;  
  27.         }  
  28.     }  
  29.     public Node Next  
  30.     {  
  31.         get  
  32.         {  
  33.             return nNext;  
  34.         }  
  35.         set  
  36.         {  
  37.             nNext = value;  
  38.         }  
  39.     }  
  40.     #endregion  
  41.     public Node(Node PrevNode, Node NextNode, object obj)  
  42.     {  
  43.         nPrevious = PrevNode;  
  44.         nNext = NextNode;  
  45.         Object = obj;  
  46.     }  
  47. }  
As you can see there isnt much of a surprise if you studied the class design.

Next thing the LinkedList class.

  1. public class LinkedList  
  2. {  
  3.     int iCount = 1;  
  4.     int iCurrent = 0;  
  5.     Node nCurrent;  
  6.     #region Properties  
  7.     /// <summary>  
  8.     /// Give back how many nodes there are.  
  9.     /// </summary>  
  10.     public int Count  
  11.     {  
  12.         get  
  13.         {  
  14.             return iCount;  
  15.         }  
  16.     }  
  17.     /// <summary>  
  18.     /// Gives back the current Node  
  19.     /// </summary>  
  20.     public Node CurrentNode  
  21.     {  
  22.         get  
  23.         {  
  24.             return nCurrent;  
  25.         }  
  26.     }  
  27.     /// <summary>  
  28.     /// Keeps track of the index where you are  
  29.     /// </summary>  
  30.     public int CurrentNodeIndex  
  31.     {  
  32.         get  
  33.         {  
  34.             return iCurrent;  
  35.         }  
  36.     }  
  37.     #endregion  
  38.     /// <summary>  
  39.     /// Default and only Constructor  
  40.     /// SetUp our LinkedList  
  41.     /// </summary>  
  42.     /// <param name="obj">Value for the first Node</param>   
  43.     public LinkedList(object obj)  
  44.     {  
  45.         nCurrent = new Node(nullnull, obj);  
  46.         nCurrent.Next = null;  
  47.         nCurrent.Previous = null;  
  48.     }  
  49.     /// <summary>  
  50.     /// This function will add another Node  
  51.     /// </summary>  
  52.     /// <param name="obj">Value for the added Node</param>  
  53.     public void AddNode(object obj)  
  54.     {  
  55.         if (nCurrent.Next == null)  
  56.         {  
  57.             nCurrent = nCurrent.Next = new Node(nCurrent, null, obj);  
  58.         }  
  59.         else  
  60.         {  
  61.             nCurrent = nCurrent.Next = new Node(nCurrent, nCurrent.Next, obj);  
  62.         }  
  63.         iCount++;  
  64.         iCurrent++;  
  65.     }  
  66.     /// <summary>  
  67.     /// Goes to the next Node  
  68.     /// </summary>  
  69.     public void ToNext()  
  70.     {  
  71.         // Checks whether the Next Node is null  
  72.         // if so it throws an exception.  
  73.         // You can also do nothing but I choos for this.  
  74.         if (nCurrent.Next == null)  
  75.         {  
  76.             throw new Exception("There is no next node!");  
  77.         }  
  78.         else // If everything is OK  
  79.         {  
  80.             nCurrent = nCurrent.Next;  
  81.             iCurrent++;  
  82.         }  
  83.     }  
  84.     /// <summary>  
  85.     /// Goes to the previous Node  
  86.     /// </summary>  
  87.     public void ToPrevious()  
  88.     {  
  89.         // Look at ToNext();  
  90.         if (nCurrent.Previous == null)  
  91.         {  
  92.             throw new Exception("There is no previous node!");  
  93.         }  
  94.         else  
  95.         {  
  96.             nCurrent = nCurrent.Previous;  
  97.             iCurrent--;  
  98.         }  
  99.     }  
  100.     /// <summary>  
  101.     /// Goes to the index you fill in  
  102.     /// </summary>  
  103.     /// <param name="index">Index Where to go?</param>  
  104.     public void GoTo(int index)  
  105.     {  
  106.         if (iCurrent < index)  
  107.         {  
  108.             ToNext();  
  109.         }  
  110.         else if (iCurrent > index)  
  111.         {  
  112.             ToPrevious();  
  113.         }  
  114.     }  
  115. }  
Conclusion

It isn't hard to make a linked list and I guess most programmers are already familiar with linked lists. And for the C++ programmers out there you see without pointers :p ( I just hate pointers)