将数据传递给类的currentNode指针

时间:2018-01-10 19:32:03

标签: c++ pointers

我目前无法正确输出我的程序。 currentNode指针是空的,我不确定如何让它不为空。这个程序处理链接列表,我尝试使用getter无效(我尝试时出现分段错误)。该程序应该采用州立公园,将它们插入链表,从州立公园搜索相关数据,并将摘要输出到文件。我得到的只是文件中的输出语句,没有数据应该在哪里。以下是该计划的所有相关代码:

parkLL.h:

#include <string>
#include <cstddef>
#include <iostream>
#include <cstring>
using namespace std;

struct ParksNode;
typedef ParksNode* ParksPtr;
struct ParksNode
{
    string parkName;
    string parkLocation;
    string parkOpeningYear;
    string parkDescription;
    int noTimesReferenced;
    ParksPtr parkLink;
};




class Parks
{
public:
    Parks();
    ~Parks();
    void Delete(string pName);
    int GetLength() const;
    ParksNode GetNextPark();
    bool HasNext() const;
    void Insert(string& pName, string& pLocation, string& pOpeningYear, 
string& pDescription, int& numberTimesReferenced);
    bool IsEmpty() const;
    bool IsFull() const;
    bool IsThere(string pName) const;

private:
ParksNode* parkDataPtr; //Points to the first node in the list
int length;
ParksNode* parkCurrentPos; //Points to the current position in the list
ParksNode* parkLastPtr; //Point to the last node in the list

};

parkLL.cpp

#include <string>
#include <iostream>
#include <cstddef>
#include <cstring>
#include "parkLL.h"
using namespace std;



Parks::Parks()
//The parkDataPtr will be set to NULL
{
    parkDataPtr = NULL;
    parkCurrentPos = parkDataPtr;
    length = 0;
}

Parks::~Parks()
//The destructor for the Parks Linked List class
{
    ParksPtr parkTempPtr;
    ParksPtr parkCurrPtr = parkDataPtr;
    while(parkCurrPtr != NULL)
    {
        parkTempPtr = parkCurrPtr;
        parkCurrPtr = parkCurrPtr->parkLink;
        delete parkTempPtr;
    }
}

void Parks::Delete(string pName)
//The delete method for the Parks Linked List class
{
    ParksPtr parkPrevPtr = NULL; //The trailing pointer for the list
    ParksPtr parkCurrPtr = parkDataPtr; //The loop control pointer

    while(parkCurrPtr != NULL && parkCurrPtr->parkName != pName && 
parkCurrPtr->parkName < pName)
    {
        parkPrevPtr = parkCurrPtr;
        parkCurrPtr = parkCurrPtr->parkLink;
    }
    if(parkCurrPtr != NULL && parkCurrPtr->parkName == pName) //the item has 
been found
    {
        if(parkCurrPtr == parkDataPtr)
        {
            parkDataPtr = parkCurrPtr->parkLink;
        }
        else
        {
            parkPrevPtr->parkLink = parkCurrPtr->parkLink;
        }
        if(parkCurrPtr == parkLastPtr)
        {
            parkLastPtr = parkPrevPtr;
        }
        delete parkCurrPtr;
        length--;
    }
}

int Parks::GetLength() const
//Returns the length of the current list
{
    return(length);
}

ParksNode Parks::GetNextPark()
//Gets the next park in the linked list
{
    ParksNode currentPark;
    currentPark.parkName = parkCurrentPos->parkName;
    currentPark.parkLocation = parkCurrentPos->parkLocation;
    currentPark.parkOpeningYear = parkCurrentPos->parkOpeningYear;
    currentPark.parkDescription = parkCurrentPos->parkDescription;
    currentPark.noTimesReferenced = parkCurrentPos->noTimesReferenced;
    parkCurrentPos = parkCurrentPos->parkLink;
    return(currentPark);
}


bool Parks::HasNext() const
//Checks to see if there is another park in the list
{
    return(parkCurrentPos!=NULL);
}

void Parks::Insert(string& pName, string& pLocation, string& pOpeningYear, 
string& pDescription, int& numberTimesReferenced)
//The insert method for the Parks Linked List class
{
    ParksPtr parkCurrPtr; //moving pointer
    ParksPtr parkPrevPtr; //trailing pointer
    ParksPtr parkNewNodePtr; //pointer to a new node

    parkNewNodePtr = new ParksNode;
    parkNewNodePtr->parkName = pName;
    parkNewNodePtr->parkLocation = pLocation;
    parkNewNodePtr->parkOpeningYear = pOpeningYear;
    parkNewNodePtr->parkDescription = pDescription;
    parkNewNodePtr->noTimesReferenced = numberTimesReferenced;

        parkCurrPtr = parkDataPtr;
        parkPrevPtr = NULL;
        while(parkCurrPtr != NULL && parkCurrPtr->parkName < pName)
        {
            parkPrevPtr = parkCurrPtr;
            parkCurrPtr = parkCurrPtr->parkLink;
        }
        //Inserting the new node
        parkNewNodePtr->parkLink = parkCurrPtr;
        if(parkPrevPtr == NULL)
        {
            parkDataPtr = parkNewNodePtr;
        }
        else
        {
            parkPrevPtr->parkLink = parkNewNodePtr;
        }
        if(parkCurrPtr == NULL)
        {
            parkLastPtr = parkNewNodePtr;
        }
    length++;
}

