Sunday, 4 January 2015

Creating a sorted list from two Linked-list

Hello readers , this code is written in c++ and it takes two linked-list as inputs and displays a linked-list after appending one linked-list at the end of the other and sorting it . In nutshell this code sorts the elements of two lists after appending them .  I have used create1 function to enter the elements in the first linked-list and create2 function to enter the elements in the second linked-list .  display1 function is used to display the elements of the first linked-list and display2 function is used to display the elements of second linked-list . Let's see the sort_logic function in the function we have first appended second list at the end of the first list and sorted the larger linked-list formed . For doing ptr1 is made to point to head1 and ptr2 is made to point to head2. We reach at the end of the first linked-list , repeatedly pointing to the next node until a NULL is encountered . On reaching the node having link part as NULL , it is made to point to the head2 i.e the starting part of the second linked-list , display1() is called to display the appended list . Then we took a temporary pointer of node type and made it to point at head1 . We see that now we have two while loops performing the conditional swapping .

The outer loop selects a node and compares the data part of the node with the data parts of all other nodes . The inner loop consists of the if clause and the else clause , in if clause a check is done . If the data part of t pointer is greater than the data part of ptr1 the swapping is done else the ptr1 is made to point to the next node .

Input:--      7->5->3->1                                                                                                                           Input:--      8->6->4->2                                                                                  Output:-    7->5->3->1->8->6->4->2  (After appending)                                                      Output:--   1->2->3->4->5->6->7->8  (After Sorting)

Code Snippet:--             

#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
    int data;
    node *link;
}*head1=NULL,*head2=NULL,*ptr1,*ptr2,*t;
void create1()
{
    int item;
    cout<<"enter the item you want to insert"<<endl;
    cin>>item;
    if(head1==NULL)
    {
        ptr1=new node;
        ptr1->data=item;
        ptr1->link=NULL;
        head1=ptr1;
    }
    else
    {
        ptr1=new node;
        ptr1->data=item;
        ptr1->link=head1;
        head1=ptr1;
    }
}

void create2()
{
    int item;
    cout<<"enter the item you want to insert"<<endl;
    cin>>item;
    if(head2==NULL)
    {
        ptr2=new node;
        ptr2->data=item;
        ptr2->link=NULL;
        head2=ptr2;
    }
    else
    {
        ptr2=new node;
        ptr2->data=item;
        ptr2->link=head2;
        head2=ptr2;
    }
}

void display1()
{
    ptr1=head1;
    while(ptr1!=NULL)
    {
        cout<<ptr1->data<<"\t";
        ptr1=ptr1->link;
    }
}

void display2()
{
    ptr2=head2;
    while(ptr2!=NULL)
    {
        cout<<ptr2->data<<"\t";
        ptr2=ptr2->link;
    }
}

void sort_logic()
{
    ptr1=head1;
    ptr2=head2;
    while(ptr1->link!=NULL)
    {
        ptr1=ptr1->link;
    }
    ptr1->link=ptr2;
    cout<<"Two list after appending"<<endl;
    display1();
    int temp=0;
    t=head1;
    while(t!=NULL)
    {
        ptr1=t->link;
        while(ptr1!=NULL)
        {
            if(t->data>ptr1->data)
            {
                temp=t->data;
                t->data=ptr1->data;
                ptr1->data=temp;
                ptr1=ptr1->link;
            }
            else
            ptr1=ptr1->link;
        }
        t=t->link;
    }
    cout<<endl;
    cout<<"list after sorting"<<endl;
    display1();
}

int main()
{
    int option;
    while(option!=6)
    {
        cout<<endl;
        cout<<"1. enter in the first list"<<endl;
        cout<<"2. enter in the second list"<<endl;
        cout<<"3. display the first list"<<endl;
        cout<<"4. display the second list"<<endl;
        cout<<"5. display the sorted list"<<endl;
        cout<<"6. to exit the program"<<endl;
        cout<<endl;
        cin>>option;
        switch(option)
        {
            case 1: create1();
            break;
            case 2: create2();
            break;
            case 3: display1();
            break;
            case 4: display2();
            break;
            case 5: sort_logic();
            break;
        }
    }
    getch();
    return 0;
}

OUTPUT                                 

 

No comments:

Post a Comment