Delete all Even nodes of a circular linked list in c#
Csharp program for Delete all Even nodes of a circular linked list. Here problem description and explanation.
// Include namespace system
using System;
// Csharp program for
// Delete all even key nodes from circular linked list
// Define class of linked list Node
public class LinkNode
{
public int data;
public LinkNode next;
public LinkNode(int data, LinkNode first)
{
this.data = data;
this.next = first;
}
}
public class CircularLinkedList
{
public LinkNode head;
// Class constructor
public CircularLinkedList()
{
this.head = null;
}
// Insert node at end of circular linked list
public void insert(int value)
{
// Create a node
var node = new LinkNode(value, this.head);
if (this.head == null)
{
// First node of linked list
this.head = node;
node.next = this.head;
}
else
{
var temp = this.head;
// Find last node
while (temp.next != this.head)
{
// Visit to next node
temp = temp.next;
}
// Add new node at the last
temp.next = node;
}
}
// Display node element of circular linked list
public void display()
{
if (this.head == null)
{
Console.WriteLine("Empty Linked List");
}
else
{
Console.Write("Linked List Element :");
var temp = this.head;
// iterate linked list
while (temp != null)
{
// Display node
Console.Write(" " + temp.data.ToString());
// Visit to next node
temp = temp.next;
if (temp == this.head)
{
// Stop iteration
return;
}
}
}
}
public void deleteEven()
{
if (this.head == null)
{
// When empty linked list
return;
}
// Define some auxiliary variables
// Get second node when exist
var temp = this.head.next;
var auxiliary = this.head;
LinkNode hold = null;
// Detect and remove even nodes from second node to
// last node in given linked list
// assume linked list is circular
while (temp != this.head)
{
// Get current node
hold = temp;
// visit to next node
temp = temp.next;
if (hold.data % 2 == 0)
{
// When even data of current node
auxiliary.next = temp;
}
else
{
// Get current node
auxiliary = hold;
}
}
if (this.head.data % 2 == 0)
{
temp = this.head;
// When need to remove head node
if (this.head.next == this.head)
{
// When only one node exists in this linked list
// Set empty linked list
this.head = null;
}
else
{
// Set new head
this.head = this.head.next;
// Auxiliary is last odd node which
// Is get by above loop
auxiliary.next = this.head;
}
}
}
public static void Main(String[] args)
{
var cll = new CircularLinkedList();
// Linked list
// 12 → 21 → 30 → 41 → 53 → 60 → 79 → 88 → To head
cll.insert(12);
cll.insert(21);
cll.insert(30);
cll.insert(41);
cll.insert(53);
cll.insert(60);
cll.insert(79);
cll.insert(88);
Console.WriteLine(" Before delete ");
cll.display();
cll.deleteEven();
Console.WriteLine("\n After delete even nodes");
cll.display();
}
}
Output
Before delete
Linked List Element : 12 21 30 41 53 60 79 88
After delete even nodes
Linked List Element : 21 41 53 79
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