-
Notifications
You must be signed in to change notification settings - Fork 82
/
phonebookapp.cpp
163 lines (154 loc) · 4.37 KB
/
phonebookapp.cpp
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <string>
using namespace std;
struct node
{
string firstName;
string lastName;
string number;
node *next;
};
class List
{
private:
node *head, *tail;
public:
List()
{
head = NULL;
tail = NULL;
}
void create_contact(string first, string last, string num)
{
node *temp = new node;
temp->firstName = first;
temp->lastName = last;
temp->number=num;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
void display_all() // Prints out current trip
{
int contactNum = 1;
node *temp = new node;
temp = head;
while (temp != NULL) // Loop through the list while the temporary node is not empty
{
cout << "\nContact Number: " << contactNum << endl;
cout << "First Name: " << temp->firstName << endl;
cout << "Last Name: " << temp->lastName << endl;
cout << "Phone number: "<<temp->number<<endl;
++contactNum;
temp = temp->next;
}
}
void search_name(string first,string last)
{
node *temp=head;
int i=1;
while(temp!=NULL)
{
if(temp->firstName==first && temp->lastName==last)
{
cout << "\nContact Number: " << i << endl;
cout << "First Name: " << temp->firstName << endl;
cout << "Last Name: " << temp->lastName << endl;
cout << "Phone number: "<<temp->number<<endl;
break;
}
temp = temp->next;
i++;
if(temp==NULL)
cout<<"Contact could not be found"<<endl;
}
}
void delete_position(int pos) // delete a stop by using the position in the list
{
node *current = new node;
node *previous = new node;
node *next = new node;
current = head;
for (int i = 1;i<pos;i++) // Loop through the link list while the current node is not empty
{
if (current == NULL)
return;
previous = current;
current = current->next;
}
next = current->next;
previous->next = current->next;
delete current;
}
void delete_head() // delete head node
{
node *temp = new node;
temp = head;
head = head->next;
delete temp;
}
};
int main()
{
List Contacts; // create a Contacts item for the List class
int choice, position;
string firstName;
string lastName;
string number;
while (1) {
cout << "\nWhat would you like to do?: " << endl;
cout << "1. Show All Contacts" << endl;
cout << "2. Add A Contact" << endl;
cout << "3. Remove A Contact" << endl;
cout << "4. Search Contacts " << endl;
cout << "5. Exit The Program" << endl;
cin >> choice;
switch (choice)
{
case 1:
cout << endl;
Contacts.display_all(); // display all contacts
cout << endl;
break;
case 2:
cout << "\nEnter your first name: ";
cin >> firstName;
cout << "\nEnter your last name: ";
cin >> lastName;
cout <<"\nEnter your phone number: ";
cin >>number;
Contacts.create_contact(firstName, lastName,number); // create the contact in the linked list
cout << endl;
break;
case 3:
cout << "Enter the contact number of the contact you would like to remove: ";
cin >> position;
if (position == 1)
Contacts.delete_head();
else
Contacts.delete_position(position); // delete contact from list
break;
case 4 :
cout << "Enter the contact name of the contact you would like to visit : ";
cin>>firstName>>lastName;
Contacts.search_name(firstName,lastName);
break;
case 5:
exit(1);
break;
default:
cout << "\n" << choice << " is not an option. Please select a valid option." << endl;
break;
}
}
return 0;
}