Splitting Single Circular Linked List in C

Description

This program is used to Create SIngle Circular Linked List and split the list as per users input in C

#include<stdio.h>
#include<stdlib.h>

struct node
{
 int data;
 struct node * next;
};
struct node *head1=NULL,*head2=NULL,*head=NULL;
void display();
void create();
void split();
int return_count();

void main()
{
  int choice;
  do
  {
   printf("\n__MENU__\n1.Create List\n2.Split List\n3...EXIT...\n");
   printf("\nENTER your choice\n");
   scanf("%d",&choice);
   switch(choice)
   {
    case 1:create();break;
    case 2:split();break; 
    case 3:printf("~~~~~~~THANK YOU~~~~~~~\n");break;
   default:printf("INVALID input !!!");break;   
  }
  }while(choice!=3);
}

void split()
{
 struct node *temp;
 int pos,i,total;
 temp=head;
 head1=NULL;
 if(head==NULL)
 printf("\nList is empty\n\n");
 else
 {
  if(temp->next==head)
   printf("list cannot be splitted as only 1 element is present\n");
  else
  {
   printf("Enter splitting position\n");
   scanf("%d",&pos);
   total=return_count();
   if(pos>=total)
    printf("\nInvalid Postion !!!!!\n");
   else
   {
    for(i=1;i<pos;i++)
     temp=temp->next;
    head2=temp->next;
    temp->next=head;
    
    printf("list 1 :\t");
    temp=head;
    while(temp->next!=head)
    {
     printf("%d\t",temp->data);
     temp=temp->next;
    }
    printf("%d\t",temp->data);
    
    printf("\nlist 2 :\t");
    temp=head2;
    while(temp->next!=head)
    {
     printf("%d\t",temp->data);
     temp=temp->next;
    }
    printf("%d\t",temp->data);
   }
  }
 }
}


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();
}
int return_count()
{
 int i = 1;
 struct node* temp;
 temp=head;
 if(head==NULL)
 return 1;
 while(temp->next!=head)
 {
  temp=temp->next;
  i++;
 }
 return i;
}

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

Splitting Linked List Ouput 1 Splitting Linked List Ouput 2
Splitting Linked List Ouput 3

Labels: