Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ language Copying the List a. Write a copy constructor for the List class. Th

ID: 3727453 • Letter: C

Question

C++ language

Copying the List

a. Write a copy constructor for the List class. Think very carefully about what it means to copy a linked list. Does it make sense to copy the nodes or to make brand new nodes? Do not copy the data.

b. Think about how you can test the copy constructor, so that you are sure that it works for every case. Implement a global function that reads Student data from the user, adds it to a list, and makes a copy of the list using the copy constructor. It will then make different changes (add or remove) to the original list and to the copy, and print each list. Make sure that changes to the original list do not change the copy, and vice-versa!

----------------------------list.cc--------------------------------------------

#include
using namespace std;

#include "List.h"

List::List() : head(0) { }

List::~List()
{
Node *currNode, *nextNode;

currNode = head;

while (currNode != 0) {
nextNode = currNode->next;
delete currNode;
currNode = nextNode;
}
}


// Adds a student to the front of the list

void List::addFront(Student* newStu)
{
Node* tmpNode = new Node;
tmpNode->data = newStu;

tmpNode->next = head;
head = tmpNode;
}

// Adds a student in alphabetical order

void List::addAlpha(Student* newStu)
{
Node* tmpNode = new Node;
tmpNode->data = newStu;
tmpNode->next = 0;

Node *currNode, *prevNode;

currNode = head;

prevNode = 0;

while (currNode != 0) {
if (currNode->data->getName() > tmpNode->data->getName())
break;
prevNode = currNode;
currNode = currNode->next;
}

if (prevNode == 0) {
head = tmpNode;
}
else {
prevNode->next = tmpNode;
}
tmpNode->next = currNode;
}

void List::print() const
{
Node* currNode = head;

if(currNode == 0)
{
cout<<" ERROR: List is empty.";
return;
}

do {
currNode->data->print();
currNode = currNode->next;
} while (currNode != 0);

}

----------------------------list.h--------------------------------------------

#ifndef LIST_H
#define LIST_H

#include "Student.h"

class List
{
class Node
{
friend class List;
private:
Student* data;
Node* next;
};

public:
List();
~List();
void addFront(Student*);
void addAlpha(Student*);
void print() const;

private:
Node* head;
};

#endif

----------------------------main.cc--------------------------------------------

#include
using namespace std;
#include
#include

#include "Student.h"
#include "List.h"

int main()
{
ifstream infile("stu.txt",ios::in);
if (!infile) {
cout<<"could not open file"< exit(1);
}

List comp2404, comp2404two;

cout << endl << "EMPTY LIST: " << endl;
comp2404.print();

Student* stuPtr;
string name, number;

while (infile >> number >> name) {
stuPtr = new Student(number, name);
comp2404.addFront(stuPtr);
comp2404two.addAlpha(stuPtr);
}

cout << endl << "ADDED TO FRONT: " << endl;
comp2404.print();

cout << endl << "ADDED IN ALPHABETICAL ORDER: " << endl;
comp2404two.print();

return 0;
}

----------------------------student.cc--------------------------------------------

#include
using namespace std;
#include

#include "Student.h"


Student::Student(string nu, string na)
: number(nu), name(na) { }

Student::~Student() { }

string Student::getName() const { return name; }

void Student::setName(string n) { name = n; }

void Student::print() const  
{
cout<<"Student: "<

----------------------------student.h--------------------------------------------

#ifndef STUDENT_H
#define STUDENT_H


class Student
{
public:
Student(string="000000000", string="");
~Student();
string getName() const;
void setName(string);
void print() const;

private:
const string number;
string name;
};

#endif

-----------------stu.txt--------------------------------

00567899 Matilda
100234555 Joe
100456777 Timmy
100333222 Clayton
100999888 Harold

Explanation / Answer

answer:

PROGRAM:

the modified file is as shown below;

#ifndef LIST_H

#define LIST_H

#include "Student.h"

class List

{

class Node

{

friend class List;

private:

Student* data;

Node* next;

};

public:

List();

List(const List &other);

~List();

void addFront(Student*);

void addAlpha(Student*);

void print() const;

private:

Node* head;

};

#endif

#include<iostream>

using namespace std;

#include "list.h"

List::List() : head(0) { }

List::~List()

{

Node currNode, nextNode;

currNode = head;

while (currNode != 0) {

nextNode = currNode->next;

delete currNode;

currNode = nextNode;

}

}

//copy constructor implementation

List::List(const List &other){

Node *temp = other.head;

head = NULL;

while(temp != NULL){

addFront(temp->data);

}

}

// Adds a student to the front of the list

void List::addFront(Student* newStu)

{

Node* tmpNode = new Node;

tmpNode->data = newStu;

tmpNode->next = head;

head = tmpNode;

}

// Adds a student in alphabetical order

void List::addAlpha(Student* newStu)

{

Node* tmpNode = new Node;

tmpNode->data = newStu;

tmpNode->next = 0;

Node currNode, prevNode;

currNode = head;

prevNode = 0;

while (currNode != 0) {

if (currNode->data->getName() > tmpNode->data->getName())

break;

prevNode = currNode;

currNode = currNode->next;

}

if (prevNode == 0) {

head = tmpNode;

}

else {

prevNode->next = tmpNode;

}

tmpNode->next = currNode;

}

void List::print() const

{

Node* currNode = head;

if(currNode == 0)

{

cout<<" ERROR: List is empty.";

return;

}

do {

currNode->data->print();

currNode = currNode->next;

} while (currNode != 0);

}

#include "Student.h"

#include "List.h"

int main()

{

ifstream infile("stu.txt",ios::in);

if (!infile) {

cout<<"could not open file"< exit(1);

}

List comp2404, comp2404two;

cout << endl << "EMPTY LIST: " << endl;

comp2404.print();

Student* stuPtr;

string name, number;

while (infile >> number >> name) {

stuPtr = new Student(number, name);

comp2404.addFront(stuPtr);

comp2404two.addAlpha(stuPtr);

}

cout << endl << "ADDED TO FRONT: " << endl;

comp2404.print();

cout << endl << "ADDED IN ALPHABETICAL ORDER: " << endl;

comp2404two.print();

List comp1(comp2404);

comp1.print();

number = "0000000045267";

name = "Rahul";

comp1.addFront(new Student(number,name));

number = "43215045267";

name = "Rakesh";

comp2404.addFront(new Student(number,name));

comp1.print();

comp2404.print();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote