Write a C Program to Merge two sorted single linked lists

By | 28.03.2017

C Program to Merge two sorted single linked lists


Write a C Program to Merge two sorted single linked lists. Here’s simple Menu Driven Program to Merge two sorted single 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.


Basic Operations


Following are the basic operations supported by a list.

  • Insertion − Adds an element at the beginning of the list.
  • Deletion − Deletes an element at the beginning of the list.
  • Display − Displays the complete list.
  • Search − Searches an element using the given key.
  • Delete − Deletes an element using the given key.

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


SOURCE CODE : :


/* C Program to Merge two sorted single linked lists*/


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

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

struct node *create(struct node *start);
struct node *insert_s(struct node *start,int data);
struct node *insert(struct node *start,int data);
void display(struct node *start );
void merge(struct node *p1,struct node *p2);
int main()
{
        struct node *start1=NULL,*start2=NULL;
        start1=create(start1);
        start2=create(start2);

        printf("List1 : ");
        display(start1);
        printf("List2 : ");
        display(start2);
        merge(start1, start2);

        return 0;

}/*End of main()*/

void merge(struct node *p1,struct node *p2)
{
        struct node *start3;
        start3=NULL;

        while(p1!=NULL && p2!=NULL)
        {
                if(p1->info < p2->info)
                {
                        start3=insert(start3,p1->info);
                        p1=p1->link;
                }
                else if(p2->info < p1->info)
                {
                        start3=insert(start3,p2->info);
                        p2=p2->link;
                }
                else if(p1->info==p2->info)
                {
                        start3=insert(start3,p1->info);
                        p1=p1->link;
                        p2=p2->link;
                }
        }
        /*If second list has finished and elements left in first list*/
        while(p1!=NULL)
        {
                start3=insert(start3,p1->info);
                p1=p1->link;
        }
        /*If first list has finished and elements left in second list*/
        while(p2!=NULL)
        {
                start3=insert(start3,p2->info);
                p2=p2->link;
        }
        printf("Merged list is : ");
        display(start3);
}

struct node *create(struct node *start )
{
        int i,n,data;
        printf("Enter the number of nodes : ");
        scanf("%d",&n);
        start=NULL;
        for(i=1;i<=n;i++)
        {
                printf("Enter the element to be inserted : ");
                scanf("%d",&data);
                start=insert_s(start, data);
        }
        return start;
}/*End of create_slist()*/

struct node *insert_s(struct node *start,int data)
{
        struct node *p,*tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        /*list empty or data to be added in beginning */
        if(start==NULL || data<start->info)
        {
                tmp->link=start;
                start=tmp;
                return start;
        }
        else
        {
                p=start;
                while(p->link!=NULL && p->link->info < data)
                        p=p->link;
                tmp->link=p->link;
                p->link=tmp;
        }
        return start;
}/*End of insert_s()*/

struct node *insert(struct node *start,int data)
{
        struct node *p,*tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        /*If list is empty*/
        if(start==NULL)
        {
                tmp->link=start;
                start=tmp;
                return start;
        }
        else    /*Insert at the end of the list*/
        {
                p=start;
                while(p->link!=NULL)
                        p=p->link;
                tmp->link=p->link;
                p->link=tmp;
        }
        return start;
}/*End of insert()*/

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

OUTPUT : :


/* C Program to Merge two sorted single linked lists*/

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


Enter the number of nodes : 5
Enter the element to be inserted : 4
Enter the element to be inserted : 1
Enter the element to be inserted : 5
Enter the element to be inserted : 8
Enter the element to be inserted : 6

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

List1 : 1 4 5 6 8

List2 : 0 1 1 2 3

Merged list is : 0 1 1 2 3 4 5 6 8

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….

4 4 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments