使用链表函数循环
Looping with Linked List Functions
我是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);
相关文章:
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 循环中的随机函数
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 未在函数C++中执行的循环
- 如何在没有可变参数函数的情况下循环函数的参数
- 循环函数中的并行数组
- 如何循环函数,直到新的 mqtt 消息到达
- 循环C++ R 函数
- 为什么我的 while 循环函数只返回"1"?
- Pthread 循环函数永远不会被调用
- 循环函数调用未正确循环!(C++)
- 对于循环函数参数在C++中的奇数
- 具有双重调度的C++多态循环函数
- 循环函数给出"return’ with a value, in function returning void"误差
- 如何用循环函数求和
- 如何使c++中的for each循环函数与自定义类一起工作
- c++分割故障及循环函数
- C++ |需要帮助从 for 循环函数转换 - >递归函数
- 为什么要在 levelDB 的缓存中使用 while 循环(函数 Resize)?
- odeint中的循环函数