-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountryList.cpp
103 lines (76 loc) · 2.31 KB
/
CountryList.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
#include "CountryList.h"
void CountryListPrint(CountryNode * head){
CountryNode * current = head;
cout << "Printing the list: " << endl;
int i = 1;
while (current != NULL) {
cout << "Country " << i++ << ": " << current->country << endl;
current = current->next;
}
cout << endl;
}
void CountryListPrintInFile(CountryNode * head, ofstream& logFile){
CountryNode * current = head;
int i = 1;
while (current != NULL) {
logFile << current->country << endl;
current = current->next;
}
}
/* Adding an item to the beginning of the list (pushing to the list) */
void CountryListPush(CountryNode ** head, string country){
CountryNode * new_node = new(CountryNode);
/* Update data */
new_node->country = country;
new_node->next = *head;
/* Now the new CountryNode will be the list's head */
*head = new_node;
}
bool CountryListSearch(CountryNode* head, string countryName){
CountryNode* current = head; // Initialize current
while (current != NULL)
{
if (current->country.compare(countryName) == 0)
return true;
current = current->next;
}
return false;
}
string* CountryListSearch2(CountryNode* head, string countryName){
CountryNode* current = head; // Initialize current
while (current != NULL)
{
if (current->country.compare(countryName) == 0)
return &(current->country);
current = current->next;
}
return NULL;
}
/* Counts no. of nodes in linked list */
int CountryListCount(CountryNode* head){
CountryNode* current = head;
int counter = 0;
while (current != NULL) {
current = current->next;
counter++;
}
return counter;
}
void CountryDeleteList(CountryNode** head){
CountryNode* current = *head; // Initialize current
CountryNode* temp = NULL;
while (current != NULL) {
temp = current;
current = current->next;
delete temp;
}
}
string getAllCountries(CountryNode* head, string inputDir){
CountryNode* current = head;
string allCountries = "";
while (current != NULL) {
allCountries = allCountries + inputDir + "/" + current->country + " ";
current = current->next;
}
return allCountries;
}