Please help rewrite the following as a linked member function of the sorted list
ID: 3538422 • Letter: P
Question
Please help rewrite the following as a linked member function of the sorted list ADT:
MergeLists(SortedType list1, Sortedtype list2, SortedType& result)
{
item1=list1;
item2=list2;
item3=result;
while(item1!=null) &&(item2!=null)
{
if(item1->value < item2->value)
{
item3=item2;
item2=item2->next;
item3=item3->next;
}
if(item2->value < item1->value)
{
item3=item1;
item1=item1->next;
item3=item3->next;
}
}
if(item2=null)
{
while(item1!=null)
{
result=item1;
item1=item1->next;
item3=item3->next;
}
}
if(item1=null)
{
while(item2!=null)
{
result=item2;
item2=item2->next;
item3=item3->next;
}
}
}
Explanation / Answer
void MergeLists(SortedType list1, SortedType list2, SortedType& list)
{
ItemType item;
bool found;
list.MakeEmpty();
list1.ResetList();
while(!list1.IsLastItem()) {
list1.GetNextItem(item);
list.InsertItem(item);
}
list2.ResetList();
while (!list2.IsLastItem() && (list.LengthIs() < MAX_ITEMS-1)) {
list2.GetNextItem(item);
list.RetrieveItem(item, found);
if(!found)
list.InsertItem(item)
}
if((list.Length()== MAX_ITEMS-1) && (!list2.IsLastItem()))
cout << %u201CNot enough space to merge the lists%u201D << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.