Help with linked list functions

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <iostream>
#include <string>
#include <sstream>
using namespace 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;
}
closed account (D80DSL3A)
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.
Thank you very much.

Also, now that I got that working, when I input "Display", nothing happens. It seems to not go through the Inbox::DisplayInbox function.

Thanks,
Matt
closed account (D80DSL3A)
The conditions in the while loops on lines 98 and 104 look wrong.
I'm not sure what !head==NULL would evaluate to.

I think the condition in both loops should be simply while( currMail ).
Topic archived. No new replies allowed.