Implementation of Circular Linked List in C

Circular Linked List

Before going towards the circular linked list, I hope that you have the knowledge about the Doubly Linked List. If not, then click here.

Circular Linked List is little more complicated linked data structure. In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements points to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required.




Basic Operations:

Following are the important operations supported by a circular list.
  • insert − Inserts an element in the list.
  • delete − Deletes an element from the list.
  • display − Displays the list.
Let us check out he code :)


#include <stdio.h> 
#include <conio.h>
typedef struct circular
{
    int data;
    struct circular *next;
}circular;
circular *temp, *head,*new1, *temp1;
int cnt=0,x;
void create();
void insert();
void display();
void del();
void main()
{
 int ch=0;
 clrscr();
 while(1)
 {
  printf("\n1. Create");
  printf("\n2. Insert");
  printf("\n3. Delete");
  printf("\n4. Display");
  printf("\n5. Exit");
  printf("\nEnter your choice: ");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    create();
    break;
   case 2:
    insert();
    break;
   case 3:
    del();
    break;
   case 4:
    display();
    break;
   case 5:
    exit(0);
   default:
    printf("Enter the correct choice.");
  }
 }
}

void create()
{

 new1=(struct circular *)malloc(sizeof(struct circular));
 if(new1==NULL)
 {
  printf("\nOut of Memory Space:\n");
  exit(0);
 }
 printf("\nEnter the data value for the node:\t");
 scanf("%d",&new1->i);
 new1->next=NULL;
 if(head==NULL)
 {
  head=new1;
 }
 else
 {
  temp=head;
  while(temp->next!=NULL)
  {
   temp=temp->next;
  }
  temp->next=new1;
  new1->next= head;
 }
}
void insert()
{
 new1=(struct circular *)malloc(sizeof(struct circular));
 if(new1==NULL)
 {
  printf("\nOut of Memory Space:\n");
  return;
 }
 printf("\nEnter the data value for the node:\t" );
 scanf("%d",&new1->i);
 new1->next =NULL;
 printf("After which node you want to insert new node: \n");
 scanf("%d",&x);
 temp=head;
 while(temp->i!=x)
 {
  temp=temp->next;
  if(temp->next==head)
  {
   printf("Node not found in list.");
   return;
  }
 }
 new1->next=temp->next;
 temp->next=new1;
 display();
}
void display()
{
    temp=head;
    printf("%d-->",temp->i);
    temp=temp->next;
    while(temp!=head)
    {
 printf("%d-->",temp->i);
 temp=temp->next;
    }
}
void del(void)
{
 if(head==NULL)
 {
  printf("List is empty.");
  exit(0);
 }
 else
 {
  temp=head;
  printf("Enter the value of node to be deleted: ");
  scanf("%d",&x);
  while(temp->i!=x)
  {
   temp1=temp;
   temp=temp->next;
   if(temp->next==head)
   {
    printf("Node not found in the list.");
    return;
   }
  }
  temp1->next=temp->next;
  printf("Your deleted element: %d\n",temp->i);
  display();
 }
}

























Happy Coding :)

Comments

Popular posts from this blog

MultiSelection of Item in Recycler View

Upload Image From Android To Php Server

Merge Sort