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

Hello please answe the question correctly and show the program runs. Part 1 - De

ID: 3725696 • Letter: H

Question

Hello please answe the question correctly and show the program runs.

Part 1 - Debugging the List class
1. You are going to be working with a variation of a program that we developed in class.
You can download the code from cuLearn. Read through the program and make sure
that you understand what it does.
2. 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.

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

LIST.CC

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


// 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);

}

MAIN.CC

#include <iostream>
using namespace std;
#include <fstream>
#include <cstdlib>

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

int main()
{
ifstream infile("stu.txt",ios::in);
if (!infile) {
    cout<<"could not open file"<<endl;
    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;
}

Explanation / Answer

STUDENT.H

class Student

{

public:

Student(string n, string num)

{

name = n;

number = num;

}

string getName()

{

return name;

}

void print(){

cout<<"Name: "<<name<<", Number: "<<number<<endl;

}

private:

string name;

string number;

};

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

LIST.CC

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

}

}

// 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 = NULL;

Node *currNode, *prevNode = NULL;

currNode = head;

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;

while (currNode != NULL)

{

currNode->data->print();

currNode = currNode->next;

}

}


MAIN.CC

#include <iostream>

using namespace std;

#include <fstream>

#include <cstdlib>

#include "Student.h"

#include "List.cc"

int main()

{

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

if (!infile)

{

cout << "could not open file" << endl;

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;

}

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