Introduction
In this blog, we are discussing the linked list concepts with simple programs.
The linked list is an abstract data type that represents the family of data structures where data structures represent a linear and sequential way.
- Please check the below snapshot:
We can see in the above snapshot A, B, C, that these are nodes. A node has two elements, data, and pointer. A pointer points towards to the next node. A points towards B and B points towards C.
We have 3 types of linked lists:
- Simple linked list: We have a node and the node points towards the next node. Here we have a one-way pointer.
- Double linked list: Node points towards the next node, also it will point to the previous node.
- Circular linked list: Every node will point towards the next node and also the last node points to the first node.
Let's create a simple linked list.
Step 1
First, create a simple console application.
Step 2
First, we need to create one node. Please create one class and name it as a node. This class has two properties, one is data and another one is the pointer.
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace LinkedList
- {
- public class Node
- {
- object data = null;
- Node next = null;
- }
- }
Step 3
We need to create one more class which holds the logic of the linked list concepts. Let's create AddFirst, AddLast, ReadAll methods.
In the AddFirst method, it will add data before the current head. First, we need to create the first item and we want this as our first item so we need to set items as the head.
- public void AddFirst(object data)
- {
- Node newitem = new Node();
- newitem.data = data;
- newitem.next = head;
- newitem = head;
- }
In the AddLast method, we are creating the last node. Loop and find the last node and if it is null this means this is the last element to the current next set new item.
- public void AddLast(object data)
- {
-
- Node newitem = new Node();
- newitem.data = data;
-
- if (head ==null)
- {
-
- head = newitem;
- head.next = null;
- }
- else
- {
-
- Node current = head;
- while(current.next != null)
- {
- current = current.next;
- }
-
- current.next = newitem;
- }
-
-
- }
In the ReadAll method using a loop, we can read the data of the node.
- public void ReadAll()
- {
- Node current = head;
-
- while(current.next !=null)
- {
- Console.WriteLine(current.data);
- current = current.next;
- }
-
- Console.WriteLine(current.data);
- Console.ReadLine();
-
- }
In your main method, create the object for class and run the application.
- class Program
- {
- static void Main(string[] args)
- {
- LinkList linkedlistobject = new LinkList();
- linkedlistobject.AddLast("One");
- linkedlistobject.AddLast("Two");
- linkedlistobject.AddLast("Three");
- linkedlistobject.AddLast("Four");
- linkedlistobject.AddFirst("Add data to first data");
- linkedlistobject.ReadAll();
- }
- }
Please check the below snapshot for results:
Summary
In this blog, we discussed a linked list and its types with a simple program.
Eat->Code->Sleep->Repeat.