C Add a new feature to mountain Listcpp prompt the user to
C++ - Add a new feature to mountain List.cpp : prompt the user to enter a state, such as Washington, and display
all mountains in your list that belong to that state. Continue asking the user for other states, until s/he enters QUIT.
(FILES)
MOUNTAIN.H
#ifndef Mountain_h
#define Mountain_h
#include<string>
using namespace std;
// Mountain Class
class Mountain
{
private:
string mountain;
int elevation;
string range;
string state;
public:
void setMountain(string mountainName){
mountain=mountainName;
}
void setElevation(int elevationNumber){
elevation=elevationNumber;
}
void setRange(string rangeName){
range=rangeName;
}
void setState(string stateName){
state=stateName;
}
string getMountain(){
return mountain;
}
int getElevation(){
return elevation;
}
string getRange(){
return range;
}
string getState(){
return state;
}
};
#endif /* mountain_h */
MOUNTAINLIST.CPP
#include <iostream>
#include <iomanip>
#include <string>
#include \"MountainList.h\"
using namespace std;
//**************************************************
// Constructor - make senteniel node at beginning
// of linked list
//**************************************************
MountainList::MountainList()
{
head = new ListNode; // head points to the sentinel node
head->next = NULL;
head->mount.setMountain(\"\");
head->mount.setElevation (0);
head->mount.setRange(\"\");
head->mount.setState(\"\");
}
//**************************************************
// The displayList function shows the value
// stored in each node of the linked list
// pointed to by head.
//**************************************************
void MountainList::displayList() const
{
// Position pCur: skip the head of the list.
ListNode *pCur = head->next;
// While pCur points to a node, traverse the list.
while (pCur)
{
// Display the value in this node.
cout << setw(18) << pCur->mount.getMountain();
cout << setw(9) << pCur->mount.getElevation();
cout << setw(23) << pCur->mount.getRange();
cout << setw(23) << pCur->mount.getState() << endl;
// Move to the next node.
pCur = pCur->next;
}
}
/**************************************************
Calculate and retrun how many mountains in the list
**************************************************/
int MountainList::mountainCount(){
return counter;}
//**************************************************
// The insertNode function inserts a new node in
// a sorted list (it keeps the list sorted)
//**************************************************
void MountainList::insertNode(Mountain dataIn)
{
ListNode *newNode; // A new node
ListNode *pCur = head->next; // To traverse the list
ListNode *pPre = head; // The previous node
// Allocate a new node and store num there.
newNode = new ListNode;
newNode->mount = dataIn;
// Initialize pointers
while (pCur != NULL && pCur->mount.getMountain() < dataIn.getMountain())
{
pPre = pCur;
pCur = pCur->next;
}
// Insert the new node between pPre and pCur
pPre->next = newNode;
newNode->next = pCur;
counter++;
}
//**************************************************
// The searchList function searches for a node
// with target as its mountain name. The node, if found, is
// deleted from the list and from memory.
//**************************************************
void MountainList:: searchList(string mountain) const
{
ListNode *pCur; // To transverse the list
pCur = head->next;
while (pCur != NULL && pCur->mount.getMountain() != mountain)
{
pCur = pCur->next;
}
if(pCur!= NULL && pCur->mount.getMountain() == mountain)
{
cout << \"FOUND: \";
cout << setw(10)<< pCur->mount.getMountain() << \" \";
cout << setw(10)<< pCur->mount.getElevation() << \" \";
cout << setw(10)<< pCur->mount.getRange() << \" \";
cout << setw(20)<< pCur->mount.getState() << \" \ \ \";
}
else
cout << mountain << \" NOT FOUND\ \ \";
}
void MountainList::deleteNode(string mountain)
{
ListNode *pCur = head->next; // To traverse the list
ListNode *pPre = head; // To point to the previous node
while (pCur != NULL && pCur->mount.getMountain() != mountain)
{
pPre = pCur;
pCur = pCur->next;
}
// If found, delete the node
if (pCur)
{
pPre->next = pCur->next;
delete pCur;
cout << \"The mountain has been deleted.\"<<endl;
counter--;
cout << \"There are \"<< counter <<\" mountains in this table!\"<<endl;
}
else
{
cout << \"<\" << mountain << \">\" << \" was not found\" << endl;
}
}
//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************
MountainList::~MountainList()
{
ListNode *pCur; // To traverse the list
ListNode *pNext; // To point to the next node
pCur = head->next;
while (pCur != NULL)
{
pNext = pCur->next;
// Delete the current node.
cout << \"DEBUG - Destructor: Now deleting \" << pCur->mount.getMountain() << endl;
delete pCur;
pCur = pNext;
}
delete head; // delete the sentinel node
}
MOUNTAINLIST.H
#ifndef MOUNTAINLIST_H
#define MOUNTAINLIST_H
#include<iostream>
#include\"Mountain.h\"
using namespace std;
class MountainList
{
private:
int counter= 0; // The purpose is to count the number of mountains in the list
struct ListNode
{
Mountain mount; // The value in this node
ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
MountainList(); // Constructor
~MountainList(); // Destructor
// Linked list operations
int mountainCount();
void insertNode(Mountain);
void deleteNode(string);
void searchList(string) const;
void shortestMountain() const; //find the shortest mountain
void highestMountain() const; //find the highest mountain
void displayList() const;
};
#endif
MOUNTAINS.TXT
Shasta
14179
Cascade Range
California
Churchill
15638
Saint Elias Mountains
Alaska
Antero
14276
Sawatch Range
Colorado
Granite Peak
12807
Beartooth Mountains
Montana
Bachelor
9068
Cascade Range
Oregon
Adams
12281
Cascade Range
Washington
Doublet Peak
13600
Wind River Range
Wyoming
Mauna Kea
13803
Mauna Kea
Hawaii
Castle Peak
9109
Sierra Nevada
California
Pyramid Peak
9985
Crystal Range
California
Torbert
11413
Tordillo Mountains
Alaska
Rainier
14411
Cascade Range
Washington
Half Dome
8836
Yosemite National Park
California
Jeff Davis Peak
12771
Snake Range
Nevada
Solution
/*added searchListState in MOUNTAINLIST.CPP . add void MountainList::searchListState(string state) const ; to class MountainList*/
void MountainList::searchListState(string state) const
{
ListNode *pCur; // To transverse the list
int FOUND = 0;
pCur = head->next;
while (pCur != NULL )
{
if (pCur->mount.getState() == state)
{
cout << setw(10) << pCur->mount.getMountain() << \" \";
cout << setw(10) << pCur->mount.getElevation() << \" \";
cout << setw(10) << pCur->mount.getRange() << \" \"<<endl;
FOUND = 1;
//cout << setw(20) << pCur->mount.getState() << \" \ \ \";
}
pCur = pCur->next;
}
if (!FOUND)
cout << state << \" NOT FOUND\ \ \";
}
//main.cpp
#include\"MOUNTAINLIST.CPP\"
#include<fstream>
int main()
{
MountainList mlist;
ifstream in;
Mountain data;
string m, s, r;
int e;
in.open(\"MOUNTAINS.txt\");
if (!in)
{
cout << \"unable to open input file\" << endl;
}
while (!in.eof())
{
//in >> m >> e ;
std::getline(in, m);
std::getline(in, s);
e = stoi(s);
std::getline(in,r );
std::getline(in, s);
//getline(in,r);
//in >> s;
data.setMountain(m);
data.setElevation(e);
data.setRange(r);
data.setState(s);
mlist.insertNode(data);
}
mlist.displayList();
do
{
cout << \"Enter the state: \";
cin >> s;
if (s.compare(\"QUIT\") == 0)
break;
mlist.searchListState(s);
} while (1);
}
----------------------------------------------------------------------------------------------------------------------------------------------------
//output
Adams 12281 Cascade Range Washington
Antero 14276 Sawatch Range Colorado
Bachelor 9068 Cascade Range Oregon
Castle Peak 9109 Sierra Nevada California
Churchill 15638 Saint Elias Mountains Alaska
Doublet Peak 13600 Wind River Range Wyoming
Granite Peak 12807 Beartooth Mountains Montana
Half Dome 8836 Yosemite National Park California
Jeff Davis Peak 12771 Snake Range Nevada
Mauna Kea 13803 Mauna Kea Hawaii
Pyramid Peak 9985 Crystal Range California
Rainier 14411 Cascade Range Washington
Shasta 14179 Cascade Range California
Torbert 11413 Tordillo Mountains Alaska
Enter the state: Washington
Adams 12281 Cascade Range
Rainier 14411 Cascade Range
Enter the state: Alaska
Churchill 15638 Saint Elias Mountains
Torbert 11413 Tordillo Mountains
Enter the state: californi
californi NOT FOUND
Enter the state: California
Castle Peak 9109 Sierra Nevada
Half Dome 8836 Yosemite National Park
Pyramid Peak 9985 Crystal Range
Shasta 14179 Cascade Range
Enter the state: QUIT










