使用链接列表函数循环

时间:2015-02-24 02:03:00

标签: c++ function pointers while-loop linked-list

我是c ++和链表的新手。

此代码用于读取字母和数字作为命令文件中的指令,并相应地在链接列表上执行函数。命令文件以'r'和'1' - 读指令开始。

我的代码读取然后执行第一个字母指令。然后它崩溃并停止执行剩余的指令。但是,如果在读取后调用,每个字母都可以正常工作,所以我不确定while循环或函数是否存在问题 - 最有可能出现指针问题。

奇怪的是,此代码以前工作正常,现在它一直卡住了。 有人可以发现问题所在并请帮助我。

由于

代码

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include <sstream>

using namespace std;

typedef int item;
struct node {
   item data;
   node* next;
   };
typedef node* nodeptr;

void add_to_list (item number, nodeptr &hdlist);
item get_from_list(nodeptr &hdlist);
void read (string file_number, nodeptr &hdlist);
void print_all (nodeptr hdlist);
void write (nodeptr hdlist, ofstream &output_file);
void entries(nodeptr hdlist, string file_number);

int main()
{
    nodeptr hdlist = NULL;
    ifstream command_file;
    string command_filename;

    cin >> command_filename;

    command_file.open(command_filename.c_str());

    if (!command_file.is_open())
    {
        cout << "file not found" << endl;
        exit (EXIT_FAILURE);
    }

    vector<string> commands;
    string line;

    while (getline(command_file, line))
    {
        commands.push_back(line);
    }

    command_file.close();

    string file_number, value_to_delete, value_to_insert;
    int i = 0;

    while (i < commands.size())
    {   
        char input = (commands[i])[0];

        if (input == 'i')
        {
            print_all(hdlist);  
        }
        else if (input == 'r')
        {   file_number = (commands[i+1]);
            i++;
            read (file_number, hdlist);
        }
        else if (input == 'w')
        {
            ofstream output_file;
            string output_filename = "output_" + file_number + ".txt";
            output_file.open(output_filename.c_str(),ios::app);

            if (!output_file.is_open())
            {
                cout << "file not found" << endl;
                exit (EXIT_FAILURE);
            }

            write(hdlist, output_file);

            output_file.close();
        }
        else if (input == 'e')
        {
            entries(hdlist, file_number);
        }
        else
        {
            i = commands.size() + 1;
        }

        i++;
        //cout << hdlist << endl;
    }
}

void add_to_list (item number, nodeptr &hdlist)
{
    nodeptr newnode = new node;
    newnode->data = number;
    newnode->next = hdlist;
    hdlist = newnode;
}

item get_from_list (nodeptr &hdlist)
{
    int number;
    nodeptr nowptr;
    nowptr = hdlist;
    number = nowptr->data;
    hdlist = nowptr->next;
    delete nowptr;
    return item(number);
}

void print_all (nodeptr hdlist)
{
    if (hdlist != NULL)
    {
        print_all(hdlist->next);
        cout << get_from_list(hdlist) << endl;
    }
}

void read (string file_number, nodeptr &hdlist)
{
    string line;
    ifstream data_file;
    string data_filename = "data_" + file_number + ".txt";;
    data_file.open(data_filename.c_str());

    if (!data_file.is_open())
    {
        cout << "file not found" << endl;
        exit (EXIT_FAILURE);
    }

    while (getline(data_file, line))
    {
        stringstream ss;
        int num;
        ss << line;
        ss >> num;
        add_to_list(num,hdlist);
    }

    data_file.close();
}

void write (nodeptr hdlist, ofstream &output_file)
{
        if (hdlist != NULL)
        {
            write(hdlist->next, output_file);
            output_file << get_from_list(hdlist) << endl;

        }
}

void entries(nodeptr hdlist, string file_number)
{
    int count = 0;
    while (hdlist != NULL)
    {
        get_from_list(hdlist);
        count++;
    }

    ofstream output_file;
    string output_filename = "output_" + file_number + ".txt";
    output_file.open(output_filename.c_str(),ios::app);

    if (!output_file.is_open())
    {
        cout << "file not found" << endl;
        exit (EXIT_FAILURE);
    }

    output_file << "Number of elements in the list:" << count << endl;

    output_file.close();

}

命令文件示例

r
1
w
e
i
w

1 个答案:

答案 0 :(得分:0)

在代码中,hdlist在执行函数write()后不会被更改,因为第一个参数是按值传递的,然后函数entries()将尝试删除无效指针。所以

void write (nodeptr hdlist, ofstream &output_file);
void entries(nodeptr hdlist, string file_number);

应该是

void write (nodeptr &hdlist, ofstream &output_file);
void entries(nodeptr &hdlist, string file_number);