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
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++.
#include
using namespace std;
// implementing the dynamic List ADT using Linked List
class Node{
private:
int data;
Node* nextNodePtr;
public:
Node(){}
void setData(int d){
data = d;
}
int getData(){
return data;
}
void setNextNodePtr(Node* nodePtr){
nextNodePtr = nodePtr;
}
Node* getNextNodePtr(){
return nextNodePtr;
}
};
class List{
private:
Node *headPtr;
public:
List(){
headPtr = new Node();
headPtr->setNextNodePtr(0);
}
Node* getHeadPtr(){
return headPtr;
}
bool isEmpty(){
if (headPtr->getNextNodePtr() == 0)
return true;
return false;
}
void insert(int data){
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
while (currentNodePtr != 0){
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
}
Node* newNodePtr = new Node();
newNodePtr->setData(data);
newNodePtr->setNextNodePtr(0);
prevNodePtr->setNextNodePtr(newNodePtr);
}
void insertAtIndex(int insertIndex, int data){
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
int index = 0;
while (currentNodePtr != 0){
if (index == insertIndex)
break;
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}
Node* newNodePtr = new Node();
newNodePtr->setData(data);
newNodePtr->setNextNodePtr(currentNodePtr);
prevNodePtr->setNextNodePtr(newNodePtr);
}
int read(int readIndex){
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
int index = 0;
while (currentNodePtr != 0){
if (index == readIndex)
return currentNodePtr->getData();
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}
return -1; // an invalid value indicating
// index is out of range
}
void modifyElement(int modifyIndex, int data){
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
int index = 0;
while (currentNodePtr != 0){
if (index == modifyIndex){
currentNodePtr->setData(data);
return;
}
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}
}
void deleteElement(int deleteIndex){
Node* currentNodePtr = headPtr->getNextNodePtr();
Node* prevNodePtr = headPtr;
Node* nextNodePtr = headPtr;
int index = 0;
while (currentNodePtr != 0){
if (index == deleteIndex){
nextNodePtr = currentNodePtr->getNextNodePtr();
break;
}
prevNodePtr = currentNodePtr;
currentNodePtr = currentNodePtr->getNextNodePtr();
index++;
}
prevNodePtr->setNextNodePtr(nextNodePtr);
}
void IterativePrint(){
Node* currentNodePtr = headPtr->getNextNodePtr();
while (currentNodePtr != 0){
cout << currentNodePtr->getData() << " ";
currentNodePtr = currentNodePtr->getNextNodePtr();
}
cout << endl;
}
// add code to the member function mergeList(List)
};
int main(){
int largerListSize;
cout << "Enter the number of elements you want to insert in the larger list: ";
cin >> largerListSize;
List largerIntegerList;
for (int i = 0; i < largerListSize; i++){
int value;
cout << "Enter element # " << i << " : ";
cin >> value;
largerIntegerList.insertAtIndex(i, value);
}
cout << "Contents of the Larger List: ";
largerIntegerList.IterativePrint();
int smallerListSize;
cout << "Enter the number of elements you want to insert in the smaller list: ";
cin >> smallerListSize;
List smallerIntegerList;
for (int i = 0; i < smallerListSize; i++){
int value;
cout << "Enter element # " << i << " : ";
cin >> value;
smallerIntegerList.insertAtIndex(i, value);
}
cout << "Contents of the Smaller List: ";
smallerIntegerList.IterativePrint();
largerIntegerList.mergeList(smallerIntegerList);
cout << "Contents of the merged list: ";
largerIntegerList.IterativePrint();
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started