Create a circular linked list and delete any user specified node

Write a C program to create a circular linked list and then delete any user specified node (Using double pointer).

Solution:

Circular linked list:
Circular linked list is a linked list where all nodes are connected to form a circle. A circularly linked list node looks exactly same as a linear singly linked list.

In this example, we create a circular list with the use of double pointer and a function to delete the specified node.

Program

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
    int info;
    struct node *next;
}s;
void insertion(s **,s**,int);
void del(s**,s**,int);
void display(s*,s*);
s* first=NULL;
s* last=NULL;
void main()
{
    int ch,n,t;
    do
    {
         printf("\n1.insert a node\n2.delete a particular node\n3.display\n");
         printf("Enter your choice: ");
         scanf("%d",&ch);
         switch(ch)
         {
              case 1:
              printf("Enter number: ");
              scanf("%d",&n);
              //calling the insertion function
              insertion(&last,&first,n);
              break;

              case 2:
              printf("Enter the info field of node you want to delete: ");
              scanf("%d",&t);
              //calling the delete function
              del(&first,&last,t);
              if(first==NULL)
              {
                  last=first;
              }
              break;

              case 3:
              //call the display function
              display(first,last);
              break;
         }
    }while(ch!=4);
    getch();
}
void insertion(s**last,s**first,int n)
{
     s* p=NULL;
     p=((s*)malloc(sizeof(s)));
     p->info=n;
     if((*last)==NULL)
     {
         *last=p;
         (*first)=(*last);
         p->next=(*first);
     }
     else
     {
         (*last)->next=p;
         p->next=(*first);
         (*last)=(*last)->next;
     }
}
void del(s** first,s** last,int t)
{
    int l=0;
    s * q=(*last),* r=(*first);
    if((*first)==NULL)
    {
         printf("linked list empty\n");
    }
    else
    {
         while(r!=NULL)
         {
              if(r->info==t&&r==(*first))
              {
                   q->next=r->next;
                   (*first)=(*first)->next;
                   free(r);
                   l++;
              }
              else if(r->info==t&&r==(*last))
              {
                   q->next=(*first);
                   (*last)=q;
                   l++;
                   free(r);
              }
              else if(r->info==t && r!=(*first))
              {
                  q->next=r->next;
                  free(r);
                  l++;
                  break;
              }
              q=q->next;
              r=r->next;
          }
     }
     if(l==0)
     {
         printf("\n The element not found in list");
     }
}
void display(s* first,s* last)
{
    s* q=first;
    if(first==NULL)
    {
        printf("\nNo node to print ");
    }
    else
    {
        printf("%d\t",q->info);
        q=q->next;
        while(q!=first)
        {
            printf("%d\t",q->info);
            q=q->next;
        }
    }
}


Output:

circular linked list