Write a C Program to Concatenate two circular linked lists

By | March 29, 2017

C Program to Concatenate two circular linked lists


Write a C Program to Concatenate two circular linked lists. Here’s simple Menu Driven Program to Concatenate two circular linked lists in C Programming Language.


What is Linked List ?


Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each link contains a connection to another link.

Following are the important terms to understand the concept of Linked List.

  • Link − Each link of a linked list can store a data called an element.
  • Next − Each link of a linked list contains a link to the next link called Next.

Each element in a linked list is called as “Node”. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs.


Below is the source code for C Program to Concatenate two circular linked lists which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C Program to concatenate two circular linked lists*/


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

struct node
{
	int info;
	struct node *link;
};

struct node *create_list(struct node *last);
void display(struct node *last);
struct node *addtoempty(struct node *last,int data );
struct node *addatend(struct node *last,int data);
struct node *concat(struct node *last1,struct node *last2);

int main( )
{
	struct node *last1=NULL,*last2=NULL;
	last1=create_list(last1);
	last2=create_list(last2);
	printf("\nFirst list is :  ");
	display(last1);
	printf("\nSecond list is :  ");
	display(last2);
    last1=concat(last1, last2);
	printf("\nConcatenated list is  : ");
	display(last1);

	return 0;

}/*End of main( )*/

struct node *concat( struct node *last1,struct node *last2)
{
	struct node *ptr;
	if(last1==NULL)
	{
		last1=last2;
		return last1;
	}
	if(last2==NULL )
		return last1;
	ptr=last1->link;
	last1->link=last2->link;
	last2->link=ptr;
	last1=last2;
	return last1;
}
struct node *create_list(struct node *last)
{
	int i,n;
	int data;
	printf("\nEnter the number of nodes : ");
	scanf("%d",&n);
	last=NULL;
	if(n==0)
		return last;
	printf("Enter the element to be inserted : ");
	scanf("%d",&data);
	last=addtoempty(last,data);

	for(i=2;i<=n;i++)
	{
		printf("Enter the element to be inserted : ");
		scanf("%d",&data);
		last=addatend(last,data);
	}
	return last;
}

void display(struct node *last)
{
	struct node *p;
	if(last==NULL)
	{
		printf("\nList is empty\n");
		return;
	}
	p=last->link;  /*p points to first node*/
	do
	{
		printf("%d ", p->info);
		p=p->link;
	}while(p!=last->link);
	printf("\n");
}/*End of display( )*/

struct node *addtoempty(struct node *last,int data)
{
	struct node *tmp;
	tmp = (struct node *)malloc(sizeof(struct node));
	tmp->info = data;
	last = tmp;
	last->link = last;
	return last;
}/*End of addtoempty( )*/

struct node *addatend(struct node *last,int data)
{
	struct node *tmp;
	tmp = (struct node *)malloc(sizeof(struct node));
	tmp->info = data;
	tmp->link = last->link;
	last->link = tmp;
	last = tmp;
	return last;
}/*End of addatend( )*/


OUTPUT : :


/* C Program to Concatenate two circular linked lists */


********************* OUTPUT *************************

Enter the number of nodes : 5
Enter the element to be inserted : 5
Enter the element to be inserted : 4
Enter the element to be inserted : 3
Enter the element to be inserted : 2
Enter the element to be inserted : 1

Enter the number of nodes : 5
Enter the element to be inserted : 6
Enter the element to be inserted : 7
Enter the element to be inserted : 8
Enter the element to be inserted : 9
Enter the element to be inserted : 0

First list is :  5 4 3 2 1

Second list is :  6 7 8 9 0

Concatenated list is  : 5 4 3 2 1 6 7 8 9 0

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

Leave a Reply