Project: Build and process a sorted linked list. As en entry-level programmer yo
ID: 3661886 • Letter: P
Question
Project: Build and process a sorted linked list. As en entry-level programmer you have to be able to read, understand existing code and update it (add new features). One of this assignment’s goals is to read about 500 lines of code (in three files: CountryList.h, CountryList.cpp, 22C_Hw_1.cpp), compile and run the program, understand it, and change it as required. The existing program does the following:
A. Reads data from a text file (countries.txt) and inserts them into a sorted linked list. The list is to be sorted in ascending order by the country code named code (a unique key). The Country structure has four fields:
code (C-string) such as FR
name (string) France
capital (string) Paris
population (int) 60656178
B. Prints the list as a table with four columns (header included)
C. Deletes countries: prompts the user to enter the code of the country to be deleted or QUIT to stop deleting.
D. Searches the list: prompts the user to enter the code of the country or QUIT to stop searching; searches for that country: if found, displays its’ data, otherwise displays some message, such as “… Not found”
E. Destroys the list //no memory leak
YOUR TASK is to read and understand this program. Then do the following:
--> Replace the Country structure by a Country class (including setters, getters, etc.)
--> Replace the ListNode structure by a ListNode class (including setters, getters, etc.)
--> The code runs but it does not always work as expected. Find and fix as many (logical)
errors as you can. Make changes to improve the efficiency of the code.
--> Write a function to identify the country with the largest population
(you may assume it is unique).
--> Display data for the country with the largest population.
--> Write a short report (not more than one page) . In your report write what did you like about this program, what you didn’t like about the program, and briefly explain what did you do to make this program better.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
struct country{
public:
string code;
string name;
string capital;
int population;
country(string c,string n,string cap,int p){
code = c;
name = n;
capital = cap;
population = p;
}
};
struct Node{
public:
country* ctr;
Node* next;
};
struct Linklist{
public:
Node* head;
Linklist(){
head = NULL;
}
void insert_sorted(country* c){
Node* temp;
temp->ctr = c;
if (head == NULL || temp->ctr->code < head->ctr->code){
temp->next = head;
head = temp;
}
else{
Node* curr = head;
Node* prev = NULL;
while (curr != NULL && curr->ctr->code > temp->ctr->code){
prev = curr;
curr = curr->next;
}
prev->next = temp;
temp->next = curr;
}
}
void del(){
string code;
cout << "Enter the country to be delete : ";
cin >> code;
Node* curr = head;
Node* prev = NULL;
while (curr != NULL && curr->ctr->code == code){
prev = curr;
curr = curr->next;
}
if (curr == NULL)
cout << "NO SUCH COUNTRY EXIST " << endl;
else
prev->next = curr->next;
}
void print(){
Node* curr = head;
while (curr != NULL){
cout << curr->ctr->code << ' ' << curr->ctr->name << ' ' << curr->ctr->capital << ' ' << curr->ctr->population << endl;
curr = curr->next;
}
}
void search(){
string code;
cout << "Enter the country to be delete : ";
cin >> code;
Node* curr = head;
while (curr != NULL){
if (curr->ctr->code == code){
cout << curr->ctr->code << ' ' << curr->ctr->name << ' ' << curr->ctr->capital << ' ' << curr->ctr->population << endl;
return;
}
curr = curr->next;
}
cout << "NO SUCH COUNTRY EXIST " << endl;
}
};
int main(){
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.