-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserList.cpp
More file actions
123 lines (106 loc) · 2.53 KB
/
UserList.cpp
File metadata and controls
123 lines (106 loc) · 2.53 KB
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
#include "UserNode.h"
#include "UserList.h"
#include <string>
#include <iostream>
// Constructors
UserList::UserList ()
{
Head = NULL;
Number_user = 0;
Total_message = 0;
}
// copy Constructors
UserList::UserList (const UserList & n)
{
Head = n.Head;
Number_user = n.Number_user;
Total_message = n.Total_message;
}
// Destructors
UserList::~UserList ()
{
//empty
}
void UserList::Add (string firstName, string lastName, string handle,long int phoneNumber, int messageCount)
{
// ADD tail node
Number_user++;
Total_message += messageCount;
UserNode *ptr = Head;
while ((ptr != NULL) && (ptr->getNext () != NULL))
ptr = ptr->getNext ();
// Insert new node
if (ptr != NULL)
{
ptr->setNext (new UserNode(firstName, lastName, handle, phoneNumber,messageCount));
}
else
Head =new UserNode (firstName, lastName, handle, phoneNumber, messageCount);
}
//--------------------------------------------------------------------------------
UserNode * UserList::Update (string first_name)
{
// Find matching node
UserNode *ptr = Head;
if (ptr == NULL){
cout << "User Not found" << endl;
cout <<"Please Input the correct spelling of the Name or add user information"<< endl;
}else
{
while ((ptr != NULL) && (ptr->getFirstName () != first_name))
{
ptr = ptr->getNext ();
}
}
// if pointer is not null Add to current message Count
if (ptr != NULL)
{
ptr->setMessageCount (1 + ptr->getMessageCount ());
Total_message++;
ptr->print ();
return ptr;
}
else
{
return NULL;
}
}
//---------------------------------------------------------------------------------
void UserList::Find ()
{
UserNode *ptr = Head;
UserNode *FINDMAX = Head;
// find max while the pointer is not null
while (ptr != NULL)
{
if (ptr->getMessageCount () > FINDMAX->getMessageCount ())
FINDMAX = ptr;
ptr = ptr->getNext ();
}
//print out the max at that location
if (FINDMAX != NULL)
{
FINDMAX->print ();
}
else
cout << "User Not found" << endl;
}
//---------------------------------------------------------------------------------
void UserList::printAll () const
{
UserNode *ptr = Head;
if (ptr == NULL)
{
cout << "Nothing to print out, Please type 1 to add Users" << endl<<endl;
}
else
{
while (ptr != NULL)
{
ptr->print ();
ptr = ptr->getNext ();
}
}
cout << "Total message:" << Total_message << endl;
cout << "Number users:" << Number_user << endl;
}