Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Relational Database And Transact SQL

Authors: Lucy Scott

1st Edition

1974679985, 978-1974679980

More Books

Students also viewed these Databases questions

Question

Conduct an effective performance feedback session. page 360

Answered: 1 week ago