Friday 2 January 2015

Merge_alternate (two linked list)

 In the below code snippet written in C++ node is defined to create nodes of Linked-List . The function insert1 is written to create the first linked-list and while creating the list we have used counters in the main() (function) to identify the number of nodes in each linked-list . Similarly, we have created another function insert2 to create another linked-list . Accordingly we have to merge the two lists alternatively . This is achieved by the function merge_alternate . Taking one element from list1 and other element from list2 we create third list such that it consists of the elements/nodes of both the initial Linked-Lists . First of all we have checked the number of nodes of both the Linked-Lists and this function works for the lists having equal number of nodes .  The linking using pointers is done as shown in the diagram below . The two display functions are created two display the two initial lists and then display the final list . 

Input:--    1->2->3->4                                                                                                                              Input:--    5->6->7->8                                                                                                                             Output:-- 1->5->2->6->3->7->4->8

Code snippet:--

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
struct node
{
    int data;
    node *link;
}*head1=NULL , *head2=NULL, *ptr2 , *ptr1, *nptr,*prev,*fptr2,*fptr1;

void insert1()
{
    int item;
    cout<<"ENTER THE ITEM :"<<endl;
    cin>>item;
    if(head1==NULL)
    {
    nptr=new node;
    nptr->data=item;
    nptr->link=NULL;
    head1=nptr;
    }
    else
    {
        ptr1=head1;
        nptr=new node;
       
        while(ptr1!=NULL)
        {   
            if(ptr1->link==NULL)
            {
                nptr->data=item;
                ptr1->link=nptr;
                nptr->link=NULL;
            }   
            ptr1=ptr1->link;
        }
    }
}


void insert2()
{
    int item;
    cout<<"ENTER THE ITEM :"<<endl;
    cin>>item;
    if(head2==NULL)
    {
    nptr=new node;
    nptr->data=item;
    nptr->link=NULL;
    head2=nptr;
    }
    else
    {
        ptr2=head2;
        nptr=new node;
       
        while(ptr2!=NULL)
        {   
            if(ptr2->link==NULL)
            {
                nptr->data=item;
                ptr2->link=nptr;
                nptr->link=NULL;
            }   
            ptr2=ptr2->link;
        }
    }
}

void display2()
{
    ptr2=head2;
    cout<<endl<<"entered list is :\n"<<endl;
    while(ptr2!=NULL)
    {
        cout<<ptr2->data<<"-->";
        ptr2=ptr2->link;
    }
    cout<<endl;
}


void display1()
{
    ptr1=head1;
    cout<<endl<<"entered list is :\n"<<endl;
    while(ptr1!=NULL)
    {
        cout<<ptr1->data<<"-->";
        ptr1=ptr1->link;
    }
    cout<<endl;
}


void merge_alternate(int c1, int c2) // merging alternate elements of two linked list:
{
    ptr2=head2;
    ptr1=head1;
   
    if(c1==c2)
    {
        while(ptr1!=NULL)
        {
            fptr1=ptr1->link;
            fptr2=ptr2->link;
           
            ptr1->link=ptr2;
            ptr2->link=fptr1;
           
            ptr1=(ptr1->link)->link;
            ptr2=fptr2;
        }
        display1();
    }
    else
    cout<<"\nBoth list should contain same number of elements \n";
    }


Logic of merge_alternate function

int main()
{
    int choice,count=0,count1=0;
    while(1)
    {
    cout<<"ENTER YOUR CHOICE \n1. INSERTION IN FiRST LIST \n2. INSERTION IN SECOND LIST \n3. FOR DISPLAY LIST1 \n4. DISPLAY LIST2 \n5. MERGE BOTH LIST \n6. EXIT\n "<<endl;
    cin>>choice;
    switch(choice)
    {
        case 1:
            insert1();
            count++;
            break;
        case 2:
            insert2();
            count1++;
            break;
        case 3:
            display1();
            break;
        case 4:
            display2();
            break;
        case 5:
            merge_alternate(count,count1);
            break;
        case 6:
            exit(0);
            break;
        default: cout<<"\n enter a valid choice ";
    }
    }
    getch();
    return 0;
}


Output :

No comments:

Post a Comment