Circular link list deletion

Circular link list deletion Program in C. Here we learn circular linked list insertion and deletion Beginning to End program in c. Try this code in your Compiler. Here mind it will run all the codes on Online Compiler. In the editor that you use while running this code, Please suggest us via Comment.

Circular link list Insertion and deletion

#include <stdio.h>
#include <stdlib.h>

struct node
{
  int data;
  struct node *next;
};

struct node *
get_node ()
{
  struct node *New;
  New = (struct node *) malloc (sizeof (struct node));
  New->next = NULL;
  return New;
}

struct node *
create ()
{
  struct node *head, *temp, *New;
  int flag = 1, val;
  char ans;
  do
    {
      printf ("Enter the element : ");
      scanf ("%d", &val);
      New = get_node ();
      New->data = val;
      if (flag == 1)
	{
	  head = New;
	  New->next = head;
	  temp = head;
	  flag = 0;
	}
      else
	{
	  temp->next = New;
	  New->next = head;
	  temp = temp->next;
	}
      printf ("\nYou want to enter more elements (Y/N) : ");
      scanf ("%s", &ans);
    }
  while (ans == 'y' || ans == 'Y');
  return head;
}

void
display (struct node *head)
{
  struct node *temp = head;
  do
    {
      printf ("%d --> ", temp->data);
      temp = temp->next;
    }
  while (temp != head);
}

struct node *
insertHead (struct node *head)
{
  struct node *temp = head, *newHead;
  newHead = get_node ();
  printf ("\nEnter the value\n");
  scanf ("%d", &newHead->data);
  newHead->next = head;
  while (temp->next != head)
    {
      temp = temp->next;
    }
  temp->next = newHead;
  head = NULL;
  return newHead;
}

void
insert_last (struct node *head)
{
  struct node *temp = head, *newNode = get_node ();
  printf ("\nEnter the new value : \n");
  scanf ("%d", &newNode->data);
  while (temp->next != head)
    {
      temp = temp->next;
    }
  temp->next = newNode;
  newNode->next = head;
  newNode = NULL;

}

void
insertAfter (struct node *head)
{
  struct node *temp = head, *newNode = get_node ();
  int key, found = 0;
  printf ("\nEnter the new value : \n");
  scanf ("%d", &newNode->data);
  printf
    ("\nEnter the value of the node after which you want to enter the new node\n");
  scanf ("%d", &key);
  do
    {
      if (temp->data == key)
	{
	  found = 1;
	  break;
	}
      temp = temp->next;
    }
  while (temp != head);
  if (found == 0)
    {
      printf ("\nElement not found\n");
      return;
    }
  newNode->next = temp->next;
  temp->next = newNode;
}

void
delete (struct node **head)
{
// if the node is empty
  if (*head == NULL)
    {
      printf ("\nThe list is already empty!\n");
      return;
    }
  struct node *temp = *head, *prev;
  int val, found = 0;
  printf ("\nEnter the value which is to be deleted\n");
  scanf ("%d", &val);
  do
    {				//Using this we will search the element in the list if it is not present then we will not do other work!
      if (temp->data == val)
	{
	  found = 1;
	  break;
	}
      temp = temp->next;
    }
  while (temp->next != *head);

  if (found == 0)
    {				// we will check here if the element is found or not!
      printf ("\nThe element is not present in the list!\n");
    }
  else
    {
// Check if node is only node
      if (temp->next == *head)
	{
	  *head = NULL;
	  free (temp);
	  return;

	}
// If more than one node, check if
// it is first node
      if (temp == *head)
	{
	  prev = *head;
	  while (prev->next != *head)
	    {
	      prev = prev->next;
	    }
	  *head = temp->next;
	  prev->next = *head;
	  free (temp);
	}
// check if node is last node
      else if (temp->next == *head && temp == *head)
	{
	  prev->next = *head;
	  free (temp);
	}
      else
	{
	  prev->next = temp->next;
	  free (temp);

	}
    }

}

int
main ()
{
  struct node *head = create ();
  printf ("\n");
  display (head);
// head = insertHead(head);
// display(head);
// insert_last(head);
// display(head);
// insertAfter(head);
// display(head);
  delete (&head);
  display (head);
  return 0;
}

Enter the element :

You want to enter more elements (Y/N) : y

Enter the element : 8

You want to enter more elements (Y/N) : y

Enter the element : 89

You want to enter more elements (Y/N) : N

6 –> 8 –> 89 –>

Enter the value which is to be deleted : 8

6 –> 89 –>

Note:- ♥  We input in Orange Color. and get output in  Green color in Circular link list deletion.

Circular link list deletion
Circular link list deletion

Circular link list deletion Here Two Different program in One tab, you need to Understand and Second one is for your understanding and Correct them. Here learn Linked list in CPP

Circular Linked List Insertion

#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *next;
};

struct node* get_node(){
struct node* New;
New = (struct node*)malloc(sizeof(struct node));
New->next = NULL;
return New;
}

struct node* create(){
struct node *head, *temp, *New;
int flag=1, val;
char ans;
do{
printf("Enter the element : ");
scanf("%d", &val);
New = get_node();
New->data = val;
if(flag == 1){
head = New;
New->next = head;
temp = head;
flag = 0;
}
else{
temp->next = New;
New->next = head;
temp = temp->next;
}
printf("\nYou want to enter more elements : ");
scanf("%s", &ans);
}while(ans == 'y' || ans == 'Y');
return head;
}

void display(struct node* head){
struct node* temp = head;
if(head!=NULL){

do{
printf("%d --> ", temp->data);
temp = temp->next;
}while(temp != head);
}
}

