Member-only story

LinkedList in C# with Examples

Shahzad Aslam
3 min readApr 18, 2024

--

The LinkedList related to System.Collections.Generic namespace. A LinkedList is a doubly linked list data structure that allows you to store and manipulate a collection of elements. Unlike arrays or lists, a LinkedList doesn’t require contiguous memory and provides efficient insertions and deletions in the middle of the list.

Features with linkedlist in C#:

Creating a LinkedList:

You can use the System.Collections.Generic namespace to work with LinkedList. You can use the following code to create the linkedlist.

Example

using System;
using System.Collections.Generic;
LinkedList<int> linkedfruitprices = new LinkedList<int>();
LinkedList<string> linkedfruitnames = new LinkedList<string>();

Adding Elements:

You can add elements to a LinkedList using the AddLast method to add to the end, or the AddFirst method to add to the beginning of the list:

Example

linkedfruitprices.AddLast(12);
linkedfruitprices.AddLast(16);
linkedfruitprices.AddLast(22);
linkedfruitprices.AddFirst(2);
linkedfruitprices.AddFirst(19);
linkedfruitnames.AddLast("Apple");
linkedfruitnames.AddLast("Mango");
linkedfruitnames.AddLast("Cherry")…

--

--

Shahzad Aslam
Shahzad Aslam

Written by Shahzad Aslam

Welcome to c-sharptutorial.com. I am Shahzad Aslam. I am the founder of c-sharptutorial.com. I'm currently living near Islamabad, Pakistan.

No responses yet