Repository navigation
Expand file tree
/
Copy pathcontactlist.cpp
More file actions
143 lines (130 loc) · 4.19 KB
/
Copy pathcontactlist.cpp
File metadata and controls
143 lines (130 loc) · 4.19 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* CIS 22B
*
* Lab4: A contact list that is implemented using a linked list
*
* Author: Rose Osterheld and Maren Alessandri Lane
*
* Group 4: Rose Osterheld, Maren Alessandri Lane, Alex Hagemeister
*
* Date: 03/07/2021
*/
#include "contact.hpp"
#include <iostream>
class PhoneBook : List
{
private:
Node *head;
/**
* helper function for the destructor
*/
void freeList(Node *currentNode)
{
// finish if the current node is null
if (currentNode == nullptr)
return;
// recursively traverse through list to delete every node
freeList(currentNode->next);
delete currentNode;
}
public:
/**
* Head is always initialized to nullptr. Use insert to add the first
* contact
*/
PhoneBook() : head(nullptr) {}
void insert(
const Contact contact) // function called to add a contact to the list
{
Node *tempNode; // node to temporarily hold the moving contact
Node *nodeToInsert = new Node(contact); // the new contact being added
Node *currentNode = head; // the contact being moved
if (head == nullptr) // if there are no contacts yet, the first contact
// becomes the head
{
head = nodeToInsert;
return;
}
// make sure we check against head before traversing through the list
else if (head->data.lastName > nodeToInsert->data.lastName)
{
tempNode = head;
head = nodeToInsert;
nodeToInsert->next = tempNode;
return;
}
while (true)
{
// append if we're at the end of the list
if (currentNode->next == nullptr)
{
tempNode = currentNode->next;
currentNode->next = nodeToInsert;
nodeToInsert->next = tempNode;
return;
}
if (currentNode->next->data.lastName >
nodeToInsert->data.lastName) // searches the list to put the new
// contact in alphabetically
{
tempNode = currentNode->next; // temporarily holds the moving
// node do it doesn't get deleted
// inserts the new contact in alphabetically
currentNode->next = nodeToInsert;
// puts the moving contact back into the list
nodeToInsert->next = tempNode;
return;
}
currentNode = currentNode->next;
}
}
void deleteContact(
string name) // function called to delete a contact in the list
{
int pos = name.find(" "); // position of the first space in name
string firstName = name.substr(0, pos); // get up to the first space
string lastName =
name.substr(pos + 1); // get from first space to the end
Node *currentNode = head;
if (head == nullptr) // exit if list is empty
return;
// check if we need to remove head before traversing
if (head->data.firstName == firstName &&
head->data.lastName == lastName)
{
Node *tmp = head;
head = head->next;
delete tmp;
return;
}
// traverse the list to find the correct node
while (currentNode->next != nullptr)
{
if (currentNode->next->data.firstName == firstName &&
currentNode->next->data.lastName == lastName)
{
Node *tmp = currentNode->next;
currentNode->next = currentNode->next->next;
delete tmp;
return;
}
currentNode = currentNode->next;
}
}
/**
* Recursively deletes each node to prevent a memory link
*/
~PhoneBook() { freeList(head); }
/**
* Prints every person in the phonebook
*/
void print() const
{
Node *currentNode = head;
while (currentNode != nullptr)
{
cout << currentNode->data << endl;
currentNode = currentNode->next;
}
}
};