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

Please solve in C++ language Part 1 - Debugging the List class There are three b

ID: 3723608 • Letter: P

Question

Please solve in C++ language

Part 1 - Debugging the List class

There are three bugs in this program. Your job is to find them and fix them. Hint: they are all in the List.cc file, do not modify any other file!
There are some common techniques used to track down bugs in a program:

Code walkthrough: use your brain as a CPU and execute the statements in your head, keeping track of the contents of variables on paper

Tracing: add output statements throughout the code to print out the execution flow and contents of variables

Debugger: use a debugging tool to step through every instruction of the program and view the contents of variables

You can use all of these techniques, or just some of them, to find the bugs in the program.


Part 2 - Copying the List

1. 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.

2. 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;

while (currNode->next != 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;

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

Explanation / Answer

// File Name: student.h
#ifndef STUDENT_H
#define STUDENT_H
#include<string>
using namespace std;

// Class Student definition
class Student
{
public:
// Prototype of member functions
Student(string na="000000000", string da="");
~Student();
string getName() const;
void setName(string);
void print() const;

private:
// Data member
const string number;
string name;
};// End of class

#endif

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

// File Name: student.cpp
#include<iostream>
#include "Student.h"
using namespace std;

// Parameterized constructor definitioni
Student::Student(string nu, string na): number(nu), name(na) { }

Student::~Student() { }
// Function to return name
string Student::getName() const { return name; }
// Function to set name
void Student::setName(string n) { name = n; }
// Function to display number and name
void Student::print() const
{
cout<<" Student Number: "<<number<<" Name: "<<name;
}// End of function

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

// File Name: list.h
#ifndef LIST_H
#define LIST_H

#include "Student.h"
// Class List definition
class List
{
class Node
{
friend class List;
private:
// Inner class data member Student class pointer
Student* data;
// To point to next node
Node* next;
};// End of inner class Node

public:
// Prototype of member function
List();
~List();
void addFront(Student*);
void addAlpha(Student*);
void print() const;

private:
// Node Object pointer for head of the list
Node* head;
};// End of class List

#endif

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

// File Name: list.cpp
#include<iostream>
#include "List.h"
#include "student.cpp"
using namespace std;

// Default constructor
List::List() : head(0) { }

// Destructor definition
List::~List()
{
// Creates two temporary pinter object of type Node
Node *currNode, *nextNode;
// Current node points to head
currNode = head;
// Loops till current node is not null
while (currNode != 0)
{
// Points to next node
nextNode = currNode->next;
// Delete current node
delete currNode;
// Current node points to next node
currNode = nextNode;
}// End of while loop
}// End of destructor


// Adds a student to the front of the list
void List::addFront(Student* newStu)
{
// Creates a temporary node
Node* tmpNode = new Node();
// Assigns Student object to the list
tmpNode->data = newStu;
// temporary node next points to head
tmpNode->next = head;
// head points to temporary node
head = tmpNode;
}// End of function

// Adds a student in alphabetical order
void List::addAlpha(Student* newStu)
{
// Creates a temporary node
Node *tmpNode = new Node();
// Assigns Student object to the list
tmpNode->data = newStu;
// temporary node next points to null
tmpNode->next = NULL;
// Creates two temporary pinter object of type Node
Node *currentNode, *prevNode;
// Both the nodes points to head
prevNode = head;
currentNode = head;

// Checks for empty list
if(head != NULL)
{
// Checks for the first on the list
if(tmpNode->data->getName() <= prevNode->data->getName())
{
// head is pointing to new node
head = tmpNode;
// new node next points to previous node
tmpNode->next = prevNode;
}// End of if condition
// Otherwise more than one node available
else
{
// Loops till end of the list
while(currentNode->next != NULL)
{
// Move to next node
currentNode = currentNode->next;
// Compares the name of the temporary node and previous node and next node name
if(tmpNode->data->getName() >= prevNode->data->getName() && tmpNode->data->getName() <= currentNode->data->getName())
{
// Previous node next points to new node
prevNode->next = tmpNode;
// New node next points to current node
tmpNode->next = currentNode;
break;
}// End of if condition
// Previous node points to previous node next
prevNode = prevNode->next;
}// End of while condition

// Checks if it is end of list
if(currentNode->next == NULL)
// Current node next points to new node
currentNode->next = tmpNode;
}// End of inner else
}// End of outer if condition
// Otherwise it is empty list
else
// head is pointing to new node
head = tmpNode;
}// End of function

// Function to print the list
void List::print() const
{
// Creates a temporary node pointing to head
Node* currNode = head;
// Checks if it is null display error message
if(currNode == 0)
{
cout<<" ERROR: List is empty.";
return;
}// End of if condition
// Loops till end of the list
do
{
// Recursively calls the print function to display data
currNode->data->print();
// Move to next node
currNode = currNode->next;
} while (currNode != 0); // End of do - while condition
}// End of function

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

// File Name: main.cpp
#include<iostream>
#include<fstream>
#include<stdlib.h>
#include "Student.h"
#include "List.cpp"

using namespace std;
// main function definition
int main()
{
// Creates ifstream class object to read stu.txt file
ifstream infile("stu.txt",ios::in);
// Checks whether file can be opened or not. If not display error message
if (!infile)
{
cout<<" Could not open file ";
exit(1);
}// End of if condition
// Declares List class two objects
List comp2404, comp2404two;

// Displays empty list
cout << endl << "EMPTY LIST: " << endl;
comp2404.print();
// Declares Student class pointer
Student* stuPtr;
// Local variable to store name and number
string name, number;
// Loops till end of file and reads name and number from file
while (infile >> number >> name)
{
// Assigns name and number to student object pointer using parameterized constructor
stuPtr = new Student(number, name);
// Calls the List class function to add the object at the front of the list
comp2404.addFront(stuPtr);
// Calls the List class function to add the object at the alphabetical order of the list
comp2404two.addAlpha(stuPtr);
}// End of while condition
cout << endl << " ADDED TO FRONT: " << endl;
// Calls the function to display unordered alphabetical list
comp2404.print();

cout << endl << " ADDED IN ALPHABETICAL ORDER: " << endl;
// Calls the function to display ordered alphabetical list
comp2404two.print();

return 0;
}// End of main function

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

Sample Output:

EMPTY LIST:

ERROR: List is empty.

ADDED TO FRONT:

Student Number: 113
Name: Sahu
Student Number: 112
Name: Mohan
Student Number: 111
Name: Pyari

ADDED IN ALPHABETICAL ORDER:

Student Number: 112
Name: Mohan
Student Number: 111
Name: Pyari
Student Number: 113
Name: Sahu

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

File stu.txt contents

111
Pyari
112
Mohan
113
Sahu