I have to write a program for class that uses a double linked list to store fake email messages that only contain a subject string. The user can enter the subject, hit enter, and then enter more subjects, or end. After end, the program prints each email subject and then a count of how many total emails there are and how many of each type of email there are too, like this.
Sample input:
Weather
Weather
Weather
Picnic
Picnic
Birthday Party
Picnic
Weather
Greetings
Birthday Party
Sample Run:
Inbox: total number of emails is 10.
Birthday Party – 2
Greetings - 1
Weather - 4
Picnic - 3
Here's where I'm at so far. I can't seem to figure out how to call the class fucntions from the main function. There are two commented off lines near the bottom.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
struct Mail {
public:
Mail* prev; //pointer to previous message
string Subject; //subject of message
int count; //number of messages with the same subject
Mail* next; //pointer to next message
};
class Inbox {
public:
Inbox(void) {head=NULL;} //constructor
~Inbox(void); //destructor
bool IsEmpty() {return head == NULL; }
int InsertEmail(string sub); //creates new correspondence or updates and existing correspondence
int DeleteCommunication(string sub); //deletes an existing correspondence
void DisplayInbox(void); //outputs the total number of messages then lists the different
//correspondence subjects as well as the number of messages
//in that correspondence
private:
int SearchCommunication(string sub); //finds a correspondence
Mail* head;
};
int Inbox::InsertEmail(string sub){
if (!SearchCommunication(sub)) //if the subject does not already exist
{
Mail* newMail = new Mail; //initiates a new correspondence node
newMail->Subject=sub;
newMail->next=head;
head=newMail;
newMail->prev=head;
return 1; //end in the case of new subject
}
Mail *currMail=head;
int currIndex=1;
int Index=SearchCommunication(sub);
while (currIndex != Index){ //tracks to the index of the existing correspondence
currMail=currMail->next;
currIndex++;
}
currMail->count++; //updates the number of messages with the same subject by 1
currMail->prev->next = currMail->next; //sets the updated subject to be the most recent correspondence
currMail->next->prev = currMail->prev;
currMail->next=head;
currMail->prev=NULL;
head=currMail;
return 1; //end in the case of existing subject
}
int Inbox::SearchCommunication(string Sub){
Mail* currMail=head;
int currIndex=1;
while (currMail && currMail->Subject != Sub){
currMail=currMail->next;
currIndex++;
}
if (currMail) return currIndex;
return 0;
}
int Inbox::DeleteCommunication(string Sub) {
if (SearchCommunication(Sub))
{
Mail *currMail=head;
int currIndex=1;
int Index=SearchCommunication(Sub);
while (currIndex != Index){
currMail=currMail->next;
currIndex++;
}
if (currMail->prev) {
currMail->next->prev = currMail->prev;
currMail->prev->next = currMail->next;
delete currMail;
}
else {
head = currMail->next;
currMail->next->prev = NULL;
delete currMail; //deletes the current correspondence
}
}
}
void Inbox::DisplayInbox(void){
Mail *currMail=head;
int number=0;
while(!head==NULL && !currMail->next==NULL){ //counts the total number of messages
number=number+currMail->count;
currMail=currMail->next;
}
cout << '\n' << "There are " << number << " total messages." << '\n';
currMail = head;
while(!head==NULL && !currMail->next==NULL){
cout << '\n' << currMail->Subject << " - " << currMail->count << '\n'; //outputs each individual subject
//and the number of messages within the subject
}
}
int main(){
string input;
while (input != "End")
{
input = " ";
cout << "Insert, Delete, Display, End?" <<endl;
cin >> input;
if (input == "Insert")
{
string Subject = " ";
cout << "Subject?" << endl;
cin >> Subject;
// int InsertEmail(Subject);
}
if (input == "Delete")
{
string Subject = " ";
cout << "Subject?" << endl;
cin >> Subject;
// int DeleteCommunication(Subject); // Passing a parameter for the DeleteCommunication
}
if (input == "Display")
{
void DisplayInbox();
}
}
return 0;
}
The commented out lines (123, 131, 135) appear as function prototypes to the compiler.
You need an instance of an Inbox to call the functions on.
After line 111 declare an Inbox. eg.Inbox ib;.
Line 123 then should be ib.InsertEmail(Subject);.
Likewise for line 131 and 135.