SlideShare a Scribd company logo
#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();
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);
}
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)
{
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;
}
//****************************************************************************
*
// 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;
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);
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;
}
}
//**************************************************
// 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)
{
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) <<
"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;
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
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
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
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
----- ----- ----------
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
Enter the city for delete (QUIT for stop searching) : QUIT
______________________________________________
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();
// 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;
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)
{
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;
}
//****************************************************************************
*
// 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;
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);
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;
}
}
//**************************************************
// 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)
{
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) <<
"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;
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
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
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
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
----- ----- ----------
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
Enter the city for delete (QUIT for stop searching) : QUIT
______________________________________________
Thank you for using the Linked Lists Project

More Related Content

PDF
Hi,you covered mostly things.there are issue to point and link poi.pdf
PDF
This assignment and the next (#5) involve design and development of a.pdf
PDF
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
PDF
#includeiostream #includecstdio #includecstdlib using na.pdf
PDF
In C++Write a recursive function to determine whether or not a Lin.pdf
PDF
Lec-4_Linked-List (1).pdf
PDF
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
PDF
Please teach me how to fix the errors and where should be modified. .pdf
Hi,you covered mostly things.there are issue to point and link poi.pdf
This assignment and the next (#5) involve design and development of a.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
In C++Write a recursive function to determine whether or not a Lin.pdf
Lec-4_Linked-List (1).pdf
C++ projectMachine Problem 7 - HashingWrite a program to do the .pdf
Please teach me how to fix the errors and where should be modified. .pdf

Similar to #include iostream #include fstream #include iomanip #.pdf (20)

PDF
maincpp Build and procees a sorted linked list of Patie.pdf
PPTX
Linked lists in Data Structure
PPTX
3.linked list
PDF
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
PDF
#include iostream #include fstream #include cstdlib #.pdf
PPT
Mi 103 linked list
PPTX
Data Structures - Lecture 7 [Linked List]
DOCX
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
PDF
Implement a priority queue using a doublyLinked-cpp where the node wit.pdf
PDF
Please need help on following program using c++ language. Please inc.pdf
PDF
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
PDF
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
PPTX
List,Stacks and Queues.pptx
DOCX
C++ Please write the whole code that is needed for this assignment- wr.docx
DOCX
C++ detyrat postim_slideshare
PDF
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
PDF
This is the main file include itemh include itemList.pdf
PDF
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
maincpp Build and procees a sorted linked list of Patie.pdf
Linked lists in Data Structure
3.linked list
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
#include iostream #include fstream #include cstdlib #.pdf
Mi 103 linked list
Data Structures - Lecture 7 [Linked List]
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
Implement a priority queue using a doublyLinked-cpp where the node wit.pdf
Please need help on following program using c++ language. Please inc.pdf
Assignment is Page 349-350 #4 and #5 Use the Linked Lis.pdf
Create a link list. Add some nodes to it, search and delete nodes fro.pdf
List,Stacks and Queues.pptx
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ detyrat postim_slideshare
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
This is the main file include itemh include itemList.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
Ad

More from KARTIKINDIA (20)

PDF
We first see what is the Small Intestine – It connects the stomach a.pdf
PDF
Water needs to be present. The 2 compounds will only react after.pdf
PDF
True.SolutionTrue..pdf
PDF
1) option d is the answer, i.e. 11Solution1) option d is the a.pdf
PDF
The same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdf
PDF
The answer is b) facilitates O2 diffusion through alveolar membrane.pdf
PDF
The cam material will be UHMW polyethylene because it had the lost c.pdf
PDF
tan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdf
PDF
Ques-1 antinormative collective pro-social behaviorReasonOnlin.pdf
PDF
Physical properties can be observed or measured without changing the.pdf
PDF
(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf
PDF
Please follow the data and description 1) An association indicate.pdf
PDF
package chapter15;import javafx.application.Application;import j.pdf
PDF
Mononucleotides are monomer of polynucleotides. Its three uses are-.pdf
PDF
Intelligence comes from the Latin verb intellegere, which means .pdf
PDF
Initial compromise is the method that is adopted by intruders to ent.pdf
PDF
I agree .Ethernet nodes listen to the medium when they want to tra.pdf
PDF
A. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdf
PDF
Hi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdf
PDF
(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf
We first see what is the Small Intestine – It connects the stomach a.pdf
Water needs to be present. The 2 compounds will only react after.pdf
True.SolutionTrue..pdf
1) option d is the answer, i.e. 11Solution1) option d is the a.pdf
The same Mechanisms of genetic exchange in bacteria 1. Conjugation .pdf
The answer is b) facilitates O2 diffusion through alveolar membrane.pdf
The cam material will be UHMW polyethylene because it had the lost c.pdf
tan(x)=1x=pi4Solutiontan(x)=1x=pi4.pdf
Ques-1 antinormative collective pro-social behaviorReasonOnlin.pdf
Physical properties can be observed or measured without changing the.pdf
(E) a+ and b+ are more closely linked than a+ and c+.   If two gen.pdf
Please follow the data and description 1) An association indicate.pdf
package chapter15;import javafx.application.Application;import j.pdf
Mononucleotides are monomer of polynucleotides. Its three uses are-.pdf
Intelligence comes from the Latin verb intellegere, which means .pdf
Initial compromise is the method that is adopted by intruders to ent.pdf
I agree .Ethernet nodes listen to the medium when they want to tra.pdf
A. 1.Virus modified exoxomes are specialized form of nano sized vesi.pdf
Hi,pease find ansers for Questions1.5 Fill in the Blanksa) The.pdf
(1)White matter in the cerebellumThe arbor vitae refers to the c.pdf
Ad

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
master seminar digital applications in india
PPTX
Presentation on HIE in infants and its manifestations
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
RMMM.pdf make it easy to upload and study
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
master seminar digital applications in india
Presentation on HIE in infants and its manifestations
Module 4: Burden of Disease Tutorial Slides S2 2025
RMMM.pdf make it easy to upload and study
Microbial diseases, their pathogenesis and prophylaxis
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
human mycosis Human fungal infections are called human mycosis..pptx
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Final Presentation General Medicine 03-08-2024.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Anesthesia in Laparoscopic Surgery in India
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
102 student loan defaulters named and shamed – Is someone you know on the list?

#include iostream #include fstream #include iomanip #.pdf

  • 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); }
  • 3. 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) { cout << endl << endl << "B. Print City Data" << endl; cout << "___________________" << endl << endl; cout << left << setw(10) << "State" << setw(10) << "Year" << setw(20) << "City" <<
  • 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