Question

I'm not sure how to fix my code I keep getting an error with rhs.begin. I...

I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part.

/* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/

#include
#include "list.h"

using namespace std;

Node::Node(string element)
{
data = element;
previous = nullptr;
next = nullptr;
}

List::List()
{
first = nullptr;
last = nullptr;
}

List::List(const List& rhs)// Copy constructor - homework THIS IS THE ONLY PART THAT CAN BE CHANGED IM NOT SURE WHAT IM DOING WRONG
{
Iterator oldIt = rhs.begin();
first = nullptr;
last = nullptr;
while (oldIt.position != nullptr){
   push_back(oldIt.get());
   oldIt.next();
}
}

void List::push_back(string element)
{
Node* new_node = new Node(element);
if (last == nullptr) // List is empty
{
first = new_node;
last = new_node;
}
else
{
new_node->previous = last;
last->next = new_node;
last = new_node;
}
}

void List::insert(Iterator iter, string element)
{
if (iter.position == nullptr)
{
push_back(element);
return;
}

Node* after = iter.position;
Node* before = after->previous;
Node* new_node = new Node(element);
new_node->previous = before;
new_node->next = after;
after->previous = new_node;
if (before == nullptr) // Insert at beginning
{
first = new_node;
}
else
{
before->next = new_node;
}
}

Iterator List::erase(Iterator iter)
{
Node* remove = iter.position;
Node* before = remove->previous;
Node* after = remove->next;
if (remove == first)
{
first = after;
}
else
{
before->next = after;
}
if (remove == last)
{
last = before;
}
else
{
after->previous = before;
}
delete remove;
Iterator r;
r.position = after;
r.container = this;
return r;
}

Iterator List::begin()
{
Iterator iter;
iter.position = first;
iter.container = this;
return iter;
}

Iterator List::end()
{
Iterator iter;
iter.position = nullptr;
iter.container = this;
return iter;
}

Iterator::Iterator()
{
position = nullptr;
container = nullptr;
}

string Iterator::get() const
{
return position->data;
}

void Iterator::next()
{
position = position->next;
}

void Iterator::previous()
{
if (position == nullptr)
{
position = container->last;
}
else
{
position = position->previous;
}
}

bool Iterator::equals(Iterator other) const
{
return position == other.position;
}

/*~~~~~~~~~~~~~~lish.h~~~~~~~~~~~~~*/

#ifndef LIST_H

#define LIST_H

#include

using namespace std;

class List;

class Iterator;

//template

class Node

{

public:

/**

Constructs a node with a given data value.

@param element the data to store in this node

*/

Node(string element); // Node(T element)

// Node(T data, Node* n, Node* n);

private:

string data; // T data;

Node* previous;

Node* next;

friend class List;

friend class Iterator;

};

class List

{

public:

/**

Constructs an empty list.

*/

List();

List(const List& rhs); // Homework

/*

Appends an element to the list.

@param element the value to append

*/

void push_back(string element);

/**

Inserts an element into the list.

@param iter the position before which to insert

@param element the value to insert

*/

void insert(Iterator iter, string element);

/**

Removes an element from the list.

@param iter the position to remove

@return an iterator pointing to the element after the

erased element

*/

Iterator erase(Iterator iter);

/**

Gets the beginning position of the list.

@return an iterator pointing to the beginning of the list

*/

Iterator begin();

/**

Gets the past-the-end position of the list.

@return an iterator pointing past the end of the list

*/

Iterator end();

private:

Node* first;

Node* last;

friend class Iterator;

};

class Iterator

{

public:

/**

Constructs an iterator that does not point into any list.

*/

Iterator();

/**

Looks up the value at a position.

@return the value of the node to which the iterator points

*/

string get() const;

/**

Advances the iterator to the next node.

*/

void next();

/**

Moves the iterator to the previous node.

*/

void previous();

/**

Compares two iterators.

@param other the iterator to compare with this iterator

@return true if this iterator and other are equal

*/

bool equals(Iterator other) const;

private:

Node* position;

List* container;

friend class List;

};

#endif

/*~~~~~~~list_test.cpp~~~~~~*/

#include

#include

#include "list.h"

using namespace std;

int main()

{

List names;

names.push_back("Tom");

names.push_back("Diana");

names.push_back("Harry");

names.push_back("Juliet");

// Add a value in fourth place

Iterator pos = names.begin();

pos.next();

pos.next();

pos.next();

names.insert(pos, "Romeo");

// Remove the value in second place

pos = names.begin();

pos.next();

names.erase(pos);

List names_copy(names); //Copy constructor - homework

names_copy.push_back("Shakespeare");

// Verify that Shakespeare was inserted.

cout << "Printing new list" << endl;

for (pos = names_copy.begin(); !pos.equals(names.end()); pos.next())

{

cout << pos.get() << endl; //

}

cout << "Printing original list " << endl;

for (pos = names.begin(); !pos.equals(names.end()); pos.next())

{

cout << pos.get() << endl;

}

return 0;

}

Homework Answers

Answer #1

Copy Constructor

List::List(const List& rhs)

{

Node *temp = rhs.first;

while (temp != nullptr){

this->push_back(temp->data);

temp = temp->next;

}

}

===========================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using...
C++ Fix my code This code is for imitating the round robin cpu scheduling algorithim using linked lists. Currently I am able to input processes and store / display them properly. The issue begins somewhere after I have displayed the processlist (I get a segmentation fault (core dumped) or the code doesnt seem to run). I have marked the location of where I think the issue begins with a comment. Please fix the code so that it is working properly....
The output only produces the values from the first linkedlist and not the second. Please fix...
The output only produces the values from the first linkedlist and not the second. Please fix the code to produce the desired objective. Thanks for your help! Objective: Interleave Example: Define the calling list as a set of ordered nodes, $L1 = {4, 2, 8 ,5, 8}$, and define the list that is passed as a parameter as a set of ordered nodes, $L2 = {5, 1, 8, 4, 5, 9}$. L1.interleave(L2) yields the set ${4, 5, 2, 1, 8,...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT