Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program Language: C++ Need help printing Output for a Linked List of Student Records .Below is what the Output should look like. Along with my

Program Language: C++

Need help printing Output for a Linked List of Student Records .Below is what the Output should look like. Along with my code which has three program files and text files: List.cpp, List.h, TestMainList.cpp, listIn.txt

Output:

L I N K E D L I S T G P A R E P O R T

Current Date: January 27, 2018

by

XXXXXXXXX, XXXXX

LastName, First ZipCode Sex GPA

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

(Unsorted)

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

Wallace, Mike 23454 M 3.47

Hardworker, Ima 23322 M 1.90

Average, Joe 32211 M 2.08

Goode, Very 02212 M 3.27

Brady, William 32011 M 2.80

Lizt, President's 45932 F 4.00

Prada, Denice 29472 F 3.91

Probie, Ima 32345 M 0.95

Wilson, Roland 23545 M 3.95

Walters, Mary 29311 F 2.70

Brady, Gerald 11234 M 1.74

Wright, Mista 23520 M 3.82

Asp, Baad 24506 F 1.19

Close, Soo 23456 M 2.99

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

14 student records

(GPA sorted)

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

Lizt, President's 45932 F 4.00

Wilson, Roland 23545 M 3.95

Prada, Denice 29472 F 3.91

Wright, Mista 23520 M 3.82

Wallace, Mike 23454 M 3.47

Goode, Very 02212 M 3.27

Close, Soo 23456 M 2.99

Brady, William 32011 M 2.80

Walters, Mary 29311 F 2.70

Average, Joe 32211 M 2.08

Hardworker, Ima 23322 M 1.90

Brady, Gerald 11234 M 1.74

Asp, Baad 24506 F 1.19

Probie, Ima 32345 M 0.95

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

14 student records.

Probie, Ima was deleted from this list.

Lizt, President's was deleted from this list.

Close, Soo was deleted from this list.

Right, Mista was not found in this list.

(GPA sorted)

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

Wilson, Roland 23545 M 3.95

Prada, Denice 29472 F 3.91

Wright, Mista 23520 M 3.82

Wallace, Mike 23454 M 3.47

Goode, Very 02212 M 3.27

Brady, William 32011 M 2.80

Walters, Mary 29311 F 2.70

Average, Joe 32211 M 2.08

Hardworker, Ima 23322 M 1.90

Brady, Gerald 11234 M 1.74

Asp, Baad 24506 F 1.19

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

11 student records.

Dawg, Top was added to this list.

(Name sorted)

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

Asp, Baad 24506 F 1.19

Average, Joe 32211 M 2.08

Brady, Gerald 11234 M 1.74

Brady, William 32011 M 2.80

Dawg, Top 23405 M 4.00

Goode, Very 02212 M 3.27

Hardworker, Ima 23322 M 1.90

Prada, Denice 29472 F 3.91

Wallace, Mike 23454 M 3.47

Walters, Mary 29311 F 2.70

Wilson, Roland 23545 M 3.95

Wright, Mista 23520 M 3.82

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

12 student records.

(Original Main List2)

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

Wallace, Mike 23454 M 3.47

Hardworker, Ima 23322 M 1.90

Average, Joe 32211 M 2.08

Goode, Very 02212 M 3.27

Brady, William 32011 M 2.80

Lizt, President's 45932 F 4.00

Prada, Denice 29472 F 3.91

Probie, Ima 32345 M 0.95

Wilson, Roland 23545 M 3.95

Walters, Mary 29311 F 2.70

Brady, Gerald 11234 M 1.74

Wright, Mista 23520 M 3.82

Asp, Baad 24506 F 1.19

Close, Soo 23456 M 2.99

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

14 student records.

List.cpp

// linkedList.cpp (implementation file for the linkedList)

// What this implementation file does........................

// Written by ...............................................

// Date: ....................................................

#include "List.h"

#include

// default constructor

template

List::List()

{

first = NULL; // set pointers to null

last = NULL; // " " " "

count = 0; // zero the count

}

// copy constructor creates a "deep copy" of the other list

template

List::List

(const List& otherList)

{

first = NULL; // set first pointer to NULL

copyList(otherList); // deep copy the other list

}//end copy constructor

// destructor destroys all nodes that were allocated

template

List::~List()

{

//destroyList(); // calls private destroyList function

cout << "List is destroyed...." << endl << endl; // demonstration only

}//end destructor

//overload the assignment operator

template

const List& List::operator=

(const List& otherList)

{

if (this != &otherList) //avoid self-copy

{

copyList(otherList); // simply call the copyList function

}

return *this; // now, left side has the assigned list

}

template

