Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Data structure C++ Below is the whole program. compile and run the program, unde

ID: 3662011 • Letter: D

Question

Data structure C++

Below is the whole program. 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 do the following:

1. Replace the Country structure by a Country class (including setters, getters, etc.)

2. Replace the ListNode structure by a ListNode class (including setters, getters, etc.)

3. 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.

4. Write a function to identify the country with the largest population

       (you may assume it is unique).

5. Display data for the country with the largest population.

6. 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.

Homework.cpp:
#include
#include    // needed for the exit() function
#include
#include "CountryList.h"
using namespace std;

#define INPUT_FILE "countries.txt"

void loadCountries(CountryList &list, const char *filename);
void requestAction(CountryList &list);
void requestSearch(CountryList &list);
void requestDeletes(CountryList &list);

int main()
{
   CountryList list;

   loadCountries(list, INPUT_FILE);
   requestAction(list);

   return 0;
}

void loadCountries(CountryList &list, const char *filename)
{
    ifstream inputFile;
    Country newCountry;
    string temp;

    // open the input file
    inputFile.open(filename);
    if(!inputFile){
        cout << "Error opening file "" << filename << "" ";
        exit(1);
    }
    //Read each line of text into its own country object, then insert it into the list
    while(inputFile >> newCountry.code){
        //getline discards the delimiter ';' while preserving the space
        getline(inputFile, newCountry.name, ';');
        getline(inputFile, newCountry.capital, ';');
        //inputFile >> as opposed to getline to avoid string to int conversion
        inputFile >> newCountry.population;
        list.insertNode(newCountry);
    }
    inputFile.close();
}

void requestAction(CountryList &list)
{
    cout << "What would you like to do?" << endl;
    string input = "1";
    do{
        cout << "1: Display countries " << "2: Search for a country "
             << "3: Delete a country " << "4: exit" << endl;
        getline(cin, input);
        cout << endl;
        //If valid input, execute corresponding functionality"
        if(input == "1" || input == "2" || input == "3" || input == "4"){
            //Process input
            if(input == "1"){
                list.displayList();
                cout << "Number of countries in the list: " << list.getCount() << " ";
            }else if(input == "2"){
                requestSearch(list);
            }else if(input == "3"){
                requestDeletes(list);
                cout << "Number of countries in the list: " << list.getCount() << " ";
            }
        }else{
            cout << "Please enter either 1, 2, 3, or 4" << endl;
        }
        //4 corresponds with the quit option
    }while(input != "4");
}


void requestSearch(CountryList &list){
    string input;
    do{
        //stores the result of the list.findCountry function
        Country* country = NULL;
        cout << "Please enter the code of a country you wish to search for or " << endl;
        cout << "type QUIT to return to the previous page" << endl;
        getline(cin, input);
        cout << endl;
        country = list.findCountry(input.c_str());
        //if country is still null display error message. Ignore this check if user is trying to quit
        if(!country && input != "QUIT"){
            cout << "Could not find a country with code: " << input << endl;
        }
        //Display country information if list.findCountry returned a country struct
        if(country){
            cout << "Found a match!" << endl;
            cout << "Country code: " << country->code << endl;
            cout << "Country name: " << country->name << endl;
            cout << "Country capital: " << country->capital << endl;
            cout << "Country population: " << country->population << endl;
        }
        cout << endl;
    }while(input != "QUIT");
}

void requestDeletes(CountryList &list){
    string input;
    do{
        int errorCode;

        cout << "Please enter the code of the country you wish to delete or " << endl;
        cout << "type QUIT to return to the previous page" << endl;
        getline(cin, input);
        cout << endl;

        errorCode = list.deleteNode(input.c_str());

        if(errorCode == -1){
            cout << "There no countries left to delete!" << endl;
        }else if(errorCode == 0 && input != "QUIT"){
            cout << "Could not find country with code: " << input << endl;
        }else if(errorCode == 1){
            cout << input << " has been deleted" << endl << endl;
        }
        cout << endl;

    }while(input != "QUIT");
}

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Countrylist.cpp:


#include
#include "CountryList.h"
using namespace std;


CountryList::CountryList()
{
     head = new ListNode;
     head->next = NULL;
     cnt = 0;
}

void CountryList::displayList() const
{
   ListNode *nodePtr;

   nodePtr = head->next;

   //Display the header
   cout << setfill('-');
   cout << setw(6) << "Code";
   cout << setw(20) << "Country";
   cout << setw(20) << "Capital";
   cout << setw(15) << "Population" << endl;
   for(int i = 0; i < 61; i++) { cout << '-'; }

   cout << endl << setfill(' ');
   while (nodePtr){
      // Display the information in the current node.
      cout << setw(5) << nodePtr->country.code << " ";
      cout << setw(20) << nodePtr->country.name;
      cout << setw(20) << nodePtr->country.capital;
      cout << setw(15) << nodePtr->country.population << endl;
      // Move to the next node.
      nodePtr = nodePtr->next;
   }
   cout << endl;
}


