-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrec1Wynne.cpp
173 lines (152 loc) · 5.1 KB
/
rec1Wynne.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
164
165
166
167
168
169
170
171
172
173
#include<iostream>
#include<string>
using namespace std;
// Define the City struct
struct City {
string name; // City name
int population; // City population
int income; // Median household income
City* next; // Pointer to the next city
};
// Initialize the head and tail pointers
City* head = NULL;
City* tail = NULL;
// Function to add a city at the tail of the list
void addCityAtTail() {
// Get input from the user
string name;
int population, income;
cout << "Enter city name: ";
cin.ignore();
getline(cin, name);
cout << "Enter city population: ";
cin >> population;
cout << "Enter median household income: ";
cin >> income;
// Create a new city node
City* newCity = new City();
newCity->name = name;
newCity->population = population;
newCity->income = income;
newCity->next = NULL; // This will be the last node, so next is NULL
// If the list is empty, this new node is both the head and the tail
if(head == NULL) {
head = newCity;
tail = newCity;
} else { // Otherwise, add the new node to the end of the list
tail->next = newCity; // Update the old tail node to point to the new node
tail = newCity; // The new node is now the tail
}
}
// Function to add a city at the head of the list
void addCityAtHead() {
// Get input from the user
string name;
int population, income;
cout << "Enter city name: ";
cin.ignore();
getline(cin, name);
cout << "Enter city population: ";
cin >> population;
cout << "Enter median household income: ";
cin >> income;
// Create a new city node
City* newCity = new City();
newCity->name = name;
newCity->population = population;
newCity->income = income;
// If the list is empty, this new node is both the head and the tail
if(head == NULL) {
newCity->next = NULL;
head = newCity;
tail = newCity;
} else { // Otherwise, add the new node to the start of the list
newCity->next = head; // The new node points to the old head
head = newCity; // The new node is now the head
}
}
// Function to display the city with the largest population
void displayLargestCity() {
// If the head is NULL, the list is empty, so print "No city records" and return
if(head == NULL) {
cout << "No city records" << endl;
return;
}
// Initialize temporary pointer to the head of the list
City* temp = head;
// Initialize a pointer to keep track of the city with the largest population
City* largest = head;
// Traverse through the list
while(temp != NULL) {
// If the current city's population is larger than the largest seen so far,
// update the largest pointer
if(temp->population > largest->population) {
largest = temp;
}
// Move to the next city
temp = temp->next;
}
// Display the city with the largest population
cout << largest->name << ", " << largest->population << ", " << "$" << largest->income << endl;
}
// Function to display all cities in the list
void displayAllCities() {
// If the list is empty, print "No city records" and return
if(head == NULL) {
cout << "No city records" << endl;
return;
}
// Initialize a temporary pointer to the head of the list
City* temp = head;
// Traverse the list
while(temp != NULL) {
// Print the details of the current city
cout << temp->name << ", " << temp->population << ", " << "$" << temp->income << endl;
// Move to the next city
temp = temp->next;
}
}
int main() {
int option;
// Run the menu until the user chooses to exit (option 5)
do {
// Display the menu options
cout << "\n1. Add city record at the tail.\n";
cout << "2. Add city record at the head.\n";
cout << "3. Display the city with largest population.\n";
cout << "4. Display all the city records.\n";
cout << "5. Exit\n";
cout << "Enter your option: ";
// Take the user's choice
cin >> option;
// Perform an action based on the user's choice
switch(option) {
case 1:
// Add a new city at the tail of the list
addCityAtTail();
break;
case 2:
// Add a new city at the head of the list
addCityAtHead();
break;
case 3:
// Display the city with the largest population
displayLargestCity();
break;
case 4:
// Display all the cities in the list
displayAllCities();
break;
case 5:
// Exit the program
cout << "Exiting...\n";
break;
default:
// The user entered an invalid option
cout << "Invalid option. Try again...\n";
break;
}
// If the user chooses option 5, exit the loop and end the program
} while(option != 5);
return 0;
}