Concatenate Single Circular Linked Lists in C

Description

This program is used to create and concatenate Circular Linked lists in C

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

struct node
{
 int data;
 struct node * next;
};
struct node *head=NULL;
void display();
void concatenate();

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

void concatenate()
{ 
 head=NULL;
 int n,i;
 struct node *temp,*newnode;
 printf("Enter the no. of elements in LIST 1\n");
 scanf("%d",&n);
 printf("Enter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  newnode=(struct node*)malloc(sizeof(struct node));
  scanf("%d",&newnode->data);
  temp=head;
  if(head==NULL)
  {
   newnode->next=NULL;
   head=newnode;
  }
  else
  {
   while(temp->next!=NULL)
    temp=temp->next;
   temp->next=newnode;
   newnode->next=NULL;
  }
  
 }
 printf("Enter the no. of elements in LIST 2\n");
 scanf("%d",&n);
 printf("Enter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  newnode=(struct node*)malloc(sizeof(struct node));
  scanf("%d",&newnode->data);
  temp=head;
  if(head==NULL)
  {
   newnode->next=NULL;
   head=newnode;
  }
  else
  {
   while(temp->next!=NULL)
    temp=temp->next;
   temp->next=newnode;
   newnode->next=NULL;
  }
 }
 newnode->next=head;
 printf("\nList concatented !\n");
 display();
}

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

Concatenation Single Circular linked List Output 1 Concatenation Single Circular linked List Output 2

Labels: