A LinkedList is a linear data structure which stores element in the non-contiguous location. The elements in a linked list are linked with each other using pointers. Or in other words, LinkedList consists of nodes where each node contains a data field and a reference(link) to the next node in the list.
In C#, LinkedList is the generic type of collection which is defined in System.Collections.Generic namespace. It is a doubly linked list, therefore, each node points forward to the Next node and backward to the Previous node. It is a dynamic collection which grows, according to the need of your program. It also provides fast inserting and removing elements.
- The LinkedList class implements the ICollection<T>, IEnumerable<T>, IReadOnlyCollection<T>, ICollection, IEnumerable, IDeserializationCallback, and ISerializable interfaces.
- It also supports enumerators.
- You can remove nodes and reinsert them, either in the same list or in another list, which results in no additional objects allocated on the heap.
- Every node in a LinkedList<T> object is of the type LinkedListNode<T>.
- It does not support chaining, splitting, cycles, or other features that can leave the list in an inconsistent state.
- If the LinkedList is empty, the First and Last properties contain null.
- The capacity of a LinkedList is the number of elements the LinkedList can hold.
- In LinkedList, it is allowed to store duplicate elements but of the same type.
How to create a LinkedList?
A LinkedList class has 3 constructors which are used to create a LinkedList which are as follows:
- LinkedList(): This constructor is used to create an instance of the LinkedList class that is empty.
- LinkedList(IEnumerable): This constructor is used to create an instance of the LinkedList class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied.
- LinkedList(SerializationInfo, StreamingContext): This constructor is used to create an instance of the LinkedList class that is serializable with the specified SerializationInfo and StreamingContext.
Let’s see how to create an LinkedList using LinkedList() constructor:
Step 1: Include System.Collections.Generic namespace in your program with the help of using keyword:
Step 2: Create a LinkedList using LinkedList class as shown below:
LinkedList <Type_of_linkedlist> linkedlist_name = new LinkedList <Type_of_linkedlist>();
Step 3: LinkedList provides 4 different methods to add nodes and these methods are:
- AddAfter: This method is used to add a new node or value after an existing node in the LinkedList.
- AddBefore: This method is used to add a new node or value before an existing node in the LinkedList.
- AddFirst: This method is used to add a new node or value at the start of the LinkedList.
- AddLast: This method is used to add a new node or value at the end of the LinkedList.
Step 4: The elements of the LinkedList is accessed by using a foreach loop or by using for loop. As shown in the below example.
Best students of XYZ university: Zoya Shilpa Rohit Rohan Juhi Zoya
How to remove elements from the LinkedList?
In LinkedList, it is allowed to remove elements from the LinkedList. LinkedList<T> class provides 5 different methods to remove elements and the methods are:
- Clear(): This method is used to remove all nodes from the LinkedList.
- Remove(LinkedListNode): This method is used to remove the specified node from the LinkedList.
- Remove(T): This method is used to remove the first occurrence of the specified value from the LinkedList.
- RemoveFirst(): This method is used to remove the node at the start of the LinkedList.
- RemoveLast(): This method is used to remove the node at the end of the LinkedList.
Best students of XYZ university initially: Zoya Shilpa Rohit Rohan Juhi Zoya Best students of XYZ university in 2000: Shilpa Rohit Rohan Juhi Zoya Best students of XYZ university in 2001: Shilpa Rohan Juhi Zoya Best students of XYZ university in 2002: Rohan Juhi Zoya Best students of XYZ university in 2003: Rohan Juhi Number of students: 0
How to check the availability of the elements in the LinkedList?
In LinkedList, you can check whether the given value is present or not using the Contains(T) method. This method is used to determine whether a value is in the LinkedList.
- Queue - Linked List Implementation
- Implementation of Deque using doubly linked list
- Recursively Reversing a linked list (A simple implementation)
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Create new linked list from two given linked list with greater element at each node
- Convert singly linked list into circular linked list
- Merge a linked list into another linked list at alternate positions
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- List Implementation in C#
- Convert Singly Linked List to XOR Linked List
- Check if a linked list is Circular Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.