I am a noob and am having trouble with my Linked-List and operator assignment. I
ID: 3904266 • Letter: I
Question
I am a noob and am having trouble with my Linked-List and operator assignment. I believe my copy constructor and operator+ functions are not correct. I cannot figure out what I am doing wrong while debugging. I appreciate any help with correct changes. Please not that the prototypes, constructors, and their inputs as listed in the instructions cannot change. New functions can always be added though if necessary. Thank you!
-------------------------------- INSTRUCTIONS -------------------------------
Consider the List class in Linked List lecture:
#include <iostream>
#include <string>
using namespace std;
class Node {
public:
char letter;
Node * next;
Node(char let = ' ', Node * ptr = nullptr) {
letter = let;
next = ptr;
}
};
class List {
Node * head;
public:
List() {
head = nullptr;
}
void add2End(char let ){
Node * ptr = head;
if (head == nullptr){
head = new Node(let);
}
else {
while (ptr->next != nullptr)
ptr = ptr->next;
ptr->next = new Node(let);
}
}
void fromString(const string & word){}
string toString() const {}
List operator+ (const List & rhs) const {}
~List () {}
friend ostream & operator<<(ostream & out, const List & rhs);
};
Question 1.
Write a void fromString(const string & word) function to create a List
from a string.
Question 2.
Write a string toString( ) function to create a string from a List;
Question 3.
Write a List operator+ (const List & rhs) const function to merge two list together and return a new list. You will need to write a copy construction List( const List & rhs) as well to allow this to work correctly by using a deep copy.
Question 4.
Write the ~List () function call deleteList(Node * & ptr) which must be done recursively. If the headPtr is not the end, call yourself On headPtr->next and then delete the headPtr.
Question 5.
Write the friend function friend ostream & operator<<(ostream & out, const List & rhs);
Remember you must return out at the end of the function.
Question 6.
Test your code with the following in main().
List list1;
List list2;
string word1 = "cat";
string word2 = "fish";
string word3 = "";
list1.fromString(word1);
list2.fromString(word2);
word3 = list2.toString();
cout << list1 << " + " << list2 << " = "; //cat + fish = catfish
cout << list1 + list2 << endl;
-------------------------- WHAT I WROTE -------------------------------
#include <iostream>
#include <string>
#include <ostream>
using namespace std;
class Node{
public:
char letter;
Node * next;
Node(char let = ' ', Node * ptr = nullptr)
{
letter = let;
next = ptr;
}
};
class List{
Node * head;
public:
List()
{
head = nullptr;
}
List(const List& rhs)
{
head = nullptr;
while (rhs.head != nullptr)
{
head = new Node;
head->letter = rhs.head->letter;
rhs.head->next;
}
}
void add2End(char letter)
{
Node * newList = head;
if (head == nullptr)
{
head = new Node(letter);
}
else
{
while (newList->next != nullptr)
newList = newList->next;
newList->next = new Node(letter);
}
}
void fromString(const string & word)
{
for (int i = 0; i < word.size(); i++)
{
add2End(word[i]);
}
}
string toString() const
{
Node* newList = head;
string word;
while (newList != nullptr)
{
word += newList->letter;
newList = newList->next;
}
return word;
}
List operator+ (const List & rhs) const
{
List newList = rhs;
Node* ptr = rhs.head;
while(ptr != nullptr)
{
newList.add2End(ptr->letter);
ptr = ptr->next;
}
return newList;
}
~List()
{
Node* newList;
while (head != nullptr)
{
newList = head->next;
delete head;
head = newList;
}
}
friend ostream & operator<<(ostream & out, const List & rhs)
{
Node* newList = rhs.head;
while (newList != nullptr)
{
out << newList->letter;
newList = newList->next;
}
return out;
}
};
int main()
{
List list1;
List list2;
string word1 = "cat";
string word2 = "fish";
string word3 = "";
list1.fromString(word1);
list2.fromString(word2);
word3 = list2.toString();
cout << " toString test should be cat" << word3 << endl;
cout << list1 << " + " << list2 << " = "; //cat + fish = catfish
cout << list1 + list2 << endl;
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <ostream>
using namespace std;
class Node{
public:
char letter;
Node * next;
Node(char let = ' ', Node * ptr = nullptr)
{
letter = let;
next = ptr;
}
};
class List{
Node * head;
public:
List()
{
head = nullptr;
}
List(const List& rhs)
{
//Initialize the head to nullptr
head = nullptr;
//If the list to be copied has data, do the following steps
if (rhs.head != nullptr)
{
//create a new node and assign that pointer to head and copy the data in head node of rhs list
head = new Node;
head->letter = rhs.head->letter;
Node *temp = head; //temporary variable to hold the complete new list while traversing
Node *orgTemp = rhs.head; //temporary variable to traverse the original list to be copied till end
//When next node in list to copied is not null, then create a new node and assign the pointer to next node
//of new list. Copy the data from next node of original list to this newly created node in the new list.
//Repeat these steps until the next node of original list is null.
while(orgTemp->next != nullptr)
{
temp->next = new Node;
temp = temp->next;
orgTemp=orgTemp->next;
temp->letter=orgTemp->letter;
}
temp->next=nullptr; //assign nullptr to the next of last node in the newly created list
}
}
void add2End(char letter)
{
Node * newList = head;
if (head == nullptr)
{
head = new Node(letter);
}
else
{
while (newList->next != nullptr)
newList = newList->next;
newList->next = new Node(letter);
}
}
void fromString(const string & word)
{
for (int i = 0; i < word.size(); i++)
{
add2End(word[i]);
}
}
string toString() const
{
Node* newList = head;
string word;
while (newList != nullptr)
{
word += newList->letter;
newList = newList->next;
}
return word;
}
List operator+ (const List & rhs) const
{
//copy constructor should be called to copy the first list(left hand side of the concatenation operator)
//into the new list
List newList(*this);
//At the end of new list which contains left hand side list, the right hand side list is concatenated
Node* ptr = rhs.head;
while(ptr != nullptr)
{
newList.add2End(ptr->letter);
ptr = ptr->next;
}
return newList;//Concatenation of lhs + rhs is returned
}
~List()
{
Node* newList;
while (head != nullptr)
{
newList = head->next;
delete head;
head = newList;
}
}
friend ostream & operator<<(ostream & out, const List & rhs)
{
Node* newList = rhs.head;
while (newList != nullptr)
{
out << newList->letter;
newList = newList->next;
}
return out;
}
};
int main()
{
List list1;
List list2;
string word1 = "cat";
string word2 = "fish";
string word3 = "";
list1.fromString(word1);
list2.fromString(word2);
word3 = list2.toString();
cout << " toString test should be cat" << word3 << endl;
cout << list1 << " + " << list2 << " = "; //cat + fish = catfish
cout << list1 + list2 << endl;
return 0;
}
Output:
toString test should be catfish
cat + fish = catfish
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.