我想了解链接列表并且遇到了困难时间。我想将三个元素放在一个节点中,然后打印出多个节点。但是,我只能打印节点的第一个元素。 例如: 输入:1,2,3 输出:1个NULL链接列表的一个节点中的三个元素
struct node
{
int Intx, Inty, Intz;
struct node *p;
}
class linked
{
public:
node* create_node(int first, int second, int third);
int Intx, Inty, Intz;
void insert();
void display();
}
main()
{
linked sl;
sl.insert();
sl.display();
}
node *linked::create_node(int first, int second, int third)
{
Intx = first;
Inty = second;
Intz = third;
struct node *temp, *p;
temp = new (struct node);
if (temp == NULL)
{
cout << "Not able to complete";
}
else
{
temp->Intx = first, Inty = second, Intz = third;
temp->next = NULL;
return temp;
}
}
void linked::insert()
{
int Intx, Inty, Intz;
cout << "Enter First Element for node: ";
cin >> Intx;
cout << "Enter Second Element for node: ";
cin >> Inty;
cout << "Enter Third Element for node: ";
cin >> Intz;
struct node *temp, *s;
temp = create_node(Intx, Inty, Intz);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
s = start;
start = temp;
start->next = s;
}
cout << "Element Inserted." << endl;
}
void linked::display()
{
struct node *temp;
cout << "Elements of list are: " << endl;
while (temp != NULL)
{
cout << temp->Intx, Inty, Intz;
temp = temp->next;
}
cout << "NULL" << endl;
}
需要分号';'在结构和类定义之后。主要必须返回int。带有非void返回类型的函数(例如create_node)必须返回函数中所有路径的值。逗号操作符的滥用已经被覆盖在答案中。 Injblue,你需要重新回到教科书中,并在有效帮助之前完成一些基本的程序构建。谨防采取答案,您可以简单地剪切和粘贴,因为这会导致您学习成为[Cargo Cult Programmer](https://en.wikipedia.org/wiki/Cargo_cult_programming)。 – user4581301