C# LinkedList Tutorial with Examples

C# LinkedList Tutorial with Examples

Today, We want to share with you LinkedList in C#.Net with example
.In this post we will show you LinkedList in C#.Net with example, hear for LinkedList(T) Class (System.Collections.Generic) we will give you demo and example for implement.In this post, we will learn about Linked List Implementation In C# with an example.

Introduction: LinkedList in C#.Net

In this post, we will learn about LinkedList in C#.Net with example.

Linked List

A linked list is a linear data structure. It is a collection of elements. The element is called as Node.Every element has value(data) and reference of next node. The very first node is called as Head and last element has reference to a null value.

READ :  Vuejs Interview Questions And Answers

Types of Linked Lists

There are 3 types of linked lists in C#.

1. Singly Linked List :

This type of Linked List has a value(data) and a reference to next node. In this, last node’s next reference will be null.

2. Doubly Linked List:

This type of Linked List has a data and 2 references, one for next node and another for previous node and last node’s next reference will be null.

3. Circular Linked List:

In the circular linked list, last node’s next reference will be head or first element. The circular linked list can be the singly linked list or doubly linked list.

READ :  Laravel JQuery AJAX Pagination Step By step

Advantage of Linked List

The major advantage of the linked list is that it is a dynamic data structure.It can grow and shrink in size we can add a dynamic number of data in a linked list without bothering about the size of the linked list.

Disadvantage of Linked List

The disadvantage of a linked list is that it takes more space than an array. Because it stores a reference of next/previous nodes.

Operations in Linked List

  • 1. Add node to last element
  • 2. Add node as fist element
  • 3. Remove node from start
  • 4. Traverse whole linked list

Now Create one Console Application in Visual Studio and write below code in it.

[php]
using System;
using System.Collections;

namespace ConsoleDemo
{
class Program
{
static void Main(string[] args)
{
// LinkedList Demo code
LinkedList _LinkedList = new LinkedList();
_LinkedList.PrintAllNodes();
Console.WriteLine();

READ :  Laravel Get last executed mysql query

_LinkedList.AddAtLast(12);
_LinkedList.AddAtLast(“DotNetK.com”);
_LinkedList.AddAtLast(“Live24u.com”);
_LinkedList.AddAtLast(34);
_LinkedList.PrintAllNodes();
Console.WriteLine();

_LinkedList.AddAtStart(55);
_LinkedList.PrintAllNodes();
Console.WriteLine();

_LinkedList.RemoveFromStart();
_LinkedList.PrintAllNodes();

Console.ReadKey();
}
}

public class LinkedList
{
public LinkedList()
{
head = new Node();
current = head;
}

private Node head;
private Node current;
public int Count;

public void AddAtLast(object data)
{
Node newNode = new Node();
newNode.Value = data;
current.Next = newNode;
current = newNode;
Count++;
}

public void AddAtStart(object data)
{
Node newNode = new Node() { Value = data };
newNode.Next = head.Next;
head.Next = newNode;
Count++;
}

public void RemoveFromStart()
{
if (Count > 0)
{
head.Next = head.Next.Next;
Count–;
}
else
{
Console.WriteLine(“linked list is empty.”);
}
}

public void PrintAllNodes()
{
//Traverse from head
Console.Write(“Head ->”);
Node curr = head;
while (curr.Next != null)
{
curr = curr.Next;
Console.Write(curr.Value);
Console.Write(“->”);
}
Console.Write(“NULL”);
}
}

public class Node
{
public Node Next;
public object Value;
}
}

[/php]

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about LinkedList in C#.Net with example and full description.I would like to have feedback on my Pakainfo.com blog.Your valuable feedback, question, or comments about this article are always welcome.If you enjoyed and liked this post, don’t forget to share.

Leave a Comment