C++ Help Needed with LinkedList and Operator Overloading This is what we have to
ID: 3685055 • Letter: C
Question
C++ Help Needed with LinkedList and Operator Overloading
This is what we have to do:
1) Define a class LinkedList and make 2 linked lists.
2) Then, combine the list 1 and list 2 to make a third linked list. So, list 1 + list 2 = list 3. To do this, we have to overload the '+' operator by doing operator+() to add list1 and list2. Then we have to overload the '=' operator by doing operator=() to set result of list1+list2 equal to list 3.
3) Each item in the linked list will contain a 4 digit ID number made of random integers and a string for a name. Each item of the linked list should also contain the next-pointer to the next item.
4) From this point on, every operation will be performed on list 3.
5) The operations we have to do is "Add item to the list", "Remove item from the list", "print the entire list", "print one element of the list", and "sort the list according to ID number".
6) To print one element of the list, we will need overload the operator[] ?to print one element of the list using index as shown. Also we need to overload the ostream operator << ?to display the output by cout << list3[index] << endl?
Could someone please help me do this? I do not understand linked list at all. Your help will be much appreciated!
This is a sample output:
Sample output of operations:
Thank you very much in advance!
Making listl Enter student name John Enter student name: Bob Enter student name Jim Making list2 Enter student name: Mike Enter student name: Chris List 1 ID Name 4325 John 5432 Bolb 6530 Jim List 2 ID Name 1034 Mike 2309 Chris Combined List 3: ID Name 4325 John 5432 Bob 6530 Jim 1034 Mike 2309 ChrisExplanation / Answer
#include<iostream>
#include<string.h>
#include<cstdlib>
using namespace std;
//Function to generate ID
long FourDigitRandom()
{
int randomNumber= (rand() % 10000);
return(randomNumber);
}
//Clas LinkedList Definition
class LinkedList
{
public:
struct Node
{
string name;
int ID;
Node *next;
}*p;
LinkedList()
{
p = NULL;
}
/*~LinkedList()
{
delete p;
}*/
void printAll();
void printOne();
void add(string name);
void remove();
void sort();
LinkedList operator+(const LinkedList &l);
};
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::printAll()
{
if(p==NULL)
{
cout<<"There are no nodes in the list"<<endl;
return;
}
else
{
Node *tmp = p;
cout<<endl<< "The nodes in the list are: "<<endl;
while(tmp!=NULL)
{
cout <<tmp->ID <<" "<<tmp->name<<endl;
tmp=tmp->next;
}
}
}
void LinkedList::printOne()
{
if(p==NULL)
{
cout<<"There are no nodes in the list"<<endl;
return;
}
else
{
cout<<"Please enter the index :";
cin>>input;
int count=0;
while(count!=input)
{
count+=1;
tmp=tmp->next;
}
cout <<tmp->ID <<" "<<tmp->name<<endl;
}
}
void LinkedList::add(string name)
{
Node *tmpLast = new Node;
tmpLast->ID=FourDigitRandom();
tmpLast->name=name;
tmpLast->next=NULL;
if(p==NULL)
p=tmpLast;
else
{
Node *tmp=p;
while(tmp->next!=NULL)
{
tmp=tmp->next;
}
tmp->next=tmpLast;
}
}
void LinkedList::remove()
{
int id;
cout<<"Enter ID to be removed: "<<endl;
cin>>id;
if(p==NULL)
{
cout<<"The list is empty"<<endl;
return;
}
else
{
Node *tmp=p;
while(p->next!=NULL)
{
if(tmp->ID=id)
{
p=p->next;
tmp->next=NULL;
}
}
delete tmp;
}
}
int main()
{
string name;
char ch;
LinkedList list1;
LinkedList list2;
LinkedList list3;
cout<<"Please enter your choice :"<<endl
cout<<"1.Add Item"<<endl<<:"2.Remove Item"<<endl<<"3. Print List"<<endl<<"4. Print Individual item"<<endl<<"5.Quit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Plese press 1 for list1 and 2 for list 2";
cin>>input;
if(input==1);
{
cout<<"Adding name to list 1"<<endl;
cout<<"Please enter name : "<<endl;
list1.add(name);
}
else if(input==2)
{
cout<<"Adding name to list 2"<<endl;
cout<<"Please enter name : "<<endl;
list2.add(name);
}
else
{
cout<<"Wrong Selection";
}
break;
case 2:
cout<<"Plese press 1 for list1 and 2 for list 2";
cin>>input;
if(input==1);
{
cout<<"Removing from list 1"<<endl;
list1.remove();
}
else if(input==2)
{
cout<<"Removing from list 2"<<endl;
list2.remove();
}
else
{
cout<<"Wrong Selection";
}
break;
case 3:
cout<<"Plese press 1 for list1 and 2 for list 2";
cin>>input;
if(input==1);
{
cout<<"Printing from list 1"<<endl;
list1.printAll();
}
else if(input==2)
{
cout<<"Printing from list 2"<<endl;
list2.printAll();
}
else
{
cout<<"Wrong Selection";
}
break;
case 4:
cout<<"Plese press 1 for list1 and 2 for list 2";
cin>>input;
if(input==1);
{
cout<<"Printing from list 1"<<endl;
list1.printOne();
}
else if(input==2)
{
cout<<"Printing from list 2"<<endl;
list2.printOne();
}
else
{
cout<<"Wrong Selection";
}
break;
case 5:
exit(1);
default:
cout<"Invalid Choice:"<<endl;
}
list1.printAll();
list2.printAll();
list3=list1+list2;
list3.printAll();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.