Untitled

 avatar
unknown
plain_text
3 years ago
3.1 kB
3
Indexable
#include<iostream>
#include "unsortedtype.cpp"
using namespace std;

int main(){
    UnsortedType<int> l1,l2;           // made the two list l1 and l2 
    int m,n;
    cout<<"\nInput: ";
    cin>>m;
    for(int i=0;i<m;i++){            // take the inputs in both list
        int data;
        cin>>data;
        l1.InsertItem(data);
    }
    cin>>n;
    for(int i=0;i<n;i++){
        int data;
        cin>>data;
        l2.InsertItem(data);
    }

     // please note that insert item function inserts the new node in first position always, so data inserted in ascending
     // will be present their in descending order actually, for ex the first node of l1 will contain data 40 (for the given input)
     // so in order to store data ascending in final list, we need to insert in descending order

    UnsortedType<int> finalList;            // now made a final list to store both in ascending order
    int item1,item2;                        // initialized two variables item1 and item2, item1 will contain data of l1 and item 2 contain l2
    int len1=0,len2=0;                      // len1 and len2 are to take record of how many value is still to be inserted in both list at any point

    l1.GetNextItem(item1);                    // get the first item of both list, as they will be the greatest so the max among these need to be inserted first to get final ascending
    l2.GetNextItem(item2);
    while(len1<l1.LengthIs() && len2<l2.LengthIs()){  // keep adding untill any one of l1 or l2 finishes
        if (item1>item2){                      // if item1 is greater than add this and increase the counter of len1 
            finalList.InsertItem(item1);
            len1++;
            if (len1<l1.LengthIs())                // if len1 is less than length of l1 that means there are remaining elements in l1 so get the next element
                l1.GetNextItem(item1);
            

        }else{                                  // do same if item2 is greater 
            finalList.InsertItem(item2);
            len2++;
            if (len2<l2.LengthIs())
                l2.GetNextItem(item2);
            
        }

    }
    

    while (len1<l1.LengthIs()){                 // if l1 is remaining then add all the elements of l1 to final
        finalList.InsertItem(item1);
            len1++;
            if (len1<l1.LengthIs())
                l1.GetNextItem(item1);
    }

    while (len2<l2.LengthIs()){                     // if l2 is remaining then add all its element to final, please note that only one while loop will be executed among these two
        finalList.InsertItem(item2);
            len2++;
            if (len2<l2.LengthIs())
                l2.GetNextItem(item2);
    }

    int item;
    int i =0;

    cout<<"\nOutput: ";
    while (i<finalList.LengthIs()){               // now print the final list
        finalList.GetNextItem(item);
        cout<<item<<" ";
        i++;
    }

    cout<<endl<<endl;
    
    return 0;
}
Editor is loading...