Delete from end in Circular linked list using C

Description

This program is used to create Circular Single Linked List and Delete element from the End of the List Using C.

#include
#include

struct node
{
 int data;
 struct node * next;
};
struct node *head=NULL;
void display();
void create();
void delEnd();

void main()
{
  int choice;
  do
  {
   printf("\n__MENU__\n1.Create List\n2.Delete from End\n3.Exit...\n");
   printf("\nENTER your choice\n");
   scanf("%d",&choice);
   switch(choice)
   {
    case 1:create();break;
   case 2:delEnd();break;
    case 3:printf("~~~~~~~THANK YOU~~~~~~~\n");break;
   default:printf("INVALID input !!!");break;   
  }
  }while(choice!=3);
}

void create()
{
 
 int x, i,n;
 head=NULL;
 struct node* newnode,*temp;
 temp=head;
 printf("Enter no. of elements in the LIST \n");
 scanf("%d",&n);
 printf("Enter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  temp=head; 
  newnode=(struct node*)malloc(sizeof(struct node*)); 
  scanf("%d",&newnode->data);
     if(head==NULL)
     {
      head=newnode;
      newnode->next=head;
  }
     else
  {
         while(temp->next!=head)
   temp=temp->next;
   temp->next=newnode;
   newnode->next=head; 
  }
 }
 display();
}

void delEnd()
{
 struct node* temp, *pretemp;
 temp=head;
 if(head==NULL)
  display();
 else if(temp->next==head)
 {
  head=NULL;
  free(temp); 
  display(); 
 }
 else
 {
  while(temp->next!=head)
  {
   pretemp=temp;
   temp=temp->next;  
  }
  pretemp->next=temp->next;
  free(temp);
  display();
 }
}

void display()
{
 struct node* temp;
 temp=head;
 if(head==NULL)
  printf("List is empty !!!");
 else 
 {
  printf("List elements are :\t");
  while(temp->next!=head)
  {
   printf("%d\t",temp->data);
   temp=temp->next;
  }
  printf("%d\n",temp->data);
 }
}

OUTPUT

Delete from End in Circular Linked List Output 1 Delete from End in Circular Linked List Output 2

Leave a comment