using System;
public class Node
// To store the value or data
public int Data;
// Pointer to the next node
public Node Next;
// Pointer to the previous node
public Node Prev;
// Constructor
public Node(int d)
Data = d;
Prev = Next = null;
public class DoublyLinkedList
// Head of the list
public Node Head { get; private set; }
// Tail of the list
public Node Tail { get; private set; }
// Length of the list
public int Length { get; private set; }
// Constructor
public DoublyLinkedList()
Head = Tail = null;
Length = 0;
// 1. Traversal in Doubly Linked List
public void Traverse()
Console.WriteLine("Doubly Linked List Elements:");
Node current = Head;
while (current != null)
Console.Write(current.Data + " <-> ");
current = current.Next;
}
Console.WriteLine("null");
// 2. Finding Length of Doubly Linked List
public int GetLength()
return Length;
// Alternative implementation if we didn't maintain length:
// int count = 0;
// Node current = Head;
// while (current != null)
// {
// count++;
// current = current.Next;
// }
// return count;
// 3. Insertion in a Doubly Linked List
// Insert at the beginning
public void InsertAtBeginning(int data)
Node newNode = new Node(data);
if (Head == null)
Head = Tail = newNode;
else
newNode.Next = Head;
Head.Prev = newNode;
Head = newNode;
Length++;
// Insert at the end
public void InsertAtEnd(int data)
Node newNode = new Node(data);
if (Tail == null)
Head = Tail = newNode;
else
{
Tail.Next = newNode;
newNode.Prev = Tail;
Tail = newNode;
Length++;
// Insert at a specific position
public void InsertAtPosition(int data, int position)
if (position < 0 || position > Length)
Console.WriteLine("Invalid position");
return;
if (position == 0)
InsertAtBeginning(data);
return;
if (position == Length)
InsertAtEnd(data);
return;
Node newNode = new Node(data);
Node current = Head;
for (int i = 0; i < position - 1; i++)
current = current.Next;
newNode.Next = current.Next;
newNode.Prev = current;
current.Next.Prev = newNode;
current.Next = newNode;
Length++;
// 4. Deletion in a Doubly Linked List
// Delete from the beginning
public void DeleteFromBeginning()
if (Head == null)
{
Console.WriteLine("List is empty");
return;
if (Head == Tail)
Head = Tail = null;
else
Head = Head.Next;
Head.Prev = null;
Length--;
// Delete from the end
public void DeleteFromEnd()
if (Tail == null)
Console.WriteLine("List is empty");
return;
}
if (Head == Tail)
Head = Tail = null;
else
Tail = Tail.Prev;
Tail.Next = null;
Length--;
// Delete from a specific position
public void DeleteFromPosition(int position)
if (position < 0 || position >= Length)
Console.WriteLine("Invalid position");
return;
if (position == 0)
DeleteFromBeginning();
return;
if (position == Length - 1)
DeleteFromEnd();
return;
Node current = Head;
for (int i = 0; i < position; i++)
current = current.Next;
current.Prev.Next = current.Next;
current.Next.Prev = current.Prev;
Length--;
// Delete a specific node by value (first occurrence)
public void DeleteNode(int data)
if (Head == null)
{
Console.WriteLine("List is empty");
return;
if (Head.Data == data)
DeleteFromBeginning();
return;
Node current = Head;
while (current != null && current.Data != data)
current = current.Next;
if (current == null)
Console.WriteLine("Node not found");
return;
if (current == Tail)
{
DeleteFromEnd();
return;
current.Prev.Next = current.Next;
current.Next.Prev = current.Prev;
Length--;
// Example usage
public class Program
public static void Main()
DoublyLinkedList dll = new DoublyLinkedList();
// Insertion
dll.InsertAtEnd(10);
dll.InsertAtBeginning(5);
dll.InsertAtEnd(20);
dll.InsertAtPosition(15, 2);
// Traversal
dll.Traverse();
Console.WriteLine("Length: " + dll.GetLength());
// Deletion
dll.DeleteFromBeginning();
dll.DeleteFromEnd();
dll.DeleteFromPosition(1);
dll.DeleteNode(15);
// Traversal after deletions
dll.Traverse();
Console.WriteLine("Length: " + dll.GetLength());