bool Parks::IsEmpty() const
//Checks to see if the list is empty
{
    return(parkDataPtr == NULL);
}

bool Parks::IsFull() const
//Checks to see if the list is full, will always return false because this 
is a linked list
{
    return(false);
}

bool Parks::IsThere(string pName) const
//Checks to see if an item is in the linked list
{
    ParksPtr parkCurrPtr = parkDataPtr; //Loop control pointer

    while(parkCurrPtr != NULL && parkCurrPtr->parkName != pName)
    {
        parkCurrPtr = parkCurrPtr->parkLink;
    }
    if(parkCurrPtr != NULL)
    {
        return(true);
    }
    else
    {
        return(false);
    }
}

updateLL.cpp

#include <string>
#include <iostream>
#include <fstream>
#include <cstddef>
#include "parkLL.h"

void CloseFiles(ifstream& parkDataFile, ifstream& parkLookupFile, ofstream& 
parkOutputFile);
//This function closes all data files

void OpenFiles(string& parkDataFileName, string& parkLookupFileName, string& 
parkOutputFileName, ifstream& parkDataFile, ifstream& parkLookupFile, 
ofstream& parkOutputFile);
//This function opens all data files

int main()
{
    string parkDataFileName; //The variable for the data file for park info
    string parkLookupFileName; //The variable for the lookup file name
    string parkOutputFileName; //The variable for the output file name
    ifstream parkDataFile; //The variable for the park data input file
    ifstream parkLookupFile; //The variable for the park lookup file
    ofstream parkOutputFile; //The variable for the park output file
    string parkName2; //The variable for the park name
    string parkLocation2; //The variable for the park location
    string parkOpeningYear2; //The variable for the park opening year
    string parkDescription2; //The variable for the park description
    int noTimesReferenced2; //The variable for the number of times 
referenced
    string lookupChoice; //The variable for the information to look up
    string option; //The variable for the option of data output
    Parks currentPark; //The variable for the current park
    ParksPtr currentNode; //The variable for the current node

    cout << "Please enter the data file name: ";
    cin >> parkDataFileName;
    cout << "Please enter the lookup file name: ";
    cin >> parkLookupFileName;
    cout << "Please enter the output file name: ";
    cin >> parkOutputFileName;

    OpenFiles(parkDataFileName, parkLookupFileName, parkOutputFileName, 
parkDataFile, parkLookupFile, parkOutputFile);

    getline(parkDataFile, parkName2, '#'); //priming read for while loop
    while (parkDataFile)
    {
        getline(parkDataFile, parkLocation2, '#');
        getline(parkDataFile, parkOpeningYear2, '#');
        getline(parkDataFile, parkDescription2);
        noTimesReferenced2 = 0;
        currentPark.Insert(parkName2, parkLocation2, parkOpeningYear2, 
parkDescription2, noTimesReferenced2);
        getline(parkDataFile, parkName2, '#');
    }


    cout << endl;
    cout << "Outputting the info to file" << endl;

    getline(parkLookupFile, parkName2, '#');
    getline(parkLookupFile, lookupChoice);

    currentNode = new ParksNode;
    while(parkLookupFile)
    {
        if(currentPark.IsThere(parkName2))
        {
            if (lookupChoice == "Year")
            {   
                parkOutputFile << currentNode->parkName << " established in 
" << currentNode->parkOpeningYear << endl;
                currentNode->noTimesReferenced = currentNode-
>noTimesReferenced + 1;
            }

            else if (lookupChoice == "Description")
            {
                parkOutputFile << currentNode->parkName << " is " << 
currentNode->parkDescription << endl;
                currentNode->noTimesReferenced = currentNode-
>noTimesReferenced + 1;
            }

            else if (lookupChoice == "Location")
            {
                parkOutputFile << currentNode->parkName << " located near " 
 << currentNode->parkLocation << endl;
                currentNode->noTimesReferenced = currentNode-
>noTimesReferenced + 1;
            }
            else if (lookupChoice == "Remove")
            {
                currentPark.Delete(parkName2);
            }
        }

        else
        {
            parkOutputFile << "** " << parkName2 << " is not an available 
state park" << endl;
        }

        getline(parkLookupFile, parkName2, '#');
        getline(parkLookupFile, lookupChoice);
    }
    delete currentNode;

    parkOutputFile << "Summary (state park/ reference frequency)" << endl;

    while(currentPark.HasNext())
    {
        parkOutputFile << currentNode->parkName << " " << currentNode-
>noTimesReferenced;
    }


    CloseFiles(parkDataFile, parkLookupFile, parkOutputFile);

}

void CloseFiles(ifstream& parkDataFile, ifstream& parkLookupFile, ofstream& 
parkOutputFile)
{
    parkDataFile.close();
    parkLookupFile.close();
    parkOutputFile.close();
}

void OpenFiles(string& parkDataFileName, string& parkLookupFileName, string& 
parkOutputFileName, ifstream& parkDataFile, ifstream& parkLookupFile, 
ofstream& parkOutputFile)
{
    parkDataFile.open(parkDataFileName.c_str());
    parkLookupFile.open(parkLookupFileName.c_str());
    parkOutputFile.open(parkOutputFileName.c_str());
}

我提前为代码中可能出现的语法错误道歉,因为传输代码很困难。请在方便的时候帮助我。非常感谢您对此事的任何和所有帮助。

0 个答案:

没有答案