Code for Circular Linked List c-programing




#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *next;
};
struct node *head;

void InsertAtBeginning(int x)
{
 struct node *temp = (struct node *)malloc(sizeof(struct node));
 struct node *temp2 = head;

 temp->data = x;
 temp->next = NULL;

 if(temp==NULL)
 {
  printf("\n MEMORY UNAVAILABLE");
 }
 if(head==NULL)
 {
  head = temp;
  temp->next = head;
 }
 if(temp2->next!=head)
 {
  temp2 = temp2->next;
 }
 temp->next = head;
 head = temp;
 temp2->next = head;
}

void InsertAtEnd(int y)
{
 struct node *temp = (struct node *)malloc(sizeof(struct node));
 struct node *temp2;

 temp->data = y;
 temp->next = NULL;

 if(temp==NULL)
 {
  printf("\n MEMORY UNAVAILABLE");
 }
 if(head==NULL)
 {
  head = temp;
  temp->next = head;
 }
 while(temp2->next!=head)
 {
  temp2 = temp2->next;
 }
 temp2->next = temp;
 temp->next = head;
}

void InsertAtN(int z,int index)
{
 struct node *newnode,*temp;
 int i;

 if(head==NULL && index==1)
 {
  InsertAtBeginning(z);
 }
 else if(index==1)
 {
  InsertAtBeginning(z);
 }
 else
 {
  newnode = (struct node *)malloc(sizeof(struct node));

  printf("\n ENTER DATA VALUE AND INDEX :- ");
  scanf("%d %d",&z,&index);

  newnode->data = z;
  newnode->next = NULL;
  temp = head;

  for(i=0 ; i<(index-2) ; i++)
  {
   temp = temp->next;
  }
  newnode->next = temp->next;
  temp->next = newnode;
 }
}

void DeleteFromBeginning()
{
 struct node *temp = head;
 struct node *temp2 = head;

 if(head==NULL)
 {
  printf("\n LIST IS EMPTY");
 }

 if(temp->next==head)
 {
  head = NULL;
 }

 else
 {
  while(temp->next!=head)
  {
   temp = temp->next;
  }
  head = temp2->next;
  temp->next = head;
  printf("\n THE DELETED NODE IS :- %d",temp2->data);
  free(temp2);
 }
}

void DeleteFromEnd()
{
 struct node *temp = head;
 struct node *prev;

 if(head==NULL)
 {
  printf("\n LIST IS EMPTY");
 }
 if(temp->next==head)
 {
  head = NULL;
 }
 else
 {
  while(temp->next!=head)
  {
   prev = temp;
   temp = temp->next;
  }
  prev->next = head;
  printf("THE DELETED NODE IS :- %d",temp->data);
 }
}

void DeleteFromN(int indexx)
{
 struct node *prev,*temp = head;
 int i;

 printf("\n ENTER INDEX VALUE :- ");
 scanf("%d",&indexx);

 if(head==NULL)
 {
  printf("\n LIST IS EMPTY");
 }
 else if(indexx==1)
 {
  DeleteFromBeginning();
 }
 else
 {
  for(i=0 ; i<(indexx-1) ; i++)
  {
   prev = temp;
   temp = temp->next;
  }
  prev->next = temp->next;
  printf("\n THE DELETED NODE IS :- %d",temp->data);
  free(temp);
 }
}

void PrintList()
{
 struct node *temp = head;
 if(head==NULL)
 {
  printf("\n LIST IS EMPTY");
 }
 else
 {
  printf("\n PRINTING LIST :- \t");

  do
  {
   printf("   |%d|%u|->",temp->data,temp->next);
   temp = temp->next;
  }while(temp!=head);
 }
 printf("\n");
}

void main()
{
 int m,x,y,z,index,indexx;
 clrscr();

  printf("\n MENU");
  printf("\n 1) Insert At Beginning");
  printf("\n 2) Insert At End");
  printf("\n 3) Insert At Specific Index");
  printf("\n 4) Delete From Beginning");
  printf("\n 5) Delete From End");
  printf("\n 6) Delete From Specific Index");
  printf("\n 7) Print List");
  printf("\n 8) Exit");

   while(1)
 {
  printf("\n\n ENTER YOUR CHOICE :- ");
  scanf("%d",&m);

  switch(m)
  {
   case 1:
  printf("\n ENTER ANY DATA VALUE :- ");
  scanf("%d",&x);
  InsertAtBeginning(x);
  PrintList();
  break;

   case 2:
  printf("\n ENTER ANY DATA VALUE :- ");
  scanf("%d",&y);
  InsertAtEnd(y);
  PrintList();
  break;

   case 3:
   InsertAtN(z,index);
   PrintList();
   break;

   case 4:
   DeleteFromBeginning();
   PrintList();
   break;

   case 5:
   DeleteFromEnd();
   PrintList();
   break;

   case 6:
   DeleteFromN(indexx);
   PrintList();
   break;

   case 7:
   PrintList();
   break;

   case 8:
   exit(0);
  }
 getch();
 }
}

Comments

Popular posts from this blog

Code for BUBBLE Sort

Code for SELECTION Sort in C-Programming