【发布时间】:2015-09-25 18:20:19
【问题描述】:
我正在尝试理解链表,但遇到了困难。我想将三个元素放在一个节点中,然后打印出多个节点。但是,我只能打印节点的第一个元素。 例如: 输入: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;
}
【问题讨论】:
-
需要分号';'在结构和类定义之后。 main 必须返回 int。具有非 void 返回类型的函数(例如 create_node)必须为函数中的所有路径返回一个值。逗号运算符滥用已包含在答案中。 Injblue,您需要深入研究教科书并进行一些基本的程序构建,然后才能得到有效的帮助。当心你可以简单地剪切和粘贴答案,因为这会导致你学习成为Cargo Cult Programmer。
标签: c++ linked-list nodes element