Delete element from beginning in Circular Linked List in C

Description

This program is used to create Circular Linked List and delete element from the beginning of the List Using C

#include
#include

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

void main()
{
  int choice;
  do
  {
   printf("\n__MENU__\n1.Create List\n2.Delete from beginning\n3.Exit...\n");
   printf("\nENTER your choice\n");
   scanf("%d",&choice);
   switch(choice)
   {
    case 1:create();break;
   case 2:delBeg();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 delBeg()
{
 struct node* temp, *del;
 temp=head;
 del=head;
 if(head==NULL)
  display();
 else if(temp->next==head)
 {
  head=NULL;
  free(temp);
  display();
 }
 else
 {
  while(temp->next!=head)
   temp=temp->next;
  temp->next=head->next;
  head=head->next;
  free(del);
  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 Beginning Circular linked Output 1 Delete from Beginning Circular linked Output 2

Leave a comment