Question: C++ - Add a new feature to mountain List.cpp : prompt the user to enter a state, such as Washington, and display all mountains in
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
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
#include
#include
#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."< counter--; cout << "There are "<< counter <<" mountains in this table!"< } 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 #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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
