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

Add a member function mergeList to the Singly Linked List-based implementation o

ID: 3869733 • Letter: A

Question

Add a member function mergeList to the Singly Linked List-based implementation of the

List ADT. The member function takes as input a List object (representing the list of smaller size) as

parameter and appends it to the List object (representing the list of larger size) on which the function will

be called.

Consider largerIntegerList and smallerIntegerList to be the two List objects in the main function. The

main function should call largerIntegerList.mergeList(smallerIntegerList) and then print the contents of

the largerIntegerList using the IterativePrint( ) member function by calling

largerIntegerList.IterativePrint( ) from main. The mergeList member function will append the elements of

the smallerIntegerList to the end of the largerIntegerList. Use the Singly Linked List code for Question 2

attached in this project description

Modify the mergeList member function added to the Singly Linked List-based List class for Question 2 in such a way that you append only elements (from the smaller link list) that are not already in the larger link list. Feel free to create additional member functions to facilitate this. i need to answer using C++

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<time.h>

using namespace std;

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

class LinkedList{

   private:
      node *head;
      node *end;
      int size;

   public:
      LinkedList(){
         head = NULL;
         end = head;
         size = 0;
      }
      void add(int q){
         node *temp;
         node *nd;
         temp = head;
         nd = new node;
         nd->next = NULL;
         nd->data = q;
         if (temp == NULL){
            head = nd;
            end = nd;
            size++;
         }
         else {
             end->next = nd;
             end = nd;
             size++;
         }
      }
      void printList(){
         node *temp;
         int count;
         temp = head;
         count = 0;
         while (temp != NULL){
             if (count % 10 == 0)
                cout << endl;
             cout << temp->data << " " ;
             temp = temp->next;
             count++;             
         }
         cout << endl;
      }
     
      int deleteFromFront(){
          int a;
          node *temp;
          if (head == NULL){
              cout << "List is empty" << endl;
              return -1;
          }
          else {
               temp = head;
               a = head->data;
               head = head->next;
               if (head == NULL)
                  end = NULL;
               delete temp;
               size--;
               return a;
          }
      }
     
      int deleteFromEnd(){
          int a;
          node *temp,*ptr;  
          if (end == NULL){
              cout << "List is empty" << endl;
              return -1;
          }
          else {
               a = end->data;
               temp = head;
               if (head->next == NULL){
                   head = NULL;
                   end = NULL;
               }
               else {
                 while (temp->next != end)
                     temp = temp->next;
                 ptr = end;
                 end = temp;
                 temp->next = NULL;
                 delete ptr;
                 size--;
              }
              return a;
          }
      }
      int get(int a){
           int count;
           struct node *p;
           p = head;
           count = 0;
           if (a > size){
               cout << "Index more than the size of the list ";
               return -1;
           }
           count = 1;
           while (count!= a){
               p = p->next;
               count++;
           }
           return p->data;
      }
      int getSize(){
          return size;
      }
      bool find(int a){
         node *p;
         p = head;
         while (p!=NULL){
            if (p->data == a){
               return true;
            }
            p = p->next;
         }
         if (p==NULL)
            return false;
      }

      void mergeList(LinkedList a){
          int n = a.getSize();
          for (int i = 1; i<=n; i++){
              if (!find(a.get(i)))
                 add(a.get(i));
          }
      }
     

};


int main(){

  
   LinkedList list1,list2;

   srand (time(NULL));

   for (int i = 0; i<7; i++){
       list1.add(rand() % 100 + 1 );
   }
   cout << "List1" << endl;
   list1.printList();
   for (int i = 0; i<5; i++){
       list2.add(rand() % 100 + 1 );
   }
   cout << "List2 " << endl;
   list2.printList();
   list1.mergeList(list2);
   cout << "List2 Merged with List1.List1: " << endl;
   list1.printList();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote