A linked list is a linear data structure just like arrays but its elements do not store in the contagious location. Elements in a linked list are connected through pointers as shown in the below image.
Link list consists of nodes. Each node contains a data field and a pointer reference to the next node in the list.
Important Points about the linked list
- A linked list can be used to store linear data of diffent type.
- Dynamic size.
- Ease of insertion and deletion of an element.
- Random access of an element is not allowed. if we want to search an element we have to go sequentially starting from the first node.
- Extra memory space would be required for the pointer with each element of a linked list.
- A linked list is represented by a pointer to the first node o the list. The first node called head.if the linked list is empty then the head is NULL.
Let's start implementation of a simple linked list in C#
I have created a project of the console application and named it "LearningDataStructure"
First, we will create a Node class. Right click on the solution explorer and add a class with name Node.Write code in the class as in the screenshot.
Now we will add another class with the name "LinkedList" and add code as in the screenshot.
And Finally, Replace the following lines of code in the program class.
static void Main(string[] args)
{
LinkedList list = new LinkedList();
list.Head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
list.Head.Next = second;
second.Next = third;
}
Now, you have learned the basics of a linked list by creating a simple linked list. In my another article I will discuss, how to insert and delete an element in a linked list.
Happy Programming :)
Comments
Post a Comment