void CountryList::insertNode(Country countryIn)
{
    ListNode *newNode;             // A new node
    ListNode *nodePtr;             // To traverse the list
    ListNode *previousNode = NULL; // The previous node

    // Allocate a new node and store the country there.
    newNode = new ListNode;
    newNode->country = countryIn;

    previousNode = head;
    nodePtr = head->next;
    // Find the location of the new node in the sorted list
    while (nodePtr != NULL && strcmp(nodePtr->country.code, countryIn.code) < 0){
        previousNode = nodePtr;
        nodePtr = nodePtr->next;
    }
    // Update links and counter
    previousNode->next = newNode;
    newNode->next = nodePtr;
    cnt++;
}


int CountryList::deleteNode(const char* code)
{
   ListNode *nodePtr;       // To traverse the list
   ListNode *previousNode; // To point to the previous node

   // check if list is empty
   if (cnt == 0)
      return -1; // -1 for empty list

   nodePtr = head->next;
   // Search for the node to be deleted from the list
   while (nodePtr != NULL && strcmp(nodePtr->country.code, code) != 0){
       previousNode = nodePtr;
       nodePtr = nodePtr->next;
   }
   if (nodePtr){ // found
       previousNode->next = nodePtr->next;
       delete nodePtr;
       return 1; // 1 for success
   }
   return 0; // 0 for not found
}


Country* CountryList::findCountry(const char* code){
    ListNode *nodePtr;       // To traverse the list
    ListNode *previousNode; // To point to the previous node

    // If the list is empty, return NULL
    if (cnt == 0)
        return NULL;

    nodePtr = head->next;
    previousNode = head;

    while (nodePtr != NULL && strcmp(nodePtr->country.code, code)){
        previousNode = nodePtr;
        nodePtr = nodePtr->next;
    }

    if (nodePtr){ // found
        return &nodePtr->country;
    }
   return NULL;
}


CountryList::~CountryList()
{
   ListNode *nodePtr;   // To traverse the list
   ListNode *nextNode; // To point to the next node

   // Position nodePtr at the head of the list.
   nodePtr = head;

   // While nodePtr is not at the end of the list...
   while (nodePtr != NULL){
      // Save a pointer to the next node.
      nextNode = nodePtr->next;
      // Delete the current node.
      delete nodePtr;
      // Position nodePtr at the next node.
      nodePtr = nextNode;
   }
}

,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Countrylist.h:

// Specification file for the CountryList class
#ifndef COUNTRYLIST_H
#define COUNTRYLIST_H

#include
#include
#include

using namespace std;

struct Country
{
    char code[3]; // 2 + 1 for ''
    string name;
    string capital;
    int population;
};

struct ListNode
{
    Country country;
    ListNode *next;
};

class CountryList
{
   private:
   ListNode *head; // pointer to the first node in the list
   int cnt;         // keeps track of the number of countries in the list

   public:
   CountryList();   // Constructor
    ~CountryList(); // Destructor

   // Basic Linked List Operations
   int getCount() { return cnt;}
   void insertNode(Country);
   int deleteNode(const char*);
   void displayList() const;
   Country* findCountry(const char*);

};
#endif

Explanation / Answer

main.cpp

#include <bits/stdc++.h>
#include "CountryList.h"
using namespace std;

void loadCountries(CountryList* list,const char *filename){
   ifstream inputFile;
   Country* newCountry;
   string temp;

   // open the input file
   inputFile.open(filename);
   if(!inputFile){
        cout << "Error opening file "" << filename << "" ";
        exit(1);
   }
   //Read each line of text into its own country object, then insert it into the list
   while(inputFile >> newCountry->code){
        //getline discards the delimiter ';' while preserving the space
        getline(inputFile, newCountry->name, ';');
        getline(inputFile, newCountry->capital, ';');
        //inputFile >> as opposed to getline to avoid string to int conversion
        inputFile >> newCountry->population;
        list->insertNode(newCountry);
   }
   inputFile.close();
}

void requestSearch(CountryList *list){
   string input;
   while (true){
       //stores the result of the list.findCountry function
       Country* country = NULL;
       cout << "Please enter the code of a country you wish to search for or type QUIT to return to the previous page" << endl;
       getline(cin, input);
       cout << endl;
       if (input == "QUIT") break;

       country = list->findCountry(input.c_str());
       //if country is still null display error message. Ignore this check if user is trying to quit
       if(country == NULL)
           cout << "Could not find a country with code: " << input << endl;
       //Display country information if list.findCountry returned a country struct
       else{
           cout << "Found a match!" << endl;
           cout << "Country code: " << country->code << endl;
           cout << "Country name: " << country->name << endl;
           cout << "Country capital: " << country->capital << endl;
           cout << "Country population: " << country->population << endl;
       }
       cout << endl;
   }
}

void requestDeletes(CountryList* list){
   string input;
   while (true){
       int errorCode;
       cout << "Please enter the code of the country you wish to delete or type QUIT to return to the previous page" << endl;
       getline(cin, input);
       cout << endl;
       if (input == "QUIT") break;

       errorCode = list->deleteNode(input.c_str());
       if(errorCode == -1)  
           cout << "There no countries left to delete!" << endl;
       else if(errorCode == 0)
           cout << "Could not find country with code: " << input << endl;
       else if(errorCode == 1)
           cout << input << " has been deleted" << endl << endl;
       cout << endl;
   }
}

void requestAction(CountryList* list){
   cout << "What would you like to do?" << endl;
   int input;
   while (true){
       cout << "1. Display Countries " << endl;
       cout << "2. Search for a Country " << endl;
       cout << "3. Delete a Country " << endl;
       cout << "4. Exit" << endl;
       cin >> input;
       if (input >= 1 && input <= 4){
           if (input == 1){
               list->displayList();
               cout << "Number of countries in the list: " << list->getCount() << endl << endl;
           }
           else if (input == 2)
               requestSearch(list);
           else if (input == 3){
               requestDeletes(list);
               cout << "Number of countries in the list: " << list->getCount() << endl << endl;
           }
           else if (input == 4)
               break;
       }
       else
           cout << "Please enter either 1, 2, 3, or 4" << endl;
   }
}

int main(){
   CountryList* LIST;
   string file = "country.txt";
   loadCountries(LIST,file.c_str());
   requestAction(LIST);
   return 0;
}



CountryList.cpp

#include <bits/stdc++.h>
#include "CountryList.h"
using namespace std;

CountryList::CountryList(){
   head = NULL;
   cnt = 0;
}

int CountryList::getCount(){
   return cnt;
}

void CountryList::displayList() const{
   ListNode* nodePtr = head;
   //Display the header
   cout << setfill('-');
   cout << setw(6) << "Code";
   cout << setw(20) << "Country";
   cout << setw(20) << "Capital";
   cout << setw(15) << "Population" << endl;
   for(int i = 0; i < 61; i++) { cout << '-'; }

   cout << endl << setfill(' ');
   while (nodePtr != NULL){
       cout << setw(5) << nodePtr->country->code << " ";
       cout << setw(20) << nodePtr->country->name;
       cout << setw(20) << nodePtr->country->capital;
       cout << setw(15) << nodePtr->country->population << endl;
       // Move to the next node.
       nodePtr = nodePtr->next;
   }
   cout << endl;
}

void CountryList::insertNode(Country* countryln){
   ListNode* prev = NULL;
   ListNode* curr = head;

   ListNode* newNode;
   newNode->country = countryln;
   while (curr != NULL && strcmp(curr->country->code,countryln->code) < 0){
       prev = curr;
       curr = curr->next;
   }
   prev->next = newNode;
   newNode->next = curr;
   cnt++;
}

int CountryList::deleteNode(const char* code){
   ListNode* prev = NULL;
   ListNode* curr = head;

   if (cnt == 0)   return -1;
   while (curr != NULL && strcmp(curr->country->code,code) != 0){
       prev = curr;
       curr = curr->next;
   }

   if (curr != NULL){
       prev->next = curr->next;
       delete curr;
       return 1;
   }
   return 0;
}

Country* CountryList::findCountry(const char* code){
   ListNode* prev = NULL;
   ListNode* curr = head;

   if (cnt == 0)
       return NULL;
   while (curr != NULL && strcmp(curr->country->code,code) != 0){
       prev = curr;
       curr = curr->next;  
   }

   if (curr != NULL){
       return curr->country;
   }
   return NULL;
}

CountryList::~CountryList(){
   ListNode* next = NULL;
   ListNode* curr = head;
   while (curr != NULL){
       next = curr->next;
       delete curr;
       curr = next;
   }  
}

CountryList.h

#ifndef COUNTRYLIST_H
#define COUNTRYLIST_H

using namespace std;

struct Country{
   char code[3]; // 2 + 1 for ''
   string name;
   string capital;
   int population;
};

struct ListNode{
   Country* country;
   ListNode *next;
};

struct CountryList{
   ListNode* head;
   int cnt;
   CountryList();
   ~CountryList();
   int getCount();
   void insertNode(Country*);
   int deleteNode(const char*);
   void displayList() const;
   Country* findCountry(const char*);
};

#endif