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

Help Please. Will ratelifesaver Create a class definition for an array. The arra

ID: 3614683 • Letter: H

Question

Help Please. Will ratelifesaver Create a class definition for an array. The array willbe implemented as a linked list.

The array should have an additional member called length which isthe current size of the array.

Include a member function called setLength which will change thelength of the array. This will mean that the number of nodes in thelinked list will be expanded and any node which does not contain aset value will contain 0. If the length requested is smaller thanthe current length, then all nodes after the node at the currentlength will be dropped.

Include a member function called sort(bool). If the bool is true,the values in the linked list will be sorted in ascending order,otherwise they will be sorted in descending order.

Include a member function called add(int,int) which will add a newelement to the array at the position specified by the firstparameter.

Include a member function called get(int) which will retrieve thevalue at the position specified by the parameter. Help Please. Will ratelifesaver Create a class definition for an array. The array willbe implemented as a linked list.

The array should have an additional member called length which isthe current size of the array.

Include a member function called setLength which will change thelength of the array. This will mean that the number of nodes in thelinked list will be expanded and any node which does not contain aset value will contain 0. If the length requested is smaller thanthe current length, then all nodes after the node at the currentlength will be dropped.

Include a member function called sort(bool). If the bool is true,the values in the linked list will be sorted in ascending order,otherwise they will be sorted in descending order.

Include a member function called add(int,int) which will add a newelement to the array at the position specified by the firstparameter.

Include a member function called get(int) which will retrieve thevalue at the position specified by the parameter.

Explanation / Answer

#include<iostream>

using namespacestd;

class linklist
{
     private:

            struct node
         {
             int data;
           node *link;
         }*p;
         int length;
   public:

linklist();
         void setLength(int m);
  
         void add( int c,int num );
         int get(intc);
         voiddisplay();
         int count();
         void sort(boolb);
         ~linklist();
};

linklist::linklist()
{
     p=NULL;
}

voidlinklist::setLength(int m)
{
     int i;
    
     if(m<=0)
     return;
node *prev,*q;
if(p==NULL)
{
p=new node;
p->data=0;
p->link=NULL;
}
for(i=0,q=p;i<m;i++)
   {
    prev=q;
    q=q->link;
      if( q == NULL )
      {
              prev->link = new node;
              prev->data=0;
              prev->link;
              q=prev->link;
              q->link = NULL;
      }
   }
   prev->link = NULL;    //discard nextnodes
}

void linklist::sort(boolb)
{
     node *q,*z;int temp;
     q=p;
     while(q!=NULL){
     for(z=q->link;z!=NULL;z=z->link)
     {
      if( q->data < z->data&& b == false)
       {
       temp = q->data;
       q->data = z->data;
       z->data = temp;
       }
       if( q->data > z->data&& b == true)
       {
       temp = q->data;
       q->data = z->data;
       z->data = temp;
       }
     }
     q=q->link;
     }
}
void linklist::add( int c, int num)
{
     node *q,*t;
     if(c>length){
     cout<<"Overflow";
     return;}
   int i;
   for(i=0,q=p;i<c;i++)
   {
        q = q->link;
      if( q == NULL )
      {
          cout<<" There are less than "<<c<<"elements.";
         return;
      }
   }

q->data =num;
}

int linklist::get( intc)
{
     node *q,*t;
     if(c>length|| c<0){
     cout<<"Overflow";
     return -1;}
   int i;
   for(i=0,q=p;i<c;i++)
   {
        q = q->link;
      if( q == NULL )
      {
          cout<<" There are less than "<<c<<"elements.";
         return -1;
      }
   }

returnq->data;
}

voidlinklist::display()
{
     node *q;
   cout<<endl;

   for( q = p ;q != NULL ; q = q->link )
       cout<<endl<<q->data;

}


linklist::~linklist()
{
     node *q;
   if( p == NULL )
        return;

   while( p !=NULL )
   {
        q = p->link;
      delete p;
      p = q;
   }
}

int main()
{
    int i;
    linklist l1;
    l1.setLength(10);
    for(i=0;i<10;i++)
    l1.add(i,i+1);
    l1.sort(false);
    l1.display();
    cout<<endl;
    cout<<"Found "<<l1.get(9)<<"at index 9"<<endl;
   system("pause");
   return 0;
}