How to Implement Linked List in C# with Example
To implement a linked list in C#, create a
Node class to hold data and a reference to the next node, then build a LinkedList class to manage nodes with methods to add and traverse them. This structure allows dynamic data storage where elements link to the next, unlike arrays.Syntax
A linked list in C# typically uses two classes: Node and LinkedList.
Nodeholds the data and a reference to the next node.LinkedListmanages the nodes, usually with a reference to the head node.- Methods like
AddandDisplayhelp insert and show elements.
csharp
public class Node { public int Data; public Node Next; public Node(int data) { Data = data; Next = null; } } public class LinkedList { private Node head; public void Add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void Display() { Node current = head; while (current != null) { System.Console.Write(current.Data + " "); current = current.Next; } System.Console.WriteLine(); } }
Example
This example shows how to create a linked list, add three numbers, and print them in order.
csharp
using System; public class Node { public int Data; public Node Next; public Node(int data) { Data = data; Next = null; } } public class LinkedList { private Node head; public void Add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } } public void Display() { Node current = head; while (current != null) { Console.Write(current.Data + " "); current = current.Next; } Console.WriteLine(); } } class Program { static void Main() { LinkedList list = new LinkedList(); list.Add(10); list.Add(20); list.Add(30); list.Display(); } }
Output
10 20 30
Common Pitfalls
Common mistakes when implementing linked lists include:
- Not initializing the head node before adding elements, causing null reference errors.
- Forgetting to update the
Nextpointer when adding new nodes, which breaks the chain. - Not handling the empty list case when displaying or traversing nodes.
- Confusing the
Nextpointer with the node itself, leading to infinite loops or crashes.
Always check if the list is empty before operations and update pointers carefully.
csharp
/* Wrong way: forgetting to update Next pointer */ public void AddWrong(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; // Missing while loop to find last node current.Next = newNode; // Overwrites first node's next } } /* Right way: traverse to last node before adding */ public void AddRight(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.Next != null) { current = current.Next; } current.Next = newNode; } }
Quick Reference
- Node class: stores data and reference to next node.
- LinkedList class: manages nodes with a head pointer.
- Add method: adds new nodes at the end.
- Display method: prints all node data in order.
Key Takeaways
Create a Node class with data and a next pointer to build linked lists.
Manage the list with a LinkedList class that tracks the head node.
Always handle empty list cases to avoid null reference errors.
Traverse to the last node before adding new nodes to maintain links.
Use loops to display or process all nodes in the list.