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

Using the following UML diagram, create two classes: MyList and MyListB. # is th

ID: 3867164 • Letter: U

Question

Using the following UML diagram, create two classes: MyList and MyListB.

# is the UML notation for protected.

Here's a list of the methods:

Class MyListB:

MyList() - constructor, initialized head to nullptr.

MyList(const MyList& m) - copy constructor

~MyList() - destructor

MyList& operator=(MyList& m) - overloaded assignment operator

void append(int i) - implements the append algorithm. Assigns it's argument to a node, and adds the node to the end of the list.

int find(int i) - searches the list for a node containing the argument. Returns -1 if not found, the relative position of the node where it's found otherwise. For example, if it's found in the head node, find returns 0. If it's found in the node after that, returns 1, etc.

void remove(int i) - searches the list for the first node containing the argument. If found, removes the node.

void print() - prints the contents of the list to the screen.

class MyListB:

Inherits from MyList.

int length() - counts and returns the number of nodes in the list.

void insert(int i) - inserts a node in order for the list. Maintains an ascending order.

Demonstrate your class by writing a program that instantiates MyList and MyListB objects. Insert and append values, remove them, assign objects, etc.

Explanation / Answer

#include <iostream>
using namespace std;

struct node
{
    int data;
    node *next;
};


class MyList
{
    protected:
    node *head, *tail;
    public:
    MyList()
    {
      head=NULL;
      tail=NULL;
    }
   
    MyList(const MyList& m){
        if (m.head == NULL) {
            head = NULL;
        }

        else
        {
            node * current = m.head;
            while( current != NULL){
            node *temp=new node;
            temp->data=current->data;
            temp->next=NULL;
            if(head==NULL)
            {
                head=temp;
                tail=temp;
                temp=NULL;
            }
            else
            {
                tail->next=temp;
                tail=temp;
            }
            current = current->next;
        }
    }
   
     node *append(int value)
    {
      node *temp=new node;
      temp->data=value;
      temp->next=NULL;
      if(head==NULL)
      {
        head=temp;
        tail=temp;
        temp=NULL;
      }
      else
      {
        tail->next=temp;
        tail=temp;
      }
      return temp;
    }

void print()
{
    node *temp=new node;
    temp=head;
    while(temp!=NULL)
    {
      cout<<temp->data<<" ";
      temp=temp->next;
    }
}


int find(int i){
    if(head->data == i)
        return 0;
    node *temp = head;
    while(temp != NULL){
        if(temp->data==i){
            return 1;
        }
        temp = temp->next;
    }
        return -1;
}

void remove(int i){
    node *temp = head;
    if(head->data == i){
        head = head->next;
        free(temp);
        return;
    }
   
    while(temp->next){
        if(temp->next->data == i){
            node *ptr = temp->next;
            temp->next = ptr->next;
            free(ptr);
            return;
        }
        temp = temp->next;
    }
}
   
   
};


class MyListB:public MyList{
      public:
      int length(){
          node *temp = head;
          int count = 0;
          while(temp !=NULL){
              count++;
              temp = temp->next;
          }
          return count;
      }
     
      // Inserting in ascending order
     
      void insert(int i){
          node *temp=new node;
        temp->data=i;
        temp->next=NULL;
        if(head == NULL){
            head = temp;     
        }
        else{
            node *ptr1 = head;
            node *ptr2 = head;
            while(ptr1 && ptr1->data <= i){
                ptr2 = ptr1;
                ptr1 = ptr1->next;
            }
           
            ptr2->next = temp;
            temp->next = ptr1;
        }
      }
     
}

int main() {
MyList list;
list.append(1);
list.append(2);
list.print();
cout << list.find(3);
return 0;
}