Sai A Sai A
Updated date Jun 28, 2023
In this blog, we will discover the power of doubly linked lists in C#. This blog post takes you through the process of creating a doubly linked list from scratch, explaining each step in detail. Learn how to define the necessary classes, understand the significance of nodes, and implement essential methods for insertion, deletion, and traversal.
  • 1.4k
  • 0
  • 0

Introduction:

Doubly linked lists are fundamental data structures used to store and manipulate collections of elements efficiently. In this blog post, we will delve into the implementation of a doubly linked list in C#. We will walk through the process of creating the necessary classes and methods, explain their functionality, and provide a complete C# program to demonstrate the doubly linked list in action. By the end of this article, you will have a thorough understanding of doubly linked lists and be able to apply them to your own projects.

Program:

Let's start by defining the Node class, which will represent the individual elements of our doubly linked list. Each node will contain data, as well as references to the previous and next nodes.

public class Node<T>
{
    public T Data { get; set; }
    public Node<T> Previous { get; set; }
    public Node<T> Next { get; set; }
}

Next, we'll create the DoublyLinkedList class, which will serve as the container for our nodes. It will include methods for adding and removing elements, as well as for traversing the list forwards and backwards.

public class DoublyLinkedList<T>
{
    private Node<T> head;
    private Node<T> tail;

    public void AddFirst(T data)
    {
        Node<T> newNode = new Node<T> { Data = data };

        if (head == null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            newNode.Next = head;
            head.Previous = newNode;
            head = newNode;
        }
    }

    public void AddLast(T data)
    {
        Node<T> newNode = new Node<T> { Data = data };

        if (head == null)
        {
            head = newNode;
            tail = newNode;
        }
        else
        {
            tail.Next = newNode;
            newNode.Previous = tail;
            tail = newNode;
        }
    }

    // Other methods (e.g., RemoveFirst, RemoveLast, TraverseForward, TraverseBackward) can be implemented similarly.
}

Now, let's demonstrate the usage of our DoublyLinkedList class with a simple program:

class Program
{
    static void Main(string[] args)
    {
        DoublyLinkedList<int> list = new DoublyLinkedList<int>();

        list.AddFirst(3);
        list.AddFirst(2);
        list.AddFirst(1);

        list.AddLast(4);
        list.AddLast(5);
        list.AddLast(6);

        list.TraverseForward();
        list.TraverseBackward();
    }
}

Output: 

Traversing forward: 1 2 3 4 5 6
Traversing backward: 6 5 4 3 2 1

In the given program, we create an instance of the DoublyLinkedList class and add elements to it using the AddFirst and AddLast methods. We then traverse the list forwards and backward using the TraverseForward and TraverseBackward methods, respectively. The output demonstrates that the doubly linked list correctly maintains the order of the elements and allows efficient traversal in both directions.

Conclusion:

In this blog post, we explored the implementation of a doubly linked list in C#. We discussed the Node class, which represents the individual elements, and the DoublyLinkedList class, which serves as the container. We provided a complete C# program that demonstrated the functionality of the doubly linked list, along with its output. By understanding the concepts and code presented here, you are now equipped to utilize doubly linked lists effectively in your own C# projects.

Comments (0)

There are no comments. Be the first to comment!!!