1 of 3 In this assignment, we will work with linked list and operator overloadin
ID: 3687694 • Letter: 1
Question
1 of 3 In this assignment, we will work with linked list and operator overloading. Define a class LinkedList and making 2 linked lists. Combine the two linked list to make the third linked list. For each node of the linked list, it will contain 2 data type, a string for student name, and a four-digit integer for the RUID (random number). Each node of linked list should also contain the next-pointer to the next node. Overloading the adding operator+) and the assignment operator ) to combine the linked list. For example, the lists are defined using LinkedList class as follow LinkedList list1, list2, list3; l populate list1 and list2 by adding nodes to the lists ist3 = list1+ list2; l overloading the adding operator (+) to perform this operation Note: we would need to overload the assignment operator()to set the result of (list1+list2) to list 3 After adding the two list, select the next operation to perform on the resulted linked list only (list3). The operations are adding node to the list, removing node from the list, print the entire list, print one element of the list, sort the list according to RUID number. To print one element of the list, we will need overload the operatorD to print one element of the list using index as shown. You would also need to overload the ostream operatorExplanation / Answer
#include <iostream>
#include <string>
using namespace std;
class LinkedList
{
public:
struct Node
{
string name;
int ruid;
Node *next;
} *p;
LinkedList()
{
P=NULL;
}
void insert(string data1, int data2);
void delete(string data1, int data2);
void display();
LinkedList operator+(const LinkedList &l);
LinkedList(const LinkedList &l);
};
LinkedList::LinkedList(const LinkedList &l)
{
Node* tmp = l.p;
while (tmp!=NULL)
{
Node* tmpcopy = new Node;
tmpcopy->value1 = tmp->value1;
tmpcopy->value2 = tmp->value2;
tmp = tmp->next;
}
}
LinkedList LinkedList::operator+(const LinkedList &l)
{
LinkedList l1 = *this;
Node *tmp = l1.p;
while(tmp->next!=NULL)
{
tmp=tmp->next;
}
tmp->next = l.p;
return l1;
}
void LinkedList::addLast(string data1, int data2)
{
Node *newNode;
Node *nodePtr;
newNode = new Node;
newNode->name = data1;
newNode->number = data2;
newNode->next = NULL;
if (!head)
head = newNode;
else
{
nodePtr = head;
while (nodePtr->next)
nodePtr = nodePtr->next;
nodePtr->next = newNode;
}
}
void LinkedList::Insert(string data1, int data2)
{
int comp;
Node *newNode;
Node *nodePtr;
Node *prevNode = NULL;
newNode = new Node;
newNode->name = data1;
newNode->number = data2;
if(!head)
{
head = newNode;
newNode->next = NULL;
}
else
{
nodePtr = head;
prevNode = NULL;
while((nodePtr != NULL) && (nodePtr->name.compare(data1) < 0))
{
prevNode = nodePtr;
nodePtr = nodePtr->next;
}
if(prevNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
else
{
prevNode->next = newNode;
newNode->next = nodePtr;
}
}
}
void LinkedList::delete(string data1, int data2)
{
int comp;
Node *nodePtr;
Node *prevNode;
if(!head)
return;
if(nodePtr->name.compare(data1) == 0)
{
nodePtr = head-> next;
delete head;
head = nodePtr;
}
else
{
nodePtr = head;
while((nodePtr != NULL) && (nodePtr->name.compare(data1) != 0))
{
prevNode = nodePtr;
nodePtr = nodePtr-> next;
}
if(nodePtr)
{
prevNode->next = nodePtr->next;
delete nodePtr;
}
}
}
void LinkedList::display()
{
Node *nodePtr;
nodePtr = head;
while(nodePtr)
{
cout << nodePtr->name << nodePtr->number << endl;
nodePtr = nodePtr->next;
}
}
int main()
{
LinkedList list1;
LinkedList list2;
LinkedList list3;
int i,id;
char name[12];
cout << "Enter number elements in the list1" ;
cin>>i;
int c=1;
while(c<=i)
{
c++;
cin>>name;
cin>>id;
list1.insert(name,id);
}
list1. display();
cout << "Enter number elements in the list2" ;
cin>>i;
int c=1;
while(c<=i)
{
c++;
cin>>name;
cin>>id;
list2.insert(name,id);
}
list1. display();
cout << "The combining two lists as follows" ;
list3 = list1+list2;
list3. display();
cout << "select the following options" <<endl;
cout << "1.Add to node the list" ;
cout << "2.Remove node from list" ;
cout << "3.print list" ;
cout << "4.Quit" ;
int o;
cin>>o;
switch(o)
{
case 1:
{
cout << "Enter student name and id to list";
cin>>name;
cin>>id;
list3.insert(name,id);
list3.diaplay();
}
break;
case 2:
{
cout << "Enter Ruid and name to be removed";
cin>>name;
cin>>id;
list3.delete(name,id);
list3.display();
}
break;
case 3:
{
cout<< "print list";
list3.display();
}
case 4:exit(0);
}//switch
return 0;
}//main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.