Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C + + Revise the Student Roster Queue class to a template version, utilizing the provided files. Implement exception handling in the code. / /

C++ Revise the Student Roster Queue class to a template version, utilizing the provided files. Implement exception handling in the code.
//Driver.cpp
#include
#include
#include "StudentRosterQueue.h"
int main()
{
StudentRosterQueue sr = StudentRosterQueue();
ifstream inputFile;
inputFile.open("Students.txt");
StudentNode s;
while (!inputFile.eof())
{
Student s;
getline(inputFile, s.name, '\t');
inputFile >> s.ssn >> s.grade;
inputFile >> ws;
sr.enqueue(s);
}
inputFile.close();
sr.printList(cout);
Student temp;
sr.dequeue(temp);
sr.printList(cout);
return 0;
}
//StudentRosterQueue.h
#pragma once
#include
using namespace std;
struct Student {
string name;
int ssn;
char grade;
};
struct StudentNode {
Student item;
StudentNode* next;
};
class EmptyStack : public exception
{
public:
const char* what(){
return "Stack is Empty, cannot dequeue!!
";
}
};
class FullStack : public exception
{
public:
const char* what(){
return "Stack is Full, or not enough memory, cannot enqueue!!
";
}
};
class StudentRosterQueue
{
private:
StudentNode* qFront;
StudentNode* qRear;
public:
StudentRosterQueue();
~StudentRosterQueue();
void dequeue(Student& x);
void enqueue(Student x);
bool isFull();
bool isEmpty();
void printList(ostream&);
};
StudentRosterQueue::StudentRosterQueue(){
qFront = NULL;
}
StudentRosterQueue::~StudentRosterQueue(){
while (!isEmpty()){
Student temp;
dequeue(temp);
}
}
bool StudentRosterQueue::isEmpty(){
if (qFront == NULL)
return true;
else
return false;
}
bool StudentRosterQueue::isFull(){
StudentNode* test;
try {
test = new StudentNode;
delete test;
return false;
}
catch (std::bad_alloc exception){
return true;
}
}
void StudentRosterQueue::enqueue(Student s){
if (isFull())
throw FullStack();
StudentNode* currPos = new StudentNode();
currPos->item = s;
currPos->next = NULL;
if (isEmpty())
qFront = currPos;
else
qRear->next = currPos;
qRear = currPos;
}
void StudentRosterQueue::dequeue(Student& s){
if (isEmpty())
throw EmptyStack();
s = qFront->item;
StudentNode* oldTop = qFront;
qFront = qFront->next;
delete oldTop;
}
void StudentRosterQueue::printList(ostream&){
StudentNode* currPtr = qFront;
while (currPtr != NULL){
cout << currPtr->item.name <<""<< currPtr->item.ssn <<""<< currPtr->item.grade << endl;
currPtr = currPtr->next;
}
cout << endl;
}
//Students.txt
Betsy 123456789 A
Sam Spade 234565677 B
Annie Smith 324233456 A
Thomas J. Jones 345344344 A
Allan Adaire 324234234 C
Jennifer Smallville 234234333 A

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

Step: 3

blur-text-image

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

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions