Skip to main content

reverse a doubly circular linked list by swapping node value in c#

Csharp program for reverse a doubly circular linked list by swapping node value. Here more information.

// Include namespace system
using System;
/*
  Csharp Program
  Reverse a doubly circular linked list by swapping value
*/
// Linked list node
public class LinkNode
{
	public int data;
	public LinkNode next;
	public LinkNode back;
	public LinkNode(int data, LinkNode back, LinkNode next)
	{
		this.data = data;
		this.back = back;
		this.next = next;
	}
}
public class CircularDoublyLL
{
	public LinkNode front;
	public LinkNode tail;
	public CircularDoublyLL()
	{
		this.front = null;
		this.tail = null;
	}
	// Add new node at the end of linked list
	public void addNode(int data)
	{
		var node = new LinkNode(data, null, null);
		// When new node is successfully created
		if (this.front == null)
		{
			// Add first node of linked list
			this.front = node;
			this.tail = node;
		}
		else
		{
			// Add node at last position
			this.tail.next = node;
			node.back = this.tail;
			this.tail = node;
		}
		// Connect the last node in the linked list to front
		this.tail.next = this.front;
		this.front.back = this.tail;
		// Set node data
		node.data = data;
	}
	// Print linked list nodes
	public void printElement()
	{
		if (this.front == null)
		{
			Console.Write("\n Empty Linked List \n");
		}
		else
		{
			var temp = this.front;
			Console.Write("\n Linked List from Front to Tail \n");
			// Display doubly linked list from start to last node
			while (temp != null)
			{
				Console.Write("  " + temp.data.ToString());
				if (temp == this.tail)
				{
					// When get last node
					temp = null;
				}
				else
				{
					temp = temp.next;
				}
			}
			temp = this.tail;
			Console.Write("\n Linked List from Tail to Front \n");
			// Display doubly linked list from last to start node
			while (temp != null)
			{
				Console.Write("  " + temp.data.ToString());
				if (temp == this.front)
				{
					// When get first node
					temp = null;
				}
				else
				{
					temp = temp.back;
				}
			}
		}
	}
	// Reverses the elements of doubly linked list
	// using swap node values
	public void reverseElement()
	{
		// Get first node
		var first = this.front;
		// Get last node
		var last = this.tail;
		var temp = 0;
		// Reverse operation
		while (first != null && last != null)
		{
			if (first != last)
			{
				// Swap the node value
				temp = first.data;
				first.data = last.data;
				last.data = temp;
				if (first.next == last)
				{
					// Stop reverse operation, or use break
					first = null;
					last = null;
				}
				else
				{
					first = first.next;
					last = last.back;
				}
			}
			else
			{
				// Stop reverse operation, or use break
				first = null;
				last = null;
			}
		}
	}
	public static void Main(String[] args)
	{
		var cll = new CircularDoublyLL();
		// Add linked list node
		cll.addNode(1);
		cll.addNode(2);
		cll.addNode(3);
		cll.addNode(4);
		cll.addNode(5);
		cll.addNode(6);
		Console.Write("\n Before reverse linked list");
		cll.printElement();
		cll.reverseElement();
		Console.Write("\n\n After reverse linked list");
		cll.printElement();
	}
}

Output

 Before reverse linked list
 Linked List from Front to Tail
  1  2  3  4  5  6
 Linked List from Tail to Front
  6  5  4  3  2  1

 After reverse linked list
 Linked List from Front to Tail
  6  5  4  3  2  1
 Linked List from Tail to Front
  1  2  3  4  5  6




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