void List::insertFirst(const Type& item) // building the list "backwards"

{

nodeType *newNode; //pointer to declare the new node

newNode = new nodeType; //create (allocate) new node

newNode->stuRecord = item; //assign item into stuRecord

newNode->link = first; //assign old first pointer to the newNode's link

first = newNode; //assign first to point to the new first node

count++; //increment count

}//end insertFirst

template

void List::insertLast(const Type& rec) // building the list "forwards"

{

nodeType *newNode; //pointer to create the new node

newNode = new nodeType; //create the new node

newNode->stuRecord = rec; //store the new item in the node

newNode->link = nullptr; //set the link field of newNode to NULL

if (first == nullptr) //if list is empty, newNode is both first & last node

{

first = newNode; //assign the new node pointer to first

last = newNode; //assign the new node pointer to last

}

else //list is not empty, insert newNode after last

{

last->link = newNode; //insert newNode after last

last = newNode; //make last point to actual last node in the list

}

count++; // increment count

}//end insertLast

template

void List::copyList(const List& otherList)

{

nodeType *newNode; //local pointer to create a node

nodeType *current; //local pointer to traverse the list

if (first != NULL) //if the list is not empty

destroyList(); //then, make it empty

if (otherList.first == NULL) //otherList is empty

{

first = NULL; //reset pointers

last = NULL; // " "

count = 0; // zero the count

}

else //the other list must have data

{

current = otherList.first; //current points to the list to be copied

count = otherList.count;

//this section copies only the first node

first = new nodeType; //create (allocate) the node

first->stuRecord = current->stuRecord; //copy the stuRecord

first->link = NULL; //set the link field of the node to NULL

last = first; //make last point to the first node

current = current->link; //make current point to the next node

//copy the remaining list

while (current != NULL)

{

newNode = new nodeType; //create (allocate) the node

newNode->stuRecord = current->stuRecord; //copy the stuRecord

newNode->link = NULL; //set the link of newNode to NULL

last->link = newNode; //attach newNode after last

last = newNode; //make last point to the actual last node

current = current->link; //make current point to the next node

}//end while

}//end else

}//end copyList

// length gets the lengto of the list

template

int List::length() const

{

return count; // returns the # of records in the list

} //end length

template

istream & operator>> (istream & infile, List & obj)

{

Type stuRec; // declare a local Type (student record)

string lname, fname, fullName; // declare local variables

string zip; // " " "

char sex; // " " "

float gpa; // " " "

infile >> lname >> fname >> zip >> sex >> gpa; // priming read the entire line

while (!infile.eof()) /// loop while there is data

{

fullName = lname + ", " + fname;// concatenate name with comma

cout << left << setw(18) << fullName /// remove this later

<< right << setw(6) << gpa << endl; /// temporarily test the >> function

stuRec.name = fullName; // assign full name Last, First

stuRec.zipCode = zip; // assign zip

stuRec.gender = sex; // assign gender

stuRec.fGpa = gpa; // assign gpa

obj.insertLast(stuRec); // add record to the end "backwards"

infile >> lname >> fname >> zip >> sex >> gpa; // priming read to entire line

}

return infile; // return the entire input stream

}

template

void List::destroyList()

{

nodeType *temp; //pointer to deallocate the memory occupied by node

while (first != NULL) //while there are nodes in the list

{

temp = first; //set temp to the current node

first = first->link; //advance first to the next node

delete temp;

cout << "Node Destoryed" << endl; //deallocate the memory occupied by temp

}

last = NULL;

temp = NULL; //initialize last to NULL; first has already

//been set to NULL by the while loop

count = 0; // zero the count

}

template

bool List::deleteNode(const Type& deleteItem)

{

nodeType *current; //pointer to traverse the list

nodeType *trailCurrent; //pointer just before current

bool found;

if (first == NULL) //Case 1; the list is empty.

cout << "Cannot delete from an empty list."

<< endl;

else

{

if (first->stuRecord == deleteItem) //Case 2

{

current = first;

first = first->link;

count--;

if (first == NULL) //the list has only one node

last = NULL;

delete current;

}

else //search the list for the node with the given stuRecord

{

found = false;

trailCurrent = first; //set trailCurrent to point

//to the first node

current = first->link; //set current to point to

//the second node

while (current != NULL && !found)

{

if (current->stuRecord != deleteItem)

{

trailCurrent = current;

current = current-> link;

}

else

found = true;

}//end while

if (found) //Case 3; if found, delete the node

{

trailCurrent->link = current->link;

count--;

if (last == current) //node to be deleted

//was the last node

last = trailCurrent; //update the value

//of last

delete current; //delete the node from the list

}

else

cout << "The item to be deleted is not in "

<< "the list." << endl;

}//end else

}//end else

}//end deleteNode

