Linked List using C#
Lot many times, this question hits our mind that how Linked List data structure is working with the object oriented Design System. Sometimes, it is bit tricky to understand the Core Object Oriented design for such kind of cases.
Today, We are going to understand that how Linked List data Structure is working with Oops.
What is Linked List ?
In their words, a linked list is a linear collection of data elements, whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence.
How is the Linked List structure ?
Linked List can be structured as Single Linked List or Double Linked List.
Pretty Cool structure, but how it is working ? If we carefully watch this design, it says everything. In Node1, it has value 23 and containing the address of another node (Node2).
It means, that we need such kind of class which can contains data and address.
Here, I have a class which has two properties as Data and Next (Address of Next Node) and I initialized with the value and next address will be null.
Let’s Create another class, which performs the operations of Linked List like insert the item from front or insert the item from the back.
Here I have a class as SingleLinkedList which is having the variable as Head of type LinkedList class, which will be placed at first element, why ? because then only we can traverse to linked elements.
I have a method named as InsertFront along with one parameter as data of integer type and I am creating a new node of type LinkedList and address will be referenced to Head type.
Confusing ???????????????????????????
Think in this way, Initially Head will be null and if we call the InsertFront method with value as 23 it will create one node as :
Here we created first node and copied to Head variable again. Remember, Head was null but now it is pointed to first node.
Now again call the InsertFront method along with 24 as value and represented as :
Here is second node which becomes First Node and pointed to existing node which we created earlier and the process goes on.
Let’s discuss that how we can add the elements from the back, it is little bit more tricky but if you understand the process, it will easy for you get through.
As we discussed before, Head is initially NULL because it not yet initialized after that it pointed to the first element therefore we put the first condition as Head as NULL and let it initialized with the first element, if not exist.
But if Head is not NULL, then I have created the object as Node and I created another object as Current_Node and give the reference of Head.
After that, I traverse the Current_Node and check whether the Current_Node.Next is NULL or not. Once I get the last element of Current_Node, then I copied the reference of Node to the Current_Node.
At last, we have to show our list which has been created, so here is the implementation.
Which will going to display the complete list of elements.
I hope, you enjoyed this blog and in my later blog we will discuss about the Double Linked List structure and implementation along with the traversal process in both the ways.
If you like this blog, hit claps as a token of your appreciation.