Move the first node to be the last node : LinkedList « Collections Data Structure « C# / C Sharp






Move the first node to be the last node

 

using System;
using System.Text;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        // Create the link list.
        string[] words ={ "the", "fox", "jumped", "over", "the", "dog" };
        LinkedList<string> sentence = new LinkedList<string>(words);
        Display(sentence, "The linked list values:");

        LinkedListNode<string> mark1 = sentence.First;
        sentence.RemoveFirst();
        sentence.AddLast(mark1);
        Display(sentence, "Test 2: Move first node to be last node:");

    }

    private static void Display(LinkedList<string> words, string test)
    {
        Console.WriteLine(test);
        foreach (string word in words)
        {
            Console.Write(word + " ");
        }
    }

}

   
  








Related examples in the same category

1.Create LinkedList Generic Class with a string array
2.Add the word 'today' to the beginning of the linked list
3.Change the last node be 'yesterday'
4.Move the last node to be the first node
5.Indicate, by using parentheisis, the last occurence of 'the'
6.Initializes a new instance of the LinkedList class that is empty.
7.LinkedListNode(T) Class represents a node in a LinkedList. This class cannot be inherited.