Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following is the code that needs to have a overload function added. Please h

ID: 3533480 • Letter: T

Question

The following is the code that needs to have a overload function added. Please help.



/*overload the operator <= (lexicographic order) such that
a a a <= a b
a b c <= a b c d
1 2 2 <= 1 2 3
1 2 3 <=1 2 3
a b <= b
*/


#ifndef GENERICLISTSOL_CPP
#define GENERICLISTSOL_CPP
#include <iostream>
#include <cstdlib>
#include "genericlist_Sol.h"
using namespace std;

namespace listsavitch
{
//Uses cstdlib:
template<class ItemType>
GenericList<ItemType>::GenericList(int max) : max_length(max),
                        current_length(0)

{
    item = new ItemType[max];
}

template<class ItemType>
GenericList<ItemType>::~GenericList( )


{
    delete [] item;
}

template<class ItemType>
int GenericList<ItemType>::length( ) const
{
    return (current_length);
}

//Uses iostream and cstdlib:
template<class ItemType>
void GenericList<ItemType>::add(ItemType new_item)
{
    if ( full( ) )
      {
    if(!growlist())
    {
        cout<<" Fail to expand the list! ";
        exit(1);
    }
      }
    //now add the new_item
    item[current_length] = new_item;
    current_length = current_length + 1;
}

template<class ItemType>
bool GenericList<ItemType>::full( ) const
{
    return (current_length == max_length);
}

template<class ItemType>
void GenericList<ItemType>::erase( )
{
    current_length = 0;
}


//grow only if needed
//return false if there is any memory allocation problem, return true otherwise.
//The list will grow twice the size if needed
template<class ItemType>
bool GenericList<ItemType>::growlist( )
{
    if (!full())
      return true;
    try
      {
    ItemType *tmp=new ItemType[2*max_length];
      
    //copy the list now from the original one
    for(int i=0;i<current_length;i++)
    {
        tmp[i]=item[i]; //make sure "=" works for ItemType. Otherwise overload "="
    }
    //adjust max_length
    max_length=2*max_length;
    delete [] item;
    item=tmp;
    return true;
      }
    catch(std::bad_alloc &exc)
      {
    return false;
      }
}

//Uses iostream:
template<class ItemType>
ostream& operator <<(ostream& outs, const GenericList<ItemType>& the_list)
{
    for (int i = 0; i < the_list.current_length; i++)
      outs << the_list.item[i] << endl;

    return outs;
}

template <class ItemType>
istream& operator >> (istream& ins, GenericList<ItemType>& the_list)
{
    ItemType temp;
    if (ins>>temp)
      {
    the_list.add(temp);
      }
    return ins;
}

  
template<class ItemType>
bool operator == (const GenericList< ItemType>& Llist,
            const GenericList< ItemType>& Rlist)
{
    int Length=Llist.length();
    if(Length!=Rlist.length())
      return false;
    for(int i=0;i<Length;i++)
      {
    if (Llist.item[i]!=Rlist.item[i])
    return false;
      }
    return true;
}
  




}//listsavitch
#endif // GENERICLISTSOL_CPP Notice that we have enclosed all the template
       // definitions in #ifndef... #endif.



Explanation / Answer

template<class ItemType>

bool operator <= (const GenericList< ItemType>& Llist,

const GenericList< ItemType>& Rlist)

{

int Length=min(Llist.length(),Rlist.length());

bool flag = true;

for(int i=0;i<Length;i++)

{

if (Llist.item[i]>Rlist.item[i])

{

flag = false;

break;

}

}

if(flag == false)

return flag;

else if(Llist.length()<=Rlist.length())

return flag;

else

return false;

}