一般链表C ++

时间:2015-02-27 19:09:18

标签: c++ linked-list nodes

C ++新手,我有一个常规链表问题。说我有:

struct Node 
{
    string name;
    Node *next
};

在头文件中我有一个函数

Node ReadIntoList(const string INPUT_FILE)
{
    ifstream inFile;
    Node *head;
    head = NULL;
    Node *perPtr;
    perPtr = new Node;

    inFile.open(INPUT_FILE);
    while(inFile && perPtr!= NULL)
    {
        getline(inFile, perPtr->name);
        perPtr -> next = head;
        head = perPtr;
        perPtr = new Node;
    }
    delete perPtr;
    perPtr = NULL;

    return *head;
}

我的问题是我需要从ReadIntoList函数返回到main 这样我就可以访问列表然后能够设置一个函数来输出它 到一个文件。到目前为止,这是我的主要内容......

int main()
{
    ofstream oFile;
    string inputFile;
    Node *head;

    cout << left;
    cout << "Please enter the name of the input file you would like to "
            "use: ";

    getline(cin, inputFile);

    head = ReadIntoList(inputFile);

    oFile.open("OFile.txt");


    return 0;
}

主要的节点显然没有正确设置,但我不确定如何从ReadIntoList访问信息。

1 个答案:

答案 0 :(得分:4)

在您的代码中,ReadIntoList()返回列表中 last 节点的内容(如果输入文件无法打开,则会崩溃)。 ReadIntoList()需要将指针返回到列表中的第一个节点。这将允许main()遍历列表,例如:

Node* ReadIntoList(const string &inputFile)
{
    ifstream inFile;
    Node *head = NULL;
    Node *last = NULL;
    Node *perPtr = NULL;

    inFile.open(inputFile);
    if (inFile)
    {
        do
        {
            perPtr = new Node;
            if (!getline(inFile, perPtr->name))
            {
                delete perPtr;
                break;
            }
            perPtr->next = NULL;

            if (!head) head = perPtr;
            if (last) last->next = perPtr;
            last = perPtr;
        }
        while (true);
    }

    return head;
}

int main()
{
    ofstream oFile;
    string inputFile;
    Node *head;
    Node *perPtr;
    Node *tmp;

    cout << left;
    cout << "Please enter the name of the input file you would like to use: ";

    getline(cin, inputFile);

    head = ReadIntoList(inputFile);
    if (head)
    {    
        oFile.open("OFile.txt");
        if (oFile)
        {
            perPtr = head;
            do
            {
                oFile << perPtr->name << endl;
                perPtr = perPtr->next;
            }
            while (perPtr != NULL);
        }

        perPtr = head;
        do
        {
            tmp = perPtr->next;
            delete perPtr;
            perPtr = tmp;
        }
        while (perPtr != NULL);
    }

    return 0;
}

话虽如此,因为您使用的是C ++,所以您应该使用std::list(或C ++ 11及更高版本中的std::forward_list),例如:

#include <list>

bool ReadIntoList(const string &inputFile, list<string> &outList)
{
    outList.clear();

    ifstream inFile(inputFile);
    if (!inFile)
        return false;

    string name;
    while (getline(inFile, name))
        outList.push_back(name);

    return true;
}

int main()
{
    ofstream oFile;
    string inputFile;
    list<string> items;

    cout << left;
    cout << "Please enter the name of the input file you would like to use: ";

    getline(cin, inputFile);

    if (ReadIntoList(inputFile, items))
    {
        oFile.open("OFile.txt");
        if (oFile)
        {
            for (list<string>::const_iterator iter = items.begin(), end = items.end(); iter != end; ++iter)
            {
                oFile << *iter << endl;
            }
        }
    }

    return 0;
}