-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserList.cpp
More file actions
89 lines (75 loc) · 1.77 KB
/
Copy pathUserList.cpp
File metadata and controls
89 lines (75 loc) · 1.77 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
#include "UserList.h"
#include <iostream>
using std::cout;
using std::endl;
/* initialize a new Node object that stores the given data */
void UserNode::init(const User user)
{
this->_data = user;
this->_next = nullptr;
}
// getters
UserNode* UserNode::get_next() const
{
return this->_next;
}
User UserNode::get_data() const
{
return this->_data;
}
// setters
void UserNode::set_next(UserNode* next)
{
this->_next = next;
}
void UserNode::set_data(const User new_data)
{
this->_data = new_data;
}
/** initialize an empty list */
void UserList::init()
{
this->_first = nullptr;
}
/** clears the list's memory */
void UserList::clear()
{
UserNode* temp;
while (this->_first != nullptr) // as long as the list is not empty
{
temp = this->_first; // temp points to the first element
this->_first = this->_first->get_next(); // the list now starts from the 2nd element
delete temp; // deletes the previous first element
}
}
// getters
UserNode* UserList::get_first() const
{
return this->_first;
}
// setters
void UserList::set_first(UserNode* first)
{
this->_first = first;
}
/** adds a User to the end of the list **/
bool UserList::add(const User new_user)
{
// create a new node to store the User object
UserNode* new_node = new UserNode;
new_node->init(new_user);
if (this->_first == nullptr) // list is empty
{
this->_first = new_node; // the new node is the first node
}
else // list has at least one element
{
UserNode* curr = this->_first; // start from first
while (curr->get_next() != nullptr) // advance to the last element
{
curr = curr->get_next();
}
curr->set_next(new_node); // add the new node at the end of the list
}
return true;
}