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

General List Program You are to implement a C++ List\' class to handle a list wi

ID: 3745192 • Letter: G

Question

General List Program You are to implement a C++ List' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. Your list will be an array. The list has no order, except for the order you insert or delete. The methods you are to implement are as given: Constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'List listA(listB)) empty returns true or false if list is empty or not. first makes current position at the beginning of the list last makes current position at the end of a list. prev places current position at the previous element in the list next places current position at the next element in the list getPos returns current position or where you are in the list setPos(int) places current position in a certain position in the list insertBefore inserts a new element before the current position insertAfter inserts a new element after the current position get Element returns the one element that current position is pointing to size returns the size of the list (number of elements in list) replace(int) replace the current element with a new value erase deletes the current element clear makes the list an empty list overload the operators: (at least)

Explanation / Answer

# include<iostream>
# include<string.h>
# include <cstring.h>
//#include<List>
//#include<iterator>
class List
{
int CAPACITY=20,a*,i,j,currindex,finalindex;
char c*,elementtype;
float b*;

List()
{
  
      i=0;
      j=0;
      finalindex=0;
      currindex=0;
      a=new int [CAPACITY];
}
List(string s)
{
      i=0;
      j=0;
      finalindex=0;
      currindex=0;
      if (s=="int")
      {
       a=new int [CAPACITY];
       elementtype='i';
      }
      else if(s=="float")
      {
       elementtype='f';
       c= new float [CAPACITY];
      }
      else if(s=="char")
      {
       elementtype='c';
      
       b=new char [CAPACITY];
      }
      else{
       cout<<"incorrect data type"<<endl;
      }
    
}
List(List &list)
{

}
bool empty()
{
    if(finalndex==0)
    {
      return true;
    }
    else
    {
      return false;
    }
}
void first()
{
    currindex=0;
}
void last()
{
    currindex=finalindex;
}
void prev()
{
    if(currindex!=0)
    {
      currindex=currindex-1;
    }
    else
    {
        cout<<"Index is at first position"<<endl;;
    }
}
void next()
{
    if(currindex!=finalindex)
    {
      currindex=currindex+1;
    }
    else
    {
      cout<<"Index at last position"<<endl;
    }
}
int getPos()
{
  
       return currindex;
}
void setPos(int x)
{
   if (x<=finalindex)
       currindex=x-1;
   else
       cout<<"Position does not exist"<<endl;
}
void insertBefore(int x)
{
  
   if(finalindex!=CAPACITY-1)
   {
      
       for(i=finalindex+1;i>=currindex-1;i++)
       {
           a[i]=a[i-1];
       }
       a[currindex-1]=x;
       finalindex++;
   }
   else
   {
       cout<<"Capacity is full"<<endl;
   }
}
    void insertBefore(float x)
{
  
   if(finalindex!=CAPACITY-1)
   {
      
       for(i=finalindex+1;i>=currindex-1;i++)
       {
           b[i]=b[i-1];
       }
       b[currindex-1]=x;
       finalindex++;
   }
   else
   {
       cout<<"Capacity is full"<<endl;
   }
}
    void insertBefore(char x)
{
  
   if(finalindex!=CAPACITY-1)
   {
      
       for(i=finalindex+1;i>=currindex-1;i++)
       {
           c[i]=c[i-1];
       }
       c[currindex-1]=x;
       finalindex++;
   }
   else
   {
       cout<<"Capacity is full"<<endl;
   }
}
void insertAfter(int x)
{
  
   if(finalindex!=CAPACITY-1)
   {
       for(i=finalindex+1;i>=currindex+1;i++)
       {
           a[i]=a[i-1];
       }
       a[currindex+1]=x;
       finalindex++;
      
      
   }
}
void insertAfter(float x)
{
  
   if(finalindex!=CAPACITY-1)
   {
       for(i=finalindex+1;i>=currindex+1;i++)
       {
           b[i]=b[i-1];
       }
       b[currindex+1]=x;
       finalindex++;
      
      
   }
}
void insertAfter(char x)
{
  
   if(finalindex!=CAPACITY-1)
   {
       for(i=finalindex+1;i>=currindex+1;i++)
       {
           c[i]=c[i-1];
       }
       c[currindex+1]=x;
       finalindex++;
      
      
   }
}
void insert(int x)
{
   if(finalindex!=CAPACITY-1)
   {
       a[finalindex]=x;
       finalindex++;
   }
   else
   {
       cout<<"List is full"<<endl;
   }
  
}
void insert(float x)
{
   if(finalindex!=CAPACITY-1)
   {
       b[finalindex]=x;
       finalindex++;
   }
   else
   {
       cout<<"List is full"<<endl;
   }

}
void insert(char x)
{
   if(finalindex!=CAPACITY-1)
   {
       c[finalindex]=x;
       finalindex++;
   }
   else
   {
       cout<<"List is full"<<endl;
   }
}
int size()
{
   return finalindex+1;
}
void replace(int x)
{
  
   a[currindex]=x;
}
void replace(float x)
{
  
   b[currindex]=x;
}
void replace(char x)
{
  
   c[currindex]=x;
}
/*void erase()
{
  
   for(i=currindex;i<=finalindex;i++)
   {
       if(elementtype=='i')
       {
           a[i]=a[i+1];
       }
       else if(elementtype=='f')
       {
           b[i]=b[i+1];
       }
       else if(elementtype=='c')
       {
           c[i]=c[i+1];
       }
      
   }
   finalindex--;
}*/
void erase()
{
   if(elementtype=='i')
       {
           a[finalindex]=0;
       }
   else if(elementtype=='f')
       {
           b[finalindex]=0;
       }
   else if(elementtype=='c')
       {
           c[finalindex]='0';
       }
   finalindex--;
}
void clear()
{
  
   for(i=0;i<CAPACITY;i++)
   {
       if(elementtype=='i')
       {
           a[finalindex]=0;
       }
   else if(elementtype=='f')
       {
           b[finalindex]=0;
       }
   else if(elementtype=='c')
       {
           c[finalindex]='0';
       }
      
   }
   finalindex=0;
}
void push_front(int x)
{
   if(finalindex!=CAPACITY-1)
   {
       a[finalindex+1]=x;
       finalindex++;
   }
   else{
       cout<<"Capacity is full"<<endl;
   }
}
    void push_front(float x)
{
   if(finalindex!=CAPACITY-1)
   {
       b[finalindex+1]=x;
       finalindex++;
   }
   else{
       cout<<"Capacity is full"<<endl;
   }
}
    void push_front(char x)
{
   if(finalindex!=CAPACITY-1)
   {
       c[finalindex+1]=x;
       finalindex++;
   }
   else{
       cout<<"Capacity is full"<<endl;
   }
}
void push_back(int x)
{
   if(finalindex!=CAPACITY)
   {
      
       for(i=finalindex+1;i>=0;i--)
       {
           a[i]=a[i-1];
       }
       a[0]=x;
   }
}
    void push_back(float x)
{
   if(finalindex!=CAPACITY)
   {
      
       for(i=finalindex+1;i>=0;i--)
       {
           b[i]=b[i-1];
       }
       b[0]=x;
   }
}
    void push_back(char x)
{
   if(finalindex!=CAPACITY)
   {
      
       for(i=finalindex+1;i>=0;i--)
       {
           c[i]=c[i-1];
       }
       c[0]=x;
   }
}
List operator <<(List &obj)
{
   for(i=0;i<=obj.finalindex;i++)
   {
       if(elementtype=='i')
       {
           cout<<a[i];
       }
       else if(elementtype=='b')
       {
           cout<<b[i];
       }
       else if(elementtype=='c')
       {
           cout<<c[i];
       }
   }
}
List operator ==(List &obj)
{
   int flag;
   if(finalindex==obj.finalindex)
   {
       if (elementtype=='i'&& obj.elementtype=='i')
       {
           for(i=0;i<=finalindex;i++)
           {
               if (a[i]==obj.a[i])
               {
                   flag=1;
               }
               else
               {
                   flag=0;
                   break;
               }
           }
          
       }
       else if(elementtype=='f' && obj.elementtype=='f')
       {
           for(i=0;i<=finalindex;i++)
           {
               if (b[i]==obj.b[i])
               {
                   flag=1;
               }
               else
               {
                   flag=0;
                   break;
               }
           }
          
       }
       else if (elementtype=='c'&& obj.elementtype=='c')
       {
          
           for(i=0;i<=finalindex;i++)
           {
               if (c[i]==obj.c[i])
               {
                   flag=1;
               }
               else
               {
                   flag=0;
                   break;
               }
           }
       }
      
       if (flag!=1)
       {
           cout<<"The lists are not equal"<<endl;
       }
      
   }
   cout<<"The lists are not equal"<<endl;
}
List operator != (List &obj)
{
   bool flag;
   if (finalindex==obj.finalindex)
   {
      
       if (elementtype=='i' && obj.elementtype=='i')
       {
          
           for(i=0;i<=finalindex;i++)
           {
               if(a[i]!=obj.a[i])
               {
                   flag=true;
               }
               else
               {
                   flag=false;
                   break;
               }
           }
       }
       else if(elementtype=='f'&& obj.elementtype=='f')
       {
               for(i=0;i<=finalindex;i++)
           {
               if(b[i]!=obj.b[i])
               {
                   flag=true;
               }
               else
               {
                   flag=false;
                   break;
               }
           }
          
       }
       else if(elementtype=='c'&& obj.elementtype=='c')
       {
               for(i=0;i<=finalindex;i++)
           {
               if(c[i]!=obj.c[i])
               {
                   flag=true;
               }
               else
               {
                   flag=false;
                   break;
               }
           }
       }
       return flag;
   }
   else
   {
       return true;
       //cout<<"List size different!"<<endl;
   }
}
List operator + (List &obj)// this function is adding the two lists and storing it in the first one.
{
   //int x*;
   if (finalindex==obj.finalindex)
   {
       //x=new int[finalindex];
       for(i=0;i<=finalindex;i++)
       {
           //x[i]=a[i]+obj.a[i];
           if (elementtype=='i' && obj.elementtype=='i')
           {
               a[i]+=obj.a[i];
              
           }
           else if(elementtype=='f'&& obj.elementtype=='f')
           {
               b[i]+=obj.b[i];
              
           }
           else if(elementtype=='c'&& obj.elementtype=='c')
           {
               c[i]+=obj.c[i];
              
           }
        }
        //return x;
   }
   /* this part will add the two list despite the differences in the lengths
   else if(finalindex>obj.finalindex)
   {
       for (i=0;i<=obj.finalindex;i++)
       {
           a[i]+=obj.a[i];
       }
   }
   else
   {
       for (i=0;i<=finalindex;i++)
       {
           a[i]+=obj.a[i];
       }
   }*/
   else{
       cout<<"Unequal lengths "<<endl;
   }
}
List operator =(List &obj)//this function will copy the second string into the first string (lack of data)
{
   if(finalindex==obj.finalindex)
   {
       if (elementtype=='i' && obj.elementtype=='i')
       {
           for (i=0;i<=finalindex;i++)
           {
               a[i]=obj.a[i];
           }
          
       }
       else if(elementtype=='f'&& obj.elementtype=='f')
       {
           for (i=0;i<=finalindex;i++)
           {
               b[i]=obj.b[i];
           }
          
       }
       else if(elementtype=='c'&& obj.elementtype=='c')
       {
           for (i=0;i<=finalindex;i++)
           {
               c[i]=obj.c[i];
           }
          
       }
                      
   }
   /*else if(finalindex>obj.finalindex)
   {
       for (i=0;i<=obj.finalindex;i++)
       {
           a[i]=obj.a[i];
       }
       finalindex=obj.finalindex;
   }
   else
   {
       for (i=0;i<=finalindex;i++)
       {
           a[i]=obj.a[i];
       }
  
}*/
}
void display()
{
   for(i=0;i<=finalindex;i++)
   {
       if(elementtype=='i')
       {
               cout<<a[i];
       }
       else if (elementtype=='f')
       {
           cout<<b[i];
       }
       else if(elementtype=='c')
       {
           cout<<c[i];
       }
   }
}
};
int main()
{
  
   List obj=new List("int");
   obj.insert(5);
  
}