使用链表函数循环 我是C++和链表的新手。< /P>
此代码用于从命令文件中读取字母和数字作为指令,并相应地在链表上执行函数。命令文件以“r”和“1”开头,这是一条读取指令 我的代码读取并执行第一个字母的指令。然后它崩溃并停止执行其余的指令。但是,如果在读取之后调用,每个字母都可以正常工作,因此我不确定while循环或函数是否存在问题——最有可能的是指针问题 奇怪的是,这段代码以前工作得很好,现在却一直卡住。 有人能找出问题所在并帮我解决吗 谢谢 代码使用链表函数循环 我是C++和链表的新手。< /P>,c++,function,pointers,while-loop,linked-list,C++,Function,Pointers,While Loop,Linked List,此代码用于从命令文件中读取字母和数字作为指令,并相应地在链表上执行函数。命令文件以“r”和“1”开头,这是一条读取指令 我的代码读取并执行第一个字母的指令。然后它崩溃并停止执行其余的指令。但是,如果在读取之后调用,每个字母都可以正常工作,因此我不确定while循环或函数是否存在问题——最有可能的是指针问题 奇怪的是,这段代码以前工作得很好,现在却一直卡住。 有人能找出问题所在并帮我解决吗 谢谢 代码 #include <iostream> #include <fstream&g
#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();
}
在代码中,执行函数
write()
后,hdlist
将不会更改,因为第一个参数是按值传递的,然后函数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);
请提供完整的源代码,
void write (nodeptr &hdlist, ofstream &output_file);
void entries(nodeptr &hdlist, string file_number);