Deletion a node from Circular Linked List

Suppose we are inserted the following (1,2,3,4,5,6) node in a sequence.

Here given code implementation process.

``````//C Program
//delete a given node in Circular Linked List Nodes
#include<stdio.h>
#include <stdlib.h> //for malloc function

//Create structure
struct Node{
int data;
struct Node*next;
};
//function prototype
void insert(struct Node**,int);
void display(struct Node*);
void delete_node(struct Node**,int);
//insert Node element at end of linked list
//Create dynamic node
struct Node*node=(struct Node*)malloc(sizeof(struct Node));
if(node==NULL){
printf("Memory overflow\n");
}else{
node->data=value;
}else{
temp=temp->next;
}
temp->next=node;
}
}
}
//Display element of Node

}
else{
while(temp){
printf("  %d",temp->data);
temp=temp->next;
break; //terminate loop
}
}

}
}
//delete a given node from circular linked list
}
else{

if(temp->data==node_value){
if(temp->next==temp){
//only one element of circular list
free(temp);
temp=NULL;
}else{

//find last node
temp=temp->next;
}
free(prev); //remove first node
prev=NULL;

}
}
else{
//find deleted node
if(temp->data==node_value){
//if deleted node found
prev->next=temp->next;
free(temp);
temp=NULL;
prev=NULL;
break;
}
prev=temp;
temp=temp->next;
}
if(temp!=NULL){
}
}
}
}
int main(){

//create node pointer
int node_data=4;

printf("\n Before delete node : %d",node_data);
//display all node

printf("\n After delete  node : %d",node_data);
//display all node
}
``````

Output

`````` Before delete node : 4
Linked List Element :  1  2  3  4  5  6
After delete  node : 4
Linked List Element :  1  2  3  5  6``````
``````//C++ Program
//delete a given node in Circular Linked List Nodes
#include<iostream>
using namespace std;

//create structure
struct Node{
int data;
struct Node*next;
};
class CircularList{
public:
CircularList();
void insert(int);
void display();
void delete_node(int);
};
CircularList::CircularList(){
}

//insert Node at end of linked list
void CircularList:: insert(int value){
//Create dynamic node
struct Node*node=new Node;
if(node==NULL){
cout<<"Memory overflow"<<endl;
}else{
node->data=value;
}else{
//find last node
temp=temp->next;
}
temp->next=node;
}
}
}
//display element of Node
void CircularList:: display(){

}
else{
while(temp){
cout<<"  "<<temp->data;
temp=temp->next;
break; //terminate loop
}
}

}
}
//delete a given node from circular linked list
void  CircularList:: delete_node(int node_value){
}
else{

if(temp->data==node_value){
if(temp->next==temp){
//only one element of circular list
delete temp;
temp=NULL;
}else{

//find last node
temp=temp->next;
}
delete prev; //remove first node
prev=NULL;

}
}
else{
//find deleted node
if(temp->data==node_value){
//if deleted node found
prev->next=temp->next;
delete temp;
temp=NULL;
prev=NULL;
break;
}
prev=temp;
temp=temp->next;
}
if(prev!=NULL){
}
}
}
}
int main(){
CircularList obj=CircularList();
int node_value=4;
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
cout<<"Before delete node :"<<node_value<<endl;
//display all node
obj.display();
cout<<"\nAfter delete node :"<<node_value<<endl;
obj.delete_node(node_value);
obj.display();
}``````

Output

``````Before delete node :4
Element of linked list :  1  2  3  4  5  6
After delete node :4
Element of linked list :  1  2  3  5  6n``````
``````//Java Program to
//Delete a given node in Circular Linked List Nodes
static class Node{
int data;
Node next;
}
//Class constructors
}
//insert node at last of linke list
void insert(int value){
//Create a node
Node node=new Node();
node.data=value;
}
else{
//find lase node
temp=temp.next;
}
temp.next=node;
}

}
//Display node element of circular linked list
public void display(){
}else{
while(temp!=null){
System.out.print("  "+temp.data);
temp=temp.next;
}
}
}
//delete a given node from circular linked list
public void deleteNode(int node_value){
}
else{

if(temp.data==node_value){
if(temp.next==temp){
//only one element of circular list
temp=null;
}else{

//find last node
temp=temp.next;
}
prev=null;

}
}
else{
//find deleted node
if(temp.data==node_value){
//if deleted node found
prev.next=temp.next;
temp=null;
prev=null;
break;
}
prev=temp;
temp=temp.next;
}
if(prev!=null){
}
}
}
}

public static void main(String[] args) {
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
System.out.println("Before Delete");
obj.display();
System.out.println("\nAfter Delete");
obj.deleteNode(4);
obj.display();

}
}``````

Output

``````Before Delete
Circular Linked List Element :  1  2  3  4  5  6
After Delete
Circular Linked List Element :  1  2  3  5  6``````
``````#Python Program
#delete a given node in Circular Linked List Nodes
class Node:
def __init__(self,data):
self.data=data
self.next=None

#create class CircularList
class CircularList:
def __init__(self):
#Assign default value

#Insert new node to End of Linked list
def insert(self,data):
node=Node(data)
#when no element of linked list
node.next=node
else:
temp=temp.next
temp.next=node
#display all element of circular linked list
def display(self):
return

while(temp!=None):
print(temp.data,end=" ")
temp=temp.next
#when find first node
break

print("\n")

#delete a given node
def deleteNode(self,node_value):
else:
prev=None

if(temp.data==node_value):
if(temp.next==temp):
#only one element of circular list
temp=None

else:
#find last node
temp=temp.next

prev=None

else:
#find deleted node
if(temp.data==node_value):
#if deleted node found
prev.next=temp.next
temp=None
prev=None
break

prev=temp
temp=temp.next

if(prev!=None):

def main():
#Create Object of class CircularList
obj=CircularList()
node_value=4
#Insert element
obj.insert(1)
obj.insert(2)
obj.insert(3)
obj.insert(4)
obj.insert(5)
obj.insert(6)
print("Before Delete node ",node_value)
obj.display()
print("After Delete node ",node_value)
obj.deleteNode(node_value)
obj.display()
if __name__ == '__main__':
main()``````

Output

``````Before Delete node  4
1 2 3 4 5 6

After Delete node  4
1 2 3 5 6

``````
``````//C# program
//delete a given node in Circular Linked List Nodes
using System;
//node class
public class Node{
public  int data;
public  Node next;
}
class Program
{
public Program(){
}
public void insert(int data){
Node newNode=new Node();
newNode.data=data;
//when no element of linked list
}
else{
//get last node
temp=temp.next;
}
temp.next=newNode;
}
}
public void display(){
}else{
Console.Write(" Circular Linked List : ");
while(temp!=null){
Console.Write(" {0}",temp.data);
temp=temp.next;
//End of Loop iteration
}
}
}
//delete a given node from circular linked list
public void deleteNode(int node_value){
}
else{

if(temp.data==node_value){
if(temp.next==temp){
//only one element of circular list
temp=null;
}else{

//find last node
temp=temp.next;
}
prev=null;

}
}
else{
//find deleted node
if(temp.data==node_value){
//if deleted node found
prev.next=temp.next;
temp=null;
prev=null;
break;
}
prev=temp;
temp=temp.next;
}
if(prev!=null){
}
}
}
}

static void Main(){

Program obj=new Program();
int remove_node=4;
obj.insert(1);
obj.insert(2);
obj.insert(3);
obj.insert(4);
obj.insert(5);
obj.insert(6);
Console.Write("Before Delete {0}",remove_node);
obj.display();
Console.Write("\nAfter Delete {0}",remove_node);
obj.deleteNode(remove_node);
obj.display();
}
}``````

Output

``````Before Delete 4 Circular Linked List :  1 2 3 4 5 6
After Delete 4 Circular Linked List :  1 2 3 5 6``````

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.