Question

In: Computer Science

Add a copy constructor for the linked list implementation below: ---------------------------------------------------------------------------------------------------------------------------------------------------- // list.cpp file #include <string>

Add a copy constructor for the linked list implementation below:

----------------------------------------------------------------------------------------------------------------------------------------------------

// list.cpp file

#include <string>
#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
{
// Your code here
  
}

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;
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Use the following header file, and test program (not to be modified) to verify that the copy constructor works correctly:

----------------------------------------------------------------------------------------------------------------------------------------------------------------

// list.h file

#ifndef LIST_H
#define LIST_H

#include <string>

using namespace std;

class List;
class Iterator;
//template <typename T>
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<T>* n, Node<T>* 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 file

#include <string>
#include <iostream>
#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;
}


---------------------------------------------------------------------------------------------------------------------------------------------------------------

Thank you for your time and help!

Solutions

Expert Solution

/* list.h */

#ifndef LIST_H
#define LIST_H

#include <string>

using namespace std;

class List;
class Iterator;
//template <typename T>
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<T>* n, Node<T>* 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.cpp */

#include <string>
#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
{
   Node *p = rhs.first;
first = last = NULL;
while(p!=NULL){
this->push_back(p->data);
p=p->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;
}

/* list_test.cpp*/

#include <string>
#include <iostream>
#include "list.cpp"

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;
}

/* OUTPUT */

/* PLEASE UPVOTE */


Related Solutions

Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added....
Add a copy constructor for the linked list implementation below. Upload list.cpp with your code added. (DO NOT MODIFY THE HEADER FILE OR TEST FILE. only modify the list.cpp) /*LIST.CPP : */ #include "list.h" using namespace std; // Node class implemenation template <typename T> Node<T>::Node(T element) { // Constructor    data = element;    previous = nullptr;    next = nullptr; } // List implementation template <typename T> List<T>::List() {    head = nullptr;    tail = nullptr; } template...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and...
Data Structures in Java In the following Singly Linked List implementation, add the following methods, and write test cases in another java file to make sure these methods work. - Write a private method addAfter(int k, Item item) that takes two arguments, an int argument k and a data item, and inserts the item into the list after the K-th list item. - Write a method removeAfter(Node node) that takes a linked-list Node as an argument and removes the node...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove,...
C++ PROGRAMMING In the linked-list based bag implementation, we demonstrated the functionalities, such as, add, remove, and list. This assignment is to extend the functionalities of the bag with other operations average, min, and max, You need to extend the Bag class (under Wk 2, BagLinked_List.cpp) with the following methods: -int Bag::min( ), is to find minimum of items of the bag. -int Bag::max( ), is to find maximum of items of the bag -float Bag::ave( ), is to find...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
Add a CountGroups method to the linked list class below (OurList). It returns the number of...
Add a CountGroups method to the linked list class below (OurList). It returns the number of groups of a value from the list. The value is passed into the method. A group is one or more values. Examples using strings: A list contains the following strings: one, one, dog, dog, one, one, one, dog, dog, dog, dog, one, one, dog, one    CountGroup(“one”) prints 4 groups of one's    CountGroup(“dog”) prints 3 groups of dog's Do not turn in the...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string...
#include <iostream> #include <string> #include <vector> using namespace std; class Song{ public: Song(); //default constructor Song(string t, string a, double d); //parametrized constructor string getTitle()const; // return title string getAuthor()const; // return author double getDurationMin() const; // return duration in minutes double getDurationSec() const; // return song's duration in seconds void setTitle(string t); //set title to t void setAuthor(string a); //set author to a void setDurationMin(double d); //set durationMin to d private: string title; //title of the song string author;...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- // slist.cpp #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to...
Add the following methods to the singly list implementation below. int size(); // Returns the number...
Add the following methods to the singly list implementation below. int size(); // Returns the number of nodes in the linked list bool search(string query); // Returns if the query is present in the list void add(List& l); // // Adds elements of input list to front of "this" list (the list that calls the add method) #include <string> #include "slist.h" using namespace std; Node::Node(string element) : data{element}, next{nullptr} {} List::List() : first{nullptr} {} // Adds to the front of...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data;...
How to reverse linked list below,thank you! #include <stdlib.h> #include <stdio.h> struct list { int data; struct list *next; }; typedef struct list node; typedef node *link; int main() { link ptr,head; int num,i; head = ( link ) malloc(sizeof(node)); ptr = head; printf("enter 10 data \n"); for ( i = 0; i <= 9; i++ ) { scanf("%d",&num); ptr->data = num; ptr->next = ( link ) malloc(sizeof(node)); if ( i == 9 ) ptr->next = NULL; else ptr =...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT