This C++ program implements a linked list to store and manipulate city data. It reads city information from a text file into a linked list, prints the list, allows the user to search for and delete cities, and includes functions for initialization and cleanup. The main function calls functions to welcome the user, read the data file, print the list, search the list, delete from the list, and say goodbye. Additional functions implement linked list operations like insertion, searching and deletion.
1. #include
#include
#include
#include "CityList.h"
using namespace std;
void welcome();
void readCityData(CityList *readList);
void printCityData(CityList *printList);
void searchCityData(CityList *searchList);
void deleteCityData(CityList *deleteList);
void goodbye();
int main()
{
CityList list;
// WELCOME FUNCTION
welcome();
// A. Read data from a text file
readCityData(&list);
// B. Print the list
printCityData(&list);
// C. Seach Function
searchCityData(&list);
// D. Delete Function
deleteCityData(&list);
// EXTRA. Check the Result (Inactive)
// list.displayList();
// GOODBYE FUNCTION
goodbye();
2. return 0;
}
//****************************************************************************
*
// Definition of function welcome
// This function displays the welcome screen
//****************************************************************************
*
void welcome()
{
cout << "Welcome to the Linked Lists Project" << endl;
cout << "___________________________________" << endl << endl;
}
//****************************************************************************
*
// Definition of function readCityData
// This function save the city list from the file to the linked list
//****************************************************************************
*
void readCityData(CityList *readList)
{
cout << endl << "A. Read City Data" << endl;
cout << "___________________" << endl;
Data import;
ifstream infile;
infile.open("cities.txt");
while(!infile)
{
cout << "FILE OPEN ERROR" << endl;
exit(111);
}
4. endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(20) << "----------" << endl;
printList->displayList();
cout << "___________________" << endl;
}
//****************************************************************************
*
// Definition of function searchCityData
// This function search the city from the city data
//****************************************************************************
*
void searchCityData(CityList * searchList)
{
string searchCity;
cout << endl << "C. Search City Data" << endl;
cout << "___________________" << endl;
while(searchCity!="QUIT")
{
cout << endl << "Enter the city for search (QUIT for stop searching) : ";
getline(cin,searchCity);
if(searchCity!="QUIT")
{
searchList->searchList(searchCity);
}
}
cout << "___________________" << endl;
}
//****************************************************************************
*
// Definition of function deleteCityData
// This function delete the city from the city data
5. //****************************************************************************
*
void deleteCityData(CityList * deleteList)
{
string searchCity;
cout << endl << endl << "D. Delete City Data" << endl;
cout << "___________________" << endl;
while(searchCity!="QUIT")
{
cout << endl << "Enter the city for delete (QUIT for stop searching) : ";
getline(cin,searchCity);
if(searchCity!="QUIT")
{
deleteList->deleteNode(searchCity);
}
}
}
//****************************************************************************
*
// Definition of function goodbye
// This function displays the goodbye screen
//****************************************************************************
*
void goodbye()
{
cout << endl << endl << "______________________________________________" <<
endl << endl;
cout << "Thank you for using the Linked Lists Project" << endl;
}
CityList.h
// Specification file for the CityList class
6. #ifndef CITYLIST_H
#define CITYLIST_H
#include
using namespace std;
struct Data
{
string state;
int year;
string city;
// other fields could be added here
};
class CityList
{
private:
// Declare a structure for the list
struct ListNode
{
//double value; // The value in this node
Data data;
ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
// Constructor
CityList()
{ head = NULL; }
// Destructor
~CityList();
// Linked list operations
void insertNode(Data); //void insertNode(double);
void deleteNode(string);
void searchList(string) const;
void displayList() const;
};
#endif
7. CityList.cpp
// Implementation file for the CityList class
#include // For cout and NULL
#include
#include "CityList.h"
using namespace std;
//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************
void CityList::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << setw(10) << nodePtr->data.state;
cout << setw(10) << nodePtr->data.year;
cout << setw(20) << nodePtr->data.city << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
//**************************************************
// The insertNode function inserts a node with *
// data copied to its data member. *
//**************************************************
void CityList::insertNode(Data dataIn)
8. {
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = NULL; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->data.state = dataIn.state;
newNode->data.year = dataIn.year;
newNode->data.city = dataIn.city;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to NULL.
previousNode = NULL;
// Skip all nodes whose value is less than num.
while (nodePtr != NULL && nodePtr->data.city < dataIn.city)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == NULL)
{
head = newNode;
newNode->next = nodePtr;
}
9. else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//**************************************************
// The searchList function searches for a node *
// with city as its value. The node, if found, is *
// print from the list. *
//**************************************************
void CityList::searchList(string city) const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node and
// whose value member is not equal to city traverse
// the list.
while (nodePtr != NULL && nodePtr->data.city!=city)
{
// Move to the next node.
nodePtr = nodePtr->next;
}
// Display the value in this node.
if(nodePtr != NULL && nodePtr->data.city==city)
{
cout << endl << left << setw(10) << "State" << setw(10) << "Year" << setw(20) <<
"City" << endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(20) << "----------" << endl;
cout << setw(10) << nodePtr->data.state;
cout << setw(10) << nodePtr->data.year;
10. cout << setw(20) << nodePtr->data.city << endl;
}
// Display error if there is no result in this node
else
{
cout << "<" << city << ">" << " was not found" << endl;
}
}
//**************************************************
// The deleteNode function searches for a node *
// with city as its value. The node, if found, is *
// deleted from the list and from memory. *
//**************************************************
void CityList::deleteNode(string city)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
//if (head->value == num)
if (head->data.city == city)
{
cout << endl << left << setw(10) << "State" << setw(10) << "Year" << setw(20) <<
"City" << endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(20) << "----------" << endl;
cout << setw(10) << head->data.state;
cout << setw(10) << head->data.year;
cout << setw(20) << head->data.city << endl;
cout << "** Delete the " << city << " **" << endl;
nodePtr = head->next;
11. delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to city.
while (nodePtr != NULL && nodePtr->data.city != city)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
cout << endl << left << setw(10) << "State" << setw(10) << "Year" << setw(20) <<
"City" << endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(20) << "----------" << endl;
cout << setw(10) << nodePtr->data.state;
cout << setw(10) << nodePtr->data.year;
cout << setw(20) << nodePtr->data.city << endl;
cout << "** Delete the " << city << " **" << endl;
previousNode->next = nodePtr->next;
delete nodePtr;
}
// Display error if there is no result in this node
else
{
cout << "<" << city << ">" << " was not found" << endl;
}
12. }
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
CityList::~CityList()
{
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;
}
}
cities.txt
TX 1837 San Antonio
CA 1850 Los Angeles
TX 1856 Dallas
AZ 1881 Phoenix
IL 1837 Chicago
PA 1701 Philadelphia
OH 1834 Columbus
WI 1846 Milwaukee
NY 1898 New York
IN 1832 Indianapolis
13. MD 1797 Baltimore
DC 1788 Washington
FL 1822 Jacksonville
TN 1826 Memphis
TX 1837 Huston
CA 1850 San Diego
CA 1850 San Jose
MI 1815 Detroit
CA 1850 San Francisco
MA 1822 Boston
Sample output
Welcome to the Linked Lists Project
___________________________________
A. Read City Data
___________________
Complete to read data
___________________
B. Print City Data
___________________
State Year City
----- ----- ----------
MD 1797 Baltimore
MA 1822 Boston
IL 1837 Chicago
OH 1834 Columbus
TX 1856 Dallas
MI 1815 Detroit
TX 1837 Huston
IN 1832 Indianapolis
14. FL 1822 Jacksonville
CA 1850 Los Angeles
TN 1826 Memphis
WI 1846 Milwaukee
NY 1898 New York
PA 1701 Philadelphia
AZ 1881 Phoenix
TX 1837 San Antonio
CA 1850 San Diego
CA 1850 San Francisco
CA 1850 San Jose
DC 1788 Washington
___________________
C. Search City Data
___________________
Enter the city for search (QUIT for stop searching) : Baltimore
State Year City
----- ----- ----------
MD 1797 Baltimore
Enter the city for search (QUIT for stop searching) : Washington
State Year City
----- ----- ----------
DC 1788 Washington
Enter the city for search (QUIT for stop searching) : Memphis
State Year City
----- ----- ----------
TN 1826 Memphis
Enter the city for search (QUIT for stop searching) : Cupertino
15. was not found
Enter the city for search (QUIT for stop searching) : QUIT
___________________
D. Delete City Data
___________________
Enter the city for delete (QUIT for stop searching) : Baltimore
State Year City
----- ----- ----------
MD 1797 Baltimore
** Delete the Baltimore**
Enter the city for delete (QUIT for stop searching) : Baltimore
was not found
Enter the city for delete (QUIT for stop searching) : Washington
State Year City
----- ----- ----------
DC 1788 Washington
** Delete the Washington**
Enter the city for delete (QUIT for stop searching) : Washington
was not found
Enter the city for delete (QUIT for stop searching) : Cupertino
was not found
Enter the city for delete (QUIT for stop searching) : QUIT
______________________________________________
16. Thank you for using the Linked Lists Project
Solution
#include
#include
#include
#include "CityList.h"
using namespace std;
void welcome();
void readCityData(CityList *readList);
void printCityData(CityList *printList);
void searchCityData(CityList *searchList);
void deleteCityData(CityList *deleteList);
void goodbye();
int main()
{
CityList list;
// WELCOME FUNCTION
welcome();
// A. Read data from a text file
readCityData(&list);
// B. Print the list
printCityData(&list);
// C. Seach Function
searchCityData(&list);
// D. Delete Function
deleteCityData(&list);
// EXTRA. Check the Result (Inactive)
// list.displayList();
17. // GOODBYE FUNCTION
goodbye();
return 0;
}
//****************************************************************************
*
// Definition of function welcome
// This function displays the welcome screen
//****************************************************************************
*
void welcome()
{
cout << "Welcome to the Linked Lists Project" << endl;
cout << "___________________________________" << endl << endl;
}
//****************************************************************************
*
// Definition of function readCityData
// This function save the city list from the file to the linked list
//****************************************************************************
*
void readCityData(CityList *readList)
{
cout << endl << "A. Read City Data" << endl;
cout << "___________________" << endl;
Data import;
ifstream infile;
infile.open("cities.txt");
while(!infile)
{
cout << "FILE OPEN ERROR" << endl;
18. exit(111);
}
while(infile>>import.state)
{
//infile >> import.state;
infile >> import.year;
infile.ignore();
getline(infile, import.city);
// DEBUG //
/*
cout << import.state;
cout << import.year;
cout << import.city;
cout << endl;
*/
readList->insertNode(import);
}
infile.close();
cout << endl << "Complete to read data" << endl;
cout << "___________________" << endl;
}
//****************************************************************************
*
// Definition of function printCityData
// This function displays the city list
//****************************************************************************
*
void printCityData(CityList * printList)
{
19. cout << endl << endl << "B. Print City Data" << endl;
cout << "___________________" << endl << endl;
cout << left << setw(10) << "State" << setw(10) << "Year" << setw(20) << "City" <<
endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(20) << "----------" << endl;
printList->displayList();
cout << "___________________" << endl;
}
//****************************************************************************
*
// Definition of function searchCityData
// This function search the city from the city data
//****************************************************************************
*
void searchCityData(CityList * searchList)
{
string searchCity;
cout << endl << "C. Search City Data" << endl;
cout << "___________________" << endl;
while(searchCity!="QUIT")
{
cout << endl << "Enter the city for search (QUIT for stop searching) : ";
getline(cin,searchCity);
if(searchCity!="QUIT")
{
searchList->searchList(searchCity);
}
}
cout << "___________________" << endl;
}
20. //****************************************************************************
*
// Definition of function deleteCityData
// This function delete the city from the city data
//****************************************************************************
*
void deleteCityData(CityList * deleteList)
{
string searchCity;
cout << endl << endl << "D. Delete City Data" << endl;
cout << "___________________" << endl;
while(searchCity!="QUIT")
{
cout << endl << "Enter the city for delete (QUIT for stop searching) : ";
getline(cin,searchCity);
if(searchCity!="QUIT")
{
deleteList->deleteNode(searchCity);
}
}
}
//****************************************************************************
*
// Definition of function goodbye
// This function displays the goodbye screen
//****************************************************************************
*
void goodbye()
{
cout << endl << endl << "______________________________________________" <<
endl << endl;
21. cout << "Thank you for using the Linked Lists Project" << endl;
}
CityList.h
// Specification file for the CityList class
#ifndef CITYLIST_H
#define CITYLIST_H
#include
using namespace std;
struct Data
{
string state;
int year;
string city;
// other fields could be added here
};
class CityList
{
private:
// Declare a structure for the list
struct ListNode
{
//double value; // The value in this node
Data data;
ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
// Constructor
CityList()
{ head = NULL; }
// Destructor
~CityList();
// Linked list operations
void insertNode(Data); //void insertNode(double);
void deleteNode(string);
22. void searchList(string) const;
void displayList() const;
};
#endif
CityList.cpp
// Implementation file for the CityList class
#include // For cout and NULL
#include
#include "CityList.h"
using namespace std;
//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************
void CityList::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << setw(10) << nodePtr->data.state;
cout << setw(10) << nodePtr->data.year;
cout << setw(20) << nodePtr->data.city << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
//**************************************************
23. // The insertNode function inserts a node with *
// data copied to its data member. *
//**************************************************
void CityList::insertNode(Data dataIn)
{
ListNode *newNode; // A new node
ListNode *nodePtr; // To traverse the list
ListNode *previousNode = NULL; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->data.state = dataIn.state;
newNode->data.year = dataIn.year;
newNode->data.city = dataIn.city;
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else // Otherwise, insert newNode
{
// Position nodePtr at the head of list.
nodePtr = head;
// Initialize previousNode to NULL.
previousNode = NULL;
// Skip all nodes whose value is less than num.
while (nodePtr != NULL && nodePtr->data.city < dataIn.city)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == NULL)
24. {
head = newNode;
newNode->next = nodePtr;
}
else // Otherwise insert after the previous node.
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
//**************************************************
// The searchList function searches for a node *
// with city as its value. The node, if found, is *
// print from the list. *
//**************************************************
void CityList::searchList(string city) const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node and
// whose value member is not equal to city traverse
// the list.
while (nodePtr != NULL && nodePtr->data.city!=city)
{
// Move to the next node.
nodePtr = nodePtr->next;
}
// Display the value in this node.
if(nodePtr != NULL && nodePtr->data.city==city)
{
cout << endl << left << setw(10) << "State" << setw(10) << "Year" << setw(20) <<
25. "City" << endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(20) << "----------" << endl;
cout << setw(10) << nodePtr->data.state;
cout << setw(10) << nodePtr->data.year;
cout << setw(20) << nodePtr->data.city << endl;
}
// Display error if there is no result in this node
else
{
cout << "<" << city << ">" << " was not found" << endl;
}
}
//**************************************************
// The deleteNode function searches for a node *
// with city as its value. The node, if found, is *
// deleted from the list and from memory. *
//**************************************************
void CityList::deleteNode(string city)
{
ListNode *nodePtr; // To traverse the list
ListNode *previousNode; // To point to the previous node
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
//if (head->value == num)
if (head->data.city == city)
{
cout << endl << left << setw(10) << "State" << setw(10) << "Year" << setw(20) <<
"City" << endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(20) << "----------" << endl;
cout << setw(10) << head->data.state;
cout << setw(10) << head->data.year;
26. cout << setw(20) << head->data.city << endl;
cout << "** Delete the " << city << " **" << endl;
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;
// Skip all nodes whose value member is
// not equal to city.
while (nodePtr != NULL && nodePtr->data.city != city)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
cout << endl << left << setw(10) << "State" << setw(10) << "Year" << setw(20) <<
"City" << endl;
cout << setw(10) << "-----" << setw(10) << "-----" << setw(20) << "----------" << endl;
cout << setw(10) << nodePtr->data.state;
cout << setw(10) << nodePtr->data.year;
cout << setw(20) << nodePtr->data.city << endl;
cout << "** Delete the " << city << " **" << endl;
previousNode->next = nodePtr->next;
delete nodePtr;
}
// Display error if there is no result in this node
27. else
{
cout << "<" << city << ">" << " was not found" << endl;
}
}
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
CityList::~CityList()
{
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;
}
}
cities.txt
TX 1837 San Antonio
CA 1850 Los Angeles
TX 1856 Dallas
AZ 1881 Phoenix
IL 1837 Chicago
PA 1701 Philadelphia
28. OH 1834 Columbus
WI 1846 Milwaukee
NY 1898 New York
IN 1832 Indianapolis
MD 1797 Baltimore
DC 1788 Washington
FL 1822 Jacksonville
TN 1826 Memphis
TX 1837 Huston
CA 1850 San Diego
CA 1850 San Jose
MI 1815 Detroit
CA 1850 San Francisco
MA 1822 Boston
Sample output
Welcome to the Linked Lists Project
___________________________________
A. Read City Data
___________________
Complete to read data
___________________
B. Print City Data
___________________
State Year City
----- ----- ----------
MD 1797 Baltimore
MA 1822 Boston
IL 1837 Chicago
OH 1834 Columbus
29. TX 1856 Dallas
MI 1815 Detroit
TX 1837 Huston
IN 1832 Indianapolis
FL 1822 Jacksonville
CA 1850 Los Angeles
TN 1826 Memphis
WI 1846 Milwaukee
NY 1898 New York
PA 1701 Philadelphia
AZ 1881 Phoenix
TX 1837 San Antonio
CA 1850 San Diego
CA 1850 San Francisco
CA 1850 San Jose
DC 1788 Washington
___________________
C. Search City Data
___________________
Enter the city for search (QUIT for stop searching) : Baltimore
State Year City
----- ----- ----------
MD 1797 Baltimore
Enter the city for search (QUIT for stop searching) : Washington
State Year City
----- ----- ----------
DC 1788 Washington
Enter the city for search (QUIT for stop searching) : Memphis
State Year City
30. ----- ----- ----------
TN 1826 Memphis
Enter the city for search (QUIT for stop searching) : Cupertino
was not found
Enter the city for search (QUIT for stop searching) : QUIT
___________________
D. Delete City Data
___________________
Enter the city for delete (QUIT for stop searching) : Baltimore
State Year City
----- ----- ----------
MD 1797 Baltimore
** Delete the Baltimore**
Enter the city for delete (QUIT for stop searching) : Baltimore
was not found
Enter the city for delete (QUIT for stop searching) : Washington
State Year City
----- ----- ----------
DC 1788 Washington
** Delete the Washington**
Enter the city for delete (QUIT for stop searching) : Washington
was not found
Enter the city for delete (QUIT for stop searching) : Cupertino
was not found
31. Enter the city for delete (QUIT for stop searching) : QUIT
______________________________________________
Thank you for using the Linked Lists Project