Data Structure C++ Change the below program to process a circularly doubly-linke
ID: 3661974 • Letter: D
Question
Data Structure C++
Change the below program to process a circularly doubly-linked list
with a sentinel node. Demonstrate that both links work by printing the list both forwards
and backwards.
Run each program once and save the output at the end of the source file as a comment.
Below is the whole code. Please Copy and Pase the program.
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
#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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.