Single Circular Linked List

A single circular linked list is a list in which each node is linked with others. A node is a structure element containing data and link field.it’s same as a single Linked list but only the difference is in the single linked list the last node next node is always NULL whereas in a singly circular linked list the last node to next always points to head.

Single Circular Linked List Representation

Declaring a Single Linked list

In C language, a linked list can be implemented using structure and pointers.

struct sclink
{
  int data;
  struct sclink*next;
};
typedef struct sclink LINK;
LINK*head=NULL;

The above definition is used to create every node in the list. The data field stores the element and the next is a pointer to store the address of the next node.

In place of a data type, struct LinkedList is written before next. That’s because of its a self-referencing pointer. It means a pointer that points to whatever it is a part of. Here next is a part of a node and it will point to the next node.typedef is used to define a data type in C.

Creating a node (new Link)

void createlink()
{
  char ch;
  int value;
  LINK*th1,*th2;
  if(head==NULL)
  {
   head=(LINK*)malloc(sizeof(LINK));
         printf("\nEnter data: ");
         scanf("%d",&value);
   head->data=value;
   head->next=head;
   printf("\nDo You want to continue Y?: ");
   fflush(stdin);
   ch=getchar();
   if(ch!='Y'||ch!='y')
    return;
  }
  th1=head;
  while(th1->next!=head)
  th1=th1->next;

  do
  {
    th2=(LINK*)malloc(sizeof(LINK));
          printf("\nEnter data: ");
          scanf("%d",&value);
    th2->data=value;
    th2->next=head;
    th1->next=th2;
    th1=th1->next;   //th1=th2;
    printf("\nDo You want to continue Y?: ");
    fflush(stdin);
    ch=getchar();
  }while(ch=='Y'||ch=='y');
  th1=th2=NULL;
  return;
}

Here if HEAD is NULL then always it creates a node at head position. Otherwise, the new node will always be added after the last node. Just keep in mind that when we are creating a new node in single circular linked list next node address always should be head.

Single Circluar Lined List traversal

void displaylink()
{
  LINK*th;
  if(head==NULL)
  {
     printf("\nLIST IS EMPTY");  
     return;
  }
  th=head;
  do
  {
     printf("%d ",th->data);
     th=th->next;
  }while(th!=head);
  return;  
}

Here is HEAD is NULL it indicates list is empty else travel from beginning to end of the list and display all the data until the head is accrued.

Finding the Number of Nodes

int linkcount()
{
   LINK *th;
   int count=0;
   if(head==NULL)
     return 0; 

   th=head;
   do{
      ++count;
      th=th->next;
   }while(th!=head);

   return count;
}

Single Circular Lined List Insert Operation

Insertion can be performed on any position, the following code can insert a new node at all possible positions 

void insertlink()
{
   LINK*th1,*th2;
   int value;
   int lp,nl,i;
   if(head==NULL)
   {
      head=(LINK*)malloc(sizeof(LINK));
      printf("\nEnter Data: ");
      scanf("%d",&value);
      head->data=value;;
      head->next=head;
      return;
   }
   nl=linkcount();
   printf("\nEnter Link position: ");
   scanf("%d",&lp);
   if(lp<1||lp>nl+1)
   {
     printf("\ninvalid position");
     getch();
     return;
   }
   if(lp==1)
   {
      th1=head;
      for(i=1; i<nl; i++)
        th1=th1->next;

      th2=(LINK*)malloc(sizeof(LINK));
      printf("\nEnter Data: ");
      scanf("%d",&value);
      th2->data=value;
      th2->next=head;
      head=th2;
      th1->next=th2;  //th1->next=head;
      return; 
   } 
   th1=head;
   for(i=1; i<lp-1; i++)
     th1=th1->next;
   
   th2=(LINK*)malloc(sizeof(LINK));
   printf("\nEnter Data: ");
   scanf("%d",&value);
   th2->data=value;
   th2->next=th1->next;
   th1->next=th2;
   return;
}

Single Lined List Deletion Operation

void deletelink()
{
  LINK*th1,*th2;
  int i,nl,lp;
  if(head==NULL)
  {
    printf("\nLIST IS EMPTY");
    return;
  }
  printf("\nEnter Link position: ");
  scanf("%d",&lp);
  nl=linkcount();
  if(lp<1||lp>nl)
  {
    printf("\ninvalid position");
    return;
  }
  if(nl==1)
  {
     free(head);
     head=NULL;
     return;
  }
  if(lp==1)
  {
     th1=head;
     for(i=1; i<nl; i++)
      th1=th1->next;

     th2=head;
     head=th2->next;  //head=head->next;
     th2->next=NULL;
     th1->next=head;
     free(th2);
     return;
  }
  th1=head;
  for(i=1; i<lp-1; i++)
     th1=th1->next;

  th2=th1->next;
  th1->next=th2->next;
  th2->next=NULL;
  free(th2);
  th1=th2=NULL;
  return;
}

Leave a Reply

Your email address will not be published. Required fields are marked *