Working on LinkedList Using C#

Introduction

LinkedList<T> represents the doubly linked list, where T is any specific datatype. It represents the collection of nodes and each node contains an element. Each node is linked to the preceding node and the following node. The doubly linked list only knows the first and the last node. There are various operations that we can perform in the LinkedList.

In this article I explain how to create a LinkedList and how we perform the operations on a LinkedList. For this the following steps are used.

Step 1

Open Visual Studio 2010 and click on File->New->Project and then click on Console application. Give the name of the project as LinkedList.

pic1.jpg

Step 2

Creation of the LinkedList

The LinkedList can be created as:

namespace Linkedlist

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of the empty linkedlist

            LinkedList<string> linkedList = new LinkedList<string>();

            string [] courses= {"MCA","BCA","MBA","BBA","BTech","MTech"};

            //creation of the linkedlist having the element

            LinkedList<string> linkedlist1=new LinkedList<string>(courses);                

        }

    }

}

Some of the methods that we use for adding the elements in the list are:

AddFirst() - Its add the node at the first position in the LinkedList.
AddLast() - Its add the node at the last position in the
LinkedList.
AddBefore() - Its add the node before the given node.
AddAfter() - Its add the node after the given node.

Now to add the nodes on the empty list that I created, named LinkedList, write the following code:

namespace Linkedlist

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of the empty linkedlist

            LinkedList<string> linkedList = new LinkedList<string>();

            //add the element at the first position

            linkedList.AddFirst("one");

            // add node at the last position

            var nodeThree = linkedList.AddLast("three");

            // add node before the given element

            linkedList.AddBefore(nodeThree, "two");

            // add node after given node

            linkedList.AddAfter(nodeThree, "four");                           

        }

    }

}

Display both the list

To display both the lists, write the code as:

namespace Linkedlist

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of the empty linkedlist

            LinkedList<string> linkedList = new LinkedList<string>();

            string [] courses= {"MCA","BCA","MBA","BBA","BTech","MTech"};

            //creation of the linkedlist having the element

            LinkedList<string> linkedlist1=new LinkedList<string>(courses);

            //add the element at the first position

            linkedList.AddFirst("one");

            // add node at the last position

            var nodeThree = linkedList.AddLast("three");

            // add node before the given element

            linkedList.AddBefore(nodeThree, "two");

            // add node after given node

            linkedList.AddAfter(nodeThree, "four");  

            //display the list-linkedlist

            Console.WriteLine("The first list is as:");

            foreach (string value in linkedList)

            {

                Console.WriteLine(value);

            }

            //display the second list

            Console.WriteLine("The second lit is as:");

            foreach(string list in linkedlist1)

            {

                Console.WriteLine(list);

            }

        }

    }

}

 

OUTPUT

pic2.jpg

 

Step 3

 

Perform various operation on linked list


Count() - count() is used to count the number of nodes/or elements in the linked list. For example:

namespace Linkedlist

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of the empty linkedlist

            LinkedList<string> linkedList = new LinkedList<string>();

            //add the element at the first position

            linkedList.AddFirst("one");

            // add node at the last position

            var nodeThree = linkedList.AddLast("three");

            // add node before the given element

            linkedList.AddBefore(nodeThree, "two");

            // add node after given node

            linkedList.AddAfter(nodeThree, "four");

            //display the list-linkedlist

            Console.WriteLine("The first list is as:");

            foreach (string value in linkedList)

            {

                Console.WriteLine(value);

            }

            //count the number of element in the list

            int i = linkedList.Count();

            Console.WriteLine("The elements in the list are:" + i);

        }

    }

}

 

OUTPUT

pic3.jpg


First() - The First() method is used to get the first node in the LinkedList.
Last() - The Last() method is used to get the last node in the LinkedList.

For example,

namespace Linkedlist

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of the empty linkedlist

            LinkedList<string> linkedList = new LinkedList<string>();

            //add the element at the first position

            linkedList.AddFirst("one");

            // add node at the last position

            var nodeThree = linkedList.AddLast("three");

            // add node before the given element

            linkedList.AddBefore(nodeThree, "two");

            // add node after given node

            linkedList.AddAfter(nodeThree, "four");

            //display the list-linkedlist

            Console.WriteLine("The first list is as:");

            foreach (string value in linkedList)

            {

                Console.WriteLine(value);

            }

            Console.WriteLine("The first node in the linkedlist is:" + linkedList.First());

            Console.WriteLine("The last node in the linkedlist is:" + linkedList.Last());

        }

    }

}

 

OUTPUT

pic4.jpg

Contains() - It determines whether the node is contained in the list or not. If yes then it gives true otherwise false.
Remove() - Removes the first occurrence of the specified value from the LinkedList<T>.
RemoveFirst() - Removes the node at the start of the LinkedList<T>.
RemoveLast() - Removes the node at the end of the LinkedList<T>.
Clear() - It clear the list.

For example:

namespace Linkedlist

{

    class Program

    {

        static void Main(string[] args)

        {

            //creation of the empty linkedlist

            LinkedList<string> linkedList = new LinkedList<string>();

            //add the element at the first position

            linkedList.AddFirst("one");

            // add node at the last position

            var nodeThree = linkedList.AddLast("three");

            // add node before the given element

            linkedList.AddBefore(nodeThree, "two");

            // add node after given node

            linkedList.AddAfter(nodeThree, "four");

            //display the list-linkedlist

            Console.WriteLine("The first list is as:");

            foreach (string value in linkedList)

            {

                Console.WriteLine(value);

            }

            //use of contain() method to find whether the node is present in the list or not

            Console.WriteLine("The node one contain in the list:" + linkedList.Contains("one"));

            Console.WriteLine("The node six contain in the list:" + linkedList.Contains("six"));

            //remove the node from the list

            linkedList.Remove("three");

            //remove the first node from the list

            linkedList.RemoveFirst();

            //remove the last node from the list

            linkedList.RemoveLast();

            Console.WriteLine("The list is as:");

            foreach (string value in linkedList)

            {

                Console.WriteLine(value);

            }

            linkedList.Clear();

            Console.WriteLine("The list is now empty");

            foreach (string value in linkedList)

            {

                Console.WriteLine(value);

            }

        }

    }

}

 

OUTPUT

pic5.jpg

Summary

In this article I explained how to create a LinkedList and the various operations on the LinkedList.

Up Next
    Ebook Download
    View all
    Learn
    View all