struct node* insertHead(struct node *head){
struct node *temp = head, *newHead;
newHead = get_node();
printf("\nEnter the value\n");
scanf("%d", &newHead->data);
newHead->next = head;
while(temp->next != head){
temp = temp->next;
}
temp->next = newHead;
head = NULL;
return newHead;
}

void insert_last(struct node* head){
struct node *temp = head, *newNode = get_node();
printf("\nEnter the new value : \n");
scanf("%d", &newNode->data);
while(temp->next != head){
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
newNode = NULL;

}

void insertAfter(struct node* head){
struct node *temp = head, *newNode = get_node();
int key, found = 0;
printf("\nEnter the new value : \n");
scanf("%d", &newNode->data);
printf("\nEnter the value of the node after which you want to enter the new node\n");
scanf("%d", &key);
do{
if(temp->data == key){
found = 1;
break;
}
temp = temp->next;
}while(temp != head);
if(found == 0){
printf("\nElement not found\n");
return;
}
newNode->next = temp->next;
temp->next = newNode;
}

void delete(struct node *head){
// if the node is empty
if(head == NULL){
printf("\nThe list is already empty!\n");
return;
}
struct node *temp = head , *prev;
int val, found = 0;
printf("\nEnter the value which is to be deleted\n");
scanf("%d", &val);
do
{ //Using this we will search the element in the list if it is not present then we will not do other work!
if(temp->data == val){
found = 1;
break;
}
prev=temp;
temp = temp->next;
}
while(temp->next != head);

if(found == 0){ // we will check here if the element is found or not!
printf("\nThe element is not present in the list!\n");
}
else
{
// Check if node is only node
if(temp->next == head)
{
head = NULL;
free(temp);
return;

}
// If more than one node, check if
// it is first node
if(temp == head)
{
prev=head;
while(prev->next != head){
prev = prev->next;
}
head = temp->next;
prev->next=head;
free(temp);
}
// check if node is last node
else if(temp->next==head && temp==head)
{
prev->next = head;
free(temp);
}
else{
prev->next=temp->next;
free(temp);

}
}

}

int main(){
struct node* head = create();
printf("\n");
display(head);
// head = insertHead(head);
// display(head);
// insert_last(head);
// display(head);
// insertAfter(head);
// display(head);
delete(head);
display(head);
return 0;
}

Enter the element : 7

You want to enter more elements : y

Enter the element : 890

You want to enter more elements : n

7 –> 890 –>

Enter the value which is to be deleted: 8

The element is not present in the list!

7 –> 890 –>

Circular link list deletion Program in C

#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node *next;
};

struct node* get_node(){
struct node* New;
New = (struct node*)malloc(sizeof(struct node));
New->next = NULL;
return New;
}

struct node* create(){
struct node *head, *temp, *New;
int flag=1, val;
char ans;
do{
printf("Enter the element : ");
scanf("%d", &val);
New = get_node();
New->data = val;
if(flag == 1){
head = New;
New->next = head;
temp = head;
flag = 0;
}
else{
temp->next = New;
New->next = head;
temp = temp->next;
}
printf("\nYou want to enter more elements : ");
scanf("%s", &ans);
}while(ans == 'y' || ans == 'Y');
return head;
}

void display(struct node* head){
struct node* temp = head;
do{
printf("%d --> ", temp->data);
temp = temp->next;
}while(temp != head);
}

struct node* insertHead(struct node *head){
struct node *temp = head, *newHead;
newHead = get_node();
printf("\nEnter the value\n");
scanf("%d", &newHead->data);
newHead->next = head;
while(temp->next != head){
temp = temp->next;
}
temp->next = newHead;
head = NULL;
return newHead;
}

void insert_last(struct node* head){
struct node *temp = head, *newNode = get_node();
printf("\nEnter the new value : \n");
scanf("%d", &newNode->data);
while(temp->next != head){
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
newNode = NULL;

}

void insertAfter(struct node* head){
struct node *temp = head, *newNode = get_node();
int key, found = 0;
printf("\nEnter the new value : \n");
scanf("%d", &newNode->data);
printf("\nEnter the value of the node after which you want to enter the new node\n");
scanf("%d", &key);
do{
if(temp->data == key){
found = 1;
break;
}
temp = temp->next;
}while(temp != head);
if(found == 0){
printf("\nElement not found\n");
return;
}
newNode->next = temp->next;
temp->next = newNode;
}

void delete(struct node **head){
// if the node is empty
if(*head == NULL){
printf("\nThe list is already empty!\n");
return;
}
struct node *temp = *head , *prev;
int val, found = 0;
printf("\nEnter the value which is to be deleted\n");
scanf("%d", &val);
do
{ //Using this we will search the element in the list if it is not present then we will not do other work!
if(temp->data == val){
found = 1;
break;
}
temp = temp->next;
}
while(temp->next != *head);

if(found == 0){ // we will check here if the element is found or not!
printf("\nThe element is not present in the list!\n");
}
else
{
// Check if node is only node
if(temp->next == *head)
{
*head = NULL;
free(temp);
return;

}
// If more than one node, check if
// it is first node
if(temp == *head)
{
prev=*head;
while(prev->next != *head){
prev = prev->next;
}
*head = temp->next;
prev->next=*head;
free(temp);
}
// check if node is last node
else if(temp->next==*head && temp==*head)
{
prev->next = *head;
free(temp);
}
else{
prev->next=temp->next;
free(temp);

}
}

}

int main(){
struct node* head = create();
printf("\n");
display(head);
// head = insertHead(head);
// display(head);
// insert_last(head);
// display(head);
// insertAfter(head);
// display(head);
delete(&head);
display(head);
return 0;
}

Enter the element : 76

You want to enter more elements : y

Enter the element : 79

You want to enter more elements : n

76 –> 79 –>

Enter the value which is to be deleted : 76

79 –>

Thanks

Leave a Reply

x