PLEASE MEET EVERY REQUIREMENT In this assignment your task i
PLEASE MEET EVERY REQUIREMENT!
In this assignment your task is to implement a special kind of doubly-linked lists inside a class called Exam_database.
Conceptually, an Exam_database stores (name, score) pairs, where name is the name of a person who wrote an exam, and score is the score they got on it. For simplicity, we’ll assume names are not empty and contain no spaces. Also, a given name occurs at most once in an Exam_database. A score can be any int (even negative!).
Internally, Exam_database should store (name, score) pairs in a private Node struct/class. Each Node should have at least these 4 pointers:
A pointer to the next node whose name occurs alphabetically after the current one.
A pointer to the previous node whose name occurs alphabetically before the current one.
A pointer to the next node with the next higher score after this one.
A pointer to the previous node with the next lower score before this one.
The idea is that the two alphabetical-order pointers form a doubly-linked list ordered by names, and the two score-order pointers form a different doubly- linked list ordered by scores. The (name, score) nodes should only occur once — don’t just implement two separate doubly-linked lists.
In addition to the a Node struct/class. Exam_database needs two head pointers and two tail pointers. One head points to the exam node whose name comes first alphabetically, and the other head points to the exam node with the lowest score. Similarly, one tail points to the alphabetically last node, and the other tail points to the node with the highest score.
With these nodes and pointers, it is now easy to print out the list in forward order, or reverse order, by name or by score: just follow the correct pointers. For example, if you start at the exam with the highest score and follow the next lowest exam pointers, you will traverse the exams from highest score to lowest score.
When add_exam is called, a Node should be created and inserted into the correct place according to both the name pointers and the score pointers. Do not use any kind of sorting routine anywhere in your code! Use only basic C++ features (such as raw pointers) in your solution.
Solution
#include <string>
#include <stdio>
using namespace std;
class Exam_database {
private:
struct node
{
string name;
int score;
struct node *next_name;
struct node *prev_name;
struct node *next_score;
struct node *prev_score;
}*start_name, *start_score;
public:
// Default constructor: create a new empty Exam_database.
Exam_database(start_name = NULL; start_score = NULL);
// Destructor: make sure all nodes are properly deleted
~Exam_database();
// Adds (n, s) to this Exam_database. If a pair in the database already
// has the name n, then its corresponding score is set to s (thus names
// occur at most in one pair).
//
// Also, it is assumed that name is not empty, and does not contain any
// spaces. If it is empty or contained any spaces, then use cmpt::error to
// throw an exception.
void add_exam(string n, int s)
{
struct node *s, *temp, *tempp, *i;
temp = new(struct node);
temp->name = n;
temp->score = s;
if (start_name == NULL)
{
temp->next_name = NULL;
temp->prev_name = NULL;
temp->next_score = NULL;
temp->prev_score = NULL;
start_name = temp;
start_score = temp;
}
else
{
for(i = start_name; i!=NULL; i=i->next_name)
{
if(strcmp(i->name, temp->n) > 0)
{
tempp = i->prev_name;
if(tempp == NULL)
temp->prev_name = NULL;
else
{
tempp->next_name = temp;
temp->prev_name = tempp;
}
temp->next_name = i;
i->prev_name = temp;
}
}
for(i = start_score; i!=NULL; i=i->next_score)
{
if(i->score > temp->n)
{
tempp = i->prev_score;
if(tempp == NULL)
temp->prev_score = NULL;
else
{
tempp->next_score = temp;
temp->prev_score = tempp;
}
temp->next_score = i;
i->prev_score = temp;
}
}
}
}
// Deletes the (n, score) pair from this Exam_database. If there is no
// pair with the name string, then this does nothing.
void remove_exam(string n)
{
struct node *tempp,*tempn,*i;
if (start_name == NULL)
{
return;
}
for(i=start_name; i!=NULL; i=i->next_name)
{
if(strcmp(i->name, n) > 0)
{
tempp = i->prev_name;
tempn = i->next_name;
if(tempp == NULL)
start_name = i->next_name;
else if(tempn == NULL)
tempp->next_name = NULL;
else
{
tempp->next_name = tempn;
tempn->prev_name = tempp;
}
sp = i->prev_score;
sn = i->next_score;
if(sp == NULL)
{
start_score = i->next_score;
start_score->prev_score = NULL;
}
else if(sn == NULL)
start->next_score = NULL;
else
{
sp->next_score = sn;
sn->prev_score = sp;
}
free(i);
return;
}
}
}
// Prints all (name, score) pairs in alphabetical order by name.
void print_by_name()
{
struct node *q;
if (start_name == NULL)
{
cout<<\"List empty,nothing to display\"<<endl;
return;
}
q = start_name;
cout<<\"Name \\t Score\"<<endl;
while (q != NULL)
{
cout<<q->name<<\"\\t\"<<q->score<<endl;
q = q->next_name;
}
}
// Prints all (name, score) pairs in reverse alphabetical order by name.
void print_by_name_rev()
{
struct node *p1, *p2, *start_reverse, *q;
p1 = start_name;
p2 = p1->next_name;
p1->next_name = NULL;
p1->prev_name = p2;
while (p2 != NULL)
{
p2->prev_name = p2->next_name;
p2->next_name = p1;
p1 = p2;
p2 = p2->prev_name;
}
start_reverse = p1;
if (start_reverse == NULL)
{
cout<<\"List empty,nothing to display\"<<endl;
return;
}
q = start_reverse;
cout<<\"Name \\t Score\"<<endl;
while (q != NULL)
{
cout<<q->name<<\"\\t\"<<q->score<<endl;
q = q->next_name;
}
}
// Prints all (name, score) pairs from smallest to biggest by score.
void print_by_score_ascending();
{
struct node *q;
if (start_score == NULL)
{
cout<<\"List empty,nothing to display\"<<endl;
return;
}
q = start_score;
cout<<\"Name \\t Score\"<<endl;
while (q != NULL)
{
cout<<q->name<<\"\\t\"<<q->score<<endl;
q = q->next_score;
}
}
// Prints all (name, score) pairs from biggest to smallest by score.
void print_by_score_descending();
{
struct node *p1, *p2, *start_reverse, *q;
p1 = start_score;
p2 = p1->next_score;
p1->next_score = NULL;
p1->prev_score = p2;
while (p2 != NULL)
{
p2->prev_score = p2->next_score;
p2->next_score = p1;
p1 = p2;
p2 = p2->prev_score;
}
start_reverse = p1;
if (start_reverse == NULL)
{
cout<<\"List empty,nothing to display\"<<endl;
return;
}
q = start_reverse;
cout<<\"Name \\t Score\"<<endl;
while (q != NULL)
{
cout<<q->name<<\"\\t\"<<q->score<<endl;
q = q->next_score;
}
}
}; // class Test_database







