162 lines
5.3 KiB
C++
162 lines
5.3 KiB
C++
/*
|
|
CIS 22C: Homework 3
|
|
|
|
Build and procees a sorted linked list of Park objects.
|
|
The list is sorted in ascending order by the park code.
|
|
Assume that the park code is unique.
|
|
|
|
Written by: Iurii Tatishchev
|
|
Reviewed & Modified by: Iurii Tatishchev
|
|
IDE: CLion
|
|
*/
|
|
|
|
/*
|
|
*
|
|
Reads data about National Parks from a text file and inserts them into a sorted linked list. The list is sorted in ascending order by the unique park identifier. For instance, the unique park identifier for the Arches National Park is ARC.
|
|
Displays the list and the number of parks in the list.
|
|
Searches the list: prompts the user to enter a park code (i.e the unique park identifier), searches for that code: if found, displays related data, otherwise displays an error message, then searches again for another code, until the user enter “Q” to stop searching.
|
|
Deletes nodes from the list: prompts the user to enter a code, searches for that code: if found, it removes it from the list, otherwise displays an error message, then searches again for another code, until the user enter “Q” to stop deleting.
|
|
|
|
Read and understand this program (draw UML diagrams, the linked list, and hierarchy charts). Then do the following:
|
|
|
|
In main.cpp: provide calling statements for the basic linked list functions
|
|
Build and run the program: search will not work, but display and delete should work
|
|
In LinkedList.cpp: finish writing the searchList() function
|
|
Build and run the program: now search should work too
|
|
In Park.h and Park.cpp overload the stream insertion operator. The overloaded operator is going to replace the hDisplay() function in the Park class, and it is going to be used in LinkedList.cpp, in displayList() as shown below
|
|
Build and run the program
|
|
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include "LinkedList.h"
|
|
|
|
using namespace std;
|
|
|
|
void buildList(const string &filename, LinkedList &list);
|
|
|
|
void deleteManager(LinkedList &list);
|
|
|
|
void searchManager(const LinkedList &list);
|
|
|
|
void displayManager(const LinkedList &list);
|
|
|
|
int main() {
|
|
|
|
string inputFileName = "national_parks.txt";
|
|
LinkedList list;
|
|
|
|
buildList(inputFileName, list);
|
|
displayManager(list);
|
|
searchManager(list);
|
|
deleteManager(list);
|
|
displayManager(list);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
This function reads data about national parks from a file and inserts them
|
|
into a sorted linked list. The list is sorted in ascending order by code.
|
|
*/
|
|
void buildList(const string &filename, LinkedList &list) {
|
|
ifstream inputFile(filename);
|
|
cout << "Reading data from \"" << filename << "\"" << endl;
|
|
|
|
if (!inputFile) {
|
|
cout << "Error opening the input file: \"" << filename << "\"" << endl;
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
string line;
|
|
while (getline(inputFile, line)) {
|
|
int year;
|
|
string code, name, state, dsc;
|
|
|
|
stringstream temp(line); // create temp with data from line
|
|
temp >> code; // read from temp
|
|
temp >> state;
|
|
temp >> year;
|
|
temp.ignore(); // to ignore space in front of name
|
|
getline(temp, name, ';'); // stop reading name at ';'
|
|
temp.ignore(); // to ignore space in front of description
|
|
getline(temp, dsc);
|
|
// create a Park object and initialize it with data from file
|
|
Park aPark(code, state, name, dsc, year);
|
|
list.insertNode(aPark);
|
|
}
|
|
|
|
inputFile.close();
|
|
}
|
|
|
|
/*
|
|
Delete manager: delete items from the list until the user enters Q to quit
|
|
deleting
|
|
Input Parameter: list
|
|
*/
|
|
void deleteManager(LinkedList &list) {
|
|
string targetCode = "";
|
|
|
|
cout << endl << " Delete" << endl;
|
|
cout << "=======" << endl;
|
|
|
|
while (targetCode != "Q") {
|
|
cout << "Enter a park code (or Q to stop deleting):" << endl;
|
|
cin >> targetCode;
|
|
|
|
if (targetCode != "Q") {
|
|
if (list.deleteNode(targetCode))
|
|
cout << " " << targetCode << " has been deleted!" << endl;
|
|
else
|
|
cout << "Park \"" << targetCode << "\" was not found in this list." << endl;
|
|
}
|
|
}
|
|
cout << "___________________END DELETE SECTION_____" << endl;
|
|
}
|
|
|
|
/*
|
|
Search manager: search the list until the user enters Q to quit searching
|
|
Input Parameter: list
|
|
*/
|
|
void searchManager(const LinkedList &list) {
|
|
string targetCode = "";
|
|
Park aPark;
|
|
|
|
cout << endl << " Search" << endl;
|
|
cout << "=======" << endl;
|
|
|
|
while (targetCode != "Q") {
|
|
cout << "Enter a park code (or Q to stop searching):" << endl;
|
|
cin >> targetCode;
|
|
|
|
if (targetCode != "Q") {
|
|
if (list.searchList(targetCode, aPark))
|
|
aPark.vDisplay();
|
|
else
|
|
cout << "Park \"" << targetCode << "\" was not found in this list." << endl;
|
|
}
|
|
}
|
|
cout << "___________________END SEARCH SECTION _____" << endl;
|
|
}
|
|
|
|
/*
|
|
Display manager:
|
|
- displays the number of national parks in this list
|
|
- calls the displayList() function upon request
|
|
Input Parameter: list
|
|
*/
|
|
void displayManager(const LinkedList &list) {
|
|
string action;
|
|
|
|
cout << "Number of National Parks in this list: " << list.getLength() << endl;
|
|
|
|
cout << "\nDisplay list [Y/N]? ";
|
|
cin >> action;
|
|
if (action == "Y" || action == "y") {
|
|
cout << endl;
|
|
list.displayList();
|
|
}
|
|
}
|