Sunday, February 5, 2012

LinkedList(Of T) Class C#

Today, I'm posting to about LinkedList class. LinkedList is one of System.Collections.Generic collection classes.

What LinkedList class can do:
You can remove nodes and reinsert them, either in the same list or in another list, which results in no additional object allocated on the heap. LinkedList accepts null value.

Please note: LinkedList class doesn't support chaining, splitting, cycles or other features that can leave the list in inconsistent state. The list remains consistent on a signle thread. The Linkedlist supports multithread read.

Please see the following examples:



   1:  using System;

   2:  using System.Text;

   3:  using System.Collections.Generic;

   4:   

   5:  public class Example

   6:  {

   7:      public static void Main()

   8:      {

   9:          // Create the link list.

  10:          string[] words =

  11:              { "the", "fox", "jumped", "over", "the", "dog" };

  12:          LinkedList<string> sentence = new LinkedList<string>(words);

  13:          Display(sentence, "The linked list values:");

  14:          Console.WriteLine("sentence.Contains(\"jumped\") = {0}",

  15:              sentence.Contains("jumped"));

  16:   

  17:          // Add the word 'today' to the beginning of the linked list.

  18:          sentence.AddFirst("today");

  19:          Display(sentence, "Test 1: Add 'today' to beginning of the list:");

  20:   

  21:          // Move the first node to be the last node.

  22:          LinkedListNode<string> mark1 = sentence.First;

  23:          sentence.RemoveFirst();

  24:          sentence.AddLast(mark1);

  25:          Display(sentence, "Test 2: Move first node to be last node:");

  26:   

  27:          // Change the last node be 'yesterday'.

  28:          sentence.RemoveLast();

  29:          sentence.AddLast("yesterday");

  30:          Display(sentence, "Test 3: Change the last node to 'yesterday':");

  31:   

  32:          // Move the last node to be the first node.

  33:          mark1 = sentence.Last;

  34:          sentence.RemoveLast();

  35:          sentence.AddFirst(mark1);

  36:          Display(sentence, "Test 4: Move last node to be first node:");

  37:   

  38:   

  39:          // Indicate, by using parentheisis, the last occurence of 'the'.

  40:          sentence.RemoveFirst();

  41:          LinkedListNode<string> current = sentence.FindLast("the");

  42:          IndicateNode(current, "Test 5: Indicate last occurence of 'the':");

  43:   

  44:          // Add 'lazy' and 'old' after 'the' (the LinkedListNode named current).

  45:          sentence.AddAfter(current, "old");

  46:          sentence.AddAfter(current, "lazy");

  47:          IndicateNode(current, "Test 6: Add 'lazy' and 'old' after 'the':");

  48:   

  49:          // Indicate 'fox' node.

  50:          current = sentence.Find("fox");

  51:          IndicateNode(current, "Test 7: Indicate the 'fox' node:");

  52:   

  53:          // Add 'quick' and 'brown' before 'fox':

  54:          sentence.AddBefore(current, "quick");

  55:          sentence.AddBefore(current, "brown");

  56:          IndicateNode(current, "Test 8: Add 'quick' and 'brown' before 'fox':");

  57:   

  58:          // Keep a reference to the current node, 'fox',

  59:          // and to the previous node in the list. Indicate the 'dog' node.

  60:          mark1 = current;

  61:          LinkedListNode<string> mark2 = current.Previous;

  62:          current = sentence.Find("dog");

  63:          IndicateNode(current, "Test 9: Indicate the 'dog' node:");

  64:   

  65:          // The AddBefore method throws an InvalidOperationException

  66:          // if you try to add a node that already belongs to a list.

  67:          Console.WriteLine("Test 10: Throw exception by adding node (fox) already in the list:");

  68:          try

  69:          {

  70:              sentence.AddBefore(current, mark1);

  71:          }

  72:          catch (InvalidOperationException ex)

  73:          {

  74:              Console.WriteLine("Exception message: {0}", ex.Message);

  75:          }

  76:          Console.WriteLine();

  77:   

  78:          // Remove the node referred to by mark1, and then add it

  79:          // before the node referred to by current.

  80:          // Indicate the node referred to by current.

  81:          sentence.Remove(mark1);

  82:          sentence.AddBefore(current, mark1);

  83:          IndicateNode(current, "Test 11: Move a referenced node (fox) before the current node (dog):");

  84:   

  85:          // Remove the node referred to by current.

  86:          sentence.Remove(current);

  87:          IndicateNode(current, "Test 12: Remove current node (dog) and attempt to indicate it:");

  88:   

  89:          // Add the node after the node referred to by mark2.

  90:          sentence.AddAfter(mark2, current);

  91:          IndicateNode(current, "Test 13: Add node removed in test 11 after a referenced node (brown):");

  92:   

  93:          // The Remove method finds and removes the

  94:          // first node that that has the specified value.

  95:          sentence.Remove("old");

  96:          Display(sentence, "Test 14: Remove node that has the value 'old':");

  97:   

  98:          // When the linked list is cast to ICollection(Of String),

  99:          // the Add method adds a node to the end of the list.

 100:          sentence.RemoveLast();

 101:          ICollection<string> icoll = sentence;

 102:          icoll.Add("rhinoceros");

 103:          Display(sentence, "Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':");

 104:   

 105:          Console.WriteLine("Test 16: Copy the list to an array:");

 106:          // Create an array with the same number of

 107:          // elements as the inked list.

 108:          string[] sArray = new string[sentence.Count];

 109:          sentence.CopyTo(sArray, 0);

 110:   

 111:          foreach (string s in sArray)

 112:          {

 113:              Console.WriteLine(s);

 114:          }

 115:   

 116:          // Release all the nodes.

 117:          sentence.Clear();

 118:   

 119:          Console.WriteLine();

 120:          Console.WriteLine("Test 17: Clear linked list. Contains 'jumped' = {0}",

 121:              sentence.Contains("jumped"));

 122:   

 123:          Console.ReadLine();

 124:      }

 125:   

 126:      private static void Display(LinkedList<string> words, string test)

 127:      {

 128:          Console.WriteLine(test);

 129:          foreach (string word in words)

 130:          {

 131:              Console.Write(word + " ");

 132:          }

 133:          Console.WriteLine();

 134:          Console.WriteLine();

 135:      }

 136:   

 137:      private static void IndicateNode(LinkedListNode<string> node, string test)

 138:      {

 139:          Console.WriteLine(test);

 140:          if (node.List == null)

 141:          {

 142:              Console.WriteLine("Node '{0}' is not in the list.\n",

 143:                  node.Value);

 144:              return;

 145:          }

 146:   

 147:          StringBuilder result = new StringBuilder("(" + node.Value + ")");

 148:          LinkedListNode<string> nodeP = node.Previous;

 149:   

 150:          while (nodeP != null)

 151:          {

 152:              result.Insert(0, nodeP.Value + " ");

 153:              nodeP = nodeP.Previous;

 154:          }

 155:   

 156:          node = node.Next;

 157:          while (node != null)

 158:          {

 159:              result.Append(" " + node.Value);

 160:              node = node.Next;

 161:          }

 162:   

 163:          Console.WriteLine(result);

 164:          Console.WriteLine();

 165:      }

 166:  }

 167:   

 168:  //This code example produces the following output:

 169:  //

 170:  //The linked list values:

 171:  //the fox jumped over the dog

 172:   

 173:  //Test 1: Add 'today' to beginning of the list:

 174:  //today the fox jumped over the dog

 175:   

 176:  //Test 2: Move first node to be last node:

 177:  //the fox jumped over the dog today

 178:   

 179:  //Test 3: Change the last node to 'yesterday':

 180:  //the fox jumped over the dog yesterday

 181:   

 182:  //Test 4: Move last node to be first node:

 183:  //yesterday the fox jumped over the dog

 184:   

 185:  //Test 5: Indicate last occurence of 'the':

 186:  //the fox jumped over (the) dog

 187:   

 188:  //Test 6: Add 'lazy' and 'old' after 'the':

 189:  //the fox jumped over (the) lazy old dog

 190:   

 191:  //Test 7: Indicate the 'fox' node:

 192:  //the (fox) jumped over the lazy old dog

 193:   

 194:  //Test 8: Add 'quick' and 'brown' before 'fox':

 195:  //the quick brown (fox) jumped over the lazy old dog

 196:   

 197:  //Test 9: Indicate the 'dog' node:

 198:  //the quick brown fox jumped over the lazy old (dog)

 199:   

 200:  //Test 10: Throw exception by adding node (fox) already in the list:

 201:  //Exception message: The LinkedList node belongs a LinkedList.

 202:   

 203:  //Test 11: Move a referenced node (fox) before the current node (dog):

 204:  //the quick brown jumped over the lazy old fox (dog)

 205:   

 206:  //Test 12: Remove current node (dog) and attempt to indicate it:

 207:  //Node 'dog' is not in the list.

 208:   

 209:  //Test 13: Add node removed in test 11 after a referenced node (brown):

 210:  //the quick brown (dog) jumped over the lazy old fox

 211:   

 212:  //Test 14: Remove node that has the value 'old':

 213:  //the quick brown dog jumped over the lazy fox

 214:   

 215:  //Test 15: Remove last node, cast to ICollection, and add 'rhinoceros':

 216:  //the quick brown dog jumped over the lazy rhinoceros

 217:   

 218:  //Test 16: Copy the list to an array:

 219:  //the

 220:  //quick

 221:  //brown

 222:  //dog

 223:  //jumped

 224:  //over

 225:  //the

 226:  //lazy

 227:  //rhinoceros

 228:   

 229:  //Test 17: Clear linked list. Contains 'jumped' = False

 230:  //



  


// ( reference to MSDN: msdn.microsoft.com/en-us/library/he2s3bh7.aspx)




No comments: