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;
- }
- }
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 is null that means it is the first item and add the new item and set next node to null
- if (head ==null)
- {
- head = newitem;
- head.next = null;
- }
- else
- {
- // if the head is not null and find out the last node by loop
- Node current = head;
- while(current.next != null)
- {
- current = current.next;
- }
- current.next = newitem;
- }
- }
- public void ReadAll()
- {
- Node current = head;
- // loop until the last node
- 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.
Please check the below snapshot for results:
- 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();
- }
- }
Summary
In this blog, we discussed a linked list and its types with a simple program.
Eat->Code->Sleep->Repeat.

Join the conversation! Your thoughts help the community grow.