Basic Operations
Helping function.
private static void Display(LinkedList<string> words, string test)
{
Console.WriteLine(test);
foreach (string word in words)
{
Console.Write(word + " ");
}
}
Initialization of Linked List.
// in Main class
string[] words = { "the", "fox", "jumped", "over", "the", "dog" };
LinkedList<string> sentence = new LinkedList<string>(words);
Display(sentence, "The linked list values:");
//The linked list values:
//the fox jumped over the dog
Adding Operation.
sentence.AddFirst("today");
Display(sentence, "Test 1: Add 'today' to beginning of the list:");
//Test 1: Add 'today' to beginning of the list:
//today the fox jumped over the dog
Moving Operation.
LinkedListNode<string> mark1 = sentence.First;
sentence.RemoveFirst();
sentence.AddLast(mark1);
Display(sentence, "Test 2: Move first node to be last node:");
//Test 2: Move first node to be last node:
//the fox jumped over the dog today
Modifying Operation.
sentence.RemoveLast();
sentence.AddLast("yesterday");
Display(sentence, "Test 3: Change the last node to 'yesterday':");
//Test 3: Change the last node to 'yesterday':
//the fox jumped over the dog yesterday
mark1 = sentence.Last;
sentence.RemoveLast();
sentence.AddFirst(mark1);
Display(sentence, "Test 4: Move last node to be first node:");
//Test 4: Move last node to be first node:
//yesterday the fox jumped over the dog