使用链表函数循环

Looping with Linked List Functions

本文关键字:循环 函数 链表      更新时间:2023-10-16

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

该代码用于从命令文件中读取字母和数字作为指令,并相应地执行链表上的函数。命令文件以"r"answers"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

在您的代码中,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);