Skip to main content

Delete a node from the end of doubly linked list in c#

Csharp program for Delete a node from the end of doubly linked list. Here problem description and explanation.

// Include namespace system
using System;
// Csharp program for
// Delete a node from the end of doubly linked list
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode prev;
	public LinkNode(int data)
	{
		this.data = data;
		this.next = null;
		this.prev = null;
	}
}
public class DoublyLinkedList
{
	public LinkNode head;
	public LinkNode tail;
	public DoublyLinkedList()
	{
		// Set inital value
		this.head = null;
		this.tail = null;
	}
	// Insert new node at end position
	public void insert(int value)
	{
		// Create a node
		var node = new LinkNode(value);
		if (this.head == null)
		{
			// Add first node
			this.head = node;
			this.tail = node;
			return;
		}
		// Add node at last position
		this.tail.next = node;
		node.prev = this.tail;
		this.tail = node;
	}
	// Display node element of doubly linked list
	public void display()
	{
		if (this.head == null)
		{
			Console.WriteLine("Empty Linked List");
		}
		else
		{
			Console.WriteLine("\nLinked List Head to Tail :");
			// Get first node of linked list
			var temp = this.head;
			// iterate linked list 
			while (temp != null)
			{
				// Display node value
				Console.Write("  " + temp.data);
				// Visit to next node
				temp = temp.next;
			}
		}
	}
	// Remove the given node from end of linked list
	public void deletionFromEnd(int key)
	{
		if (this.head == null)
		{
			// Empty linked list
			return;
		}
		LinkNode auxiliary = null;
		var temp = this.tail;
		// Find the deleted node
		while (temp != null && auxiliary == null)
		{
			if (temp.data == key)
			{
				// We found deleted node
				auxiliary = temp;
			}
			// Visit to previous node
			temp = temp.prev;
		}
		if (auxiliary != null)
		{
			// When we get deleted node
			if (auxiliary.prev != null)
			{
				// When previous node present
				auxiliary.prev.next = auxiliary.next;
			}
			if (auxiliary.next != null)
			{
				// When next node present
				auxiliary.next.prev = auxiliary.prev;
			}
			if (this.head == auxiliary)
			{
				// When delete head node
				this.head = this.head.next;
			}
			if (auxiliary == this.tail)
			{
				// When delete last node
				this.tail = this.tail.prev;
			}
			auxiliary = null;
		}
		else
		{
			Console.WriteLine("\nNode not exist " + key);
		}
	}
	public static void Main(String[] args)
	{
		var dll = new DoublyLinkedList();
		// Insert following linked list nodes
		dll.insert(1);
		dll.insert(2);
		dll.insert(9);
		dll.insert(4);
		dll.insert(5);
		dll.insert(2);
		dll.insert(7);
		dll.insert(9);
		Console.Write("\nBefore Delete");
		dll.display();
		var key = 2;
		dll.deletionFromEnd(key);
		Console.Write("\nAfter Delete Node " + key.ToString());
		dll.display();
	}
}

Output

Before Delete
Linked List Head to Tail :
  1  2  9  4  5  2  7  9
After Delete Node 2
Linked List Head to Tail :
  1  2  9  4  5  7  9




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment