【发布时间】:2012-03-28 09:46:20
【问题描述】:
我试图实现一个链表数据类型的小样本。我创建了一个包含一个值的结构节点,一个跟踪所有节点的结构列表(以链表格式)。最后是一个类,它是两个结构的通用数据类型。
我运行程序时,总是一到步骤就报段错误错误: item->link[i++] = 新节点(输入)
代码:
struct Node
{
int val;
Node();
Node(int x);
};
struct List
{
Node *link[20];
List();
};
class pol
{
public:
void read_txt(ifstream &file);
private:
List *item;
};
void pol::read_txt(ifstream &file)
{
int input, i;
i = 0;
file >> input;
cout << "Value read from the file: " << input << endl;
while(input != 0)
{
item.link[i++] = new Node(input);
file >> input;
cout << "Value read from the file: " << input << endl;
}
}
我想要做的是,创建一个新节点,其值为“输入”,我从读取文件中获得。接下来,我想创建一个连接所有节点的链表。
【问题讨论】:
-
此
item.link不应编译为item是List*:item->link。
标签: c++ memory pointers linked-list segmentation-fault