template

ostream & operator<< (ostream & outfile, const List & obj)

{

nodeType *current; // declare local ptr in the function

current = obj.first; // points to a local ptr

outfile << fixed << showpoint << setprecision (2);

while(current != nullptr)

{

outfile << left;

outfile << setw(18)<< current -> stuRecord.name

<

<< setw(8) << current -> stuRecord.zipCode

<< setw(4) << current -> stuRecord.gender

<< setw(7) << current -> stuRecord.fGpa << endl;

current = current ->link;

}

// more stuff and looping

return outfile; // return to the data stream

}

template

void List::sortLinkedList(string field)

{

nodeType * curr ;

nodeType * next ;

bool sorted = true;

for(int i = 0; sorted && i < count; i++)

{

curr = first;

next = curr ->link;

sorted = false;

for(int j = 0; j < count - i - 1; j++)

{

// this tests the gpa sort

if(curr ->stuRecord.fGpa < next->stuRecord.fGpa)

sorted = swapData(curr -> stuRecord, next -> stuRecord);

//advance both pointers again

// how do you do both

}

}

}

template

bool List::swapData(Type & a, Type & b)

{

Type temp = a;

a = b;

b = temp;

return true;

}

List.h

// List class header file (specification). No additions, deletions, or changes are allowed.

#ifndef H_List

#define H_List

#include

#include

using namespace std;

//Definition of one data node (student record)

struct stuRecord

{

string name; // student's "Last, First" name

string zipCode; // student's zip code

char gender; // student's sex

float fGpa; // student's gpa as a floating point

};

// definition of the linked list node used

template

struct nodeType

{

Type stuRecord;

nodeType *link;

};

// forward declaration of class (and friend functions necessary for templated)

template

class List;

template

ostream & operator<< (ostream & outfile, const List &);

template

istream & operator>> (istream & infile, List &);

template

class List

{

friend ostream & operator<< <>(ostream & outfile, const List &);

friend istream & operator>> <>(istream & infile, List &);

public:

List(); // default constructor

List(const List& otherList); // copy constructor

~List(); // destructor

const List& operator= (const List &); // overload assignment operator

void initializeList(); // list cleared to an empty state

bool isEmptyList() const; // determines if list is empty

int length() const; // returns number of records in list

Type front() const; // returns first element in list

Type back() const; // returns last element in list

void insertFirst(const Type& newItem); // inserts newItem in list beginning

void insertLast(const Type& newItem); // inserts newItem at end of list

bool deleteNode(const Type& deleteItem); // deletes record from list

void sortLinkedList(string field); // optimized bubble sort on one field

private:

int count; // number of records in list

nodeType *first; // pointer to first node of list

nodeType *last; // pointer to last node of list

bool swapData(Type &, Type & ); // swaps records for sorting

void copyList(const List& ); // makes a copy of the list.

void destroyList(); // deletes all the nodes from list.

};

#endif

testMainList.cpp

// This program tests various operation of a linked list

// Originally written by author: Malik, but did not work

// Severely modified by Jeff Goldstein, TCC Adjunct Professor, VB Campus

// "... a good start!"

#include

#include

#include

#include "List.cpp"

using namespace std;

void title();

void process (List list);

ifstream fin ("listIn.txt");

ofstream fout("listOut.txt");

int main()

{

List list1;

List list2;

int num;

fin >> list1;

list2 = list1; // you'll have to overload operator = to do this. // you'll have to overload the extraction operator for this.

title();

process (list2);

cout << "List 2 is below" << endl;

cout << list2;

cout << "list 2 # of records is: " << list2.length() << endl;

cout << " ";

return 0;

}

void title()

{

cout << "title Goes here" << endl;

fout << "Title Goes Here" << endl;

}

void process(List list)

{

string key = "GPA";

cout << "is the process function" << endl;

cout << list << endl;

list.sortLinkedList(key);

cout << list <

}

void showlist(List & list, string field)

{

}

listIn.txt

Wallace Mike 23454 M 3.47

Hardworker Ima 23322 M 1.9

Average Joe 32211 M 2.08

Goode Very 02212 M 3.27

Brady William 32011 M 2.8

Lizt President's 45932 F 4.0

Prada Denice 29472 F 3.91

Probie Ima 32345 M 0.95

Wilson Roland 23545 M 3.95

Walters Mary 29311 F 2.7

Brady Gerald 11234 M 1.74

Wright Mista 23520 M 3.82

Asp Baad 24506 F 1.19

Close Soo 23456 M 2.99

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

More Books

Students also viewed these Databases questions