C++ 链表的一个节点中的三个元素

C++ 链表的一个节点中的三个元素,c++,linked-list,nodes,element,C++,Linked List,Nodes,Element,我正在努力理解链表,现在很困难。我想在一个节点中放入三个元素,然后打印出多个节点。但是,我只能打印节点的第一个元素。 例如: 输入:1、2、3 输出:1空 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;

我正在努力理解链表,现在很困难。我想在一个节点中放入三个元素,然后打印出多个节点。但是,我只能打印节点的第一个元素。 例如: 输入:1、2、3 输出:1空

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;
}
struct节点
{
Intx,Inty,Intz;
结构节点*p;
}
类链接
{
公众:
节点*创建_节点(int-first,int-second,int-third);
Intx,Inty,Intz;
无效插入();
void display();
}
main()
{
链接sl;
sl.插入();
sl.display();
}
节点*链接::创建_节点(int-first,int-second,int-third)
{
Intx=第一;
Inty=秒;
Intz=第三;
结构节点*temp,*p;
temp=新建(结构节点);
if(temp==NULL)
{
cout Intx=第一,Inty=第二,Intz=第三;
temp->next=NULL;
返回温度;
}
}
void linked::insert()
{
Intx,Inty,Intz;
cout>Intx;
cout>Inty;
cout>Intz;
结构节点*temp,*s;
temp=创建_节点(Intx、Inty、Intz);
if(start==NULL)
{
启动=温度;
开始->下一步=空;
}
其他的
{
s=启动;
启动=温度;
开始->下一步=s;
}

不要讨论代码中的错误。我建议您理解链表算法背后的逻辑。这将有助于您的成长

链接和将为您提供链接列表算法工作原理的基本信息

  • < P>由于程序是用C++制作的,链接1是YouTube教程,在Visual Studio中使用C++编程对每个链表操作逐一提供,这可能帮助您理解正确使用<代码> -> /Cl>运算符。此外,它还可以帮助您理解对象及其成员之间的关系。

  • 而链接2仅在理论上有助于了解链接列表如何随着数据结构的增长而增长,以及如何维护它

  • 用逗号分隔东西并不能像您认为的那样。您应该使用三个语句,并且必须在每个语句中包含
    temp->

    temp->Intx = first;
    temp->Inty = second;
    temp->Intz = third;
    
    如果您真的想使用逗号运算符,可以,但在所有三个赋值上仍然需要
    temp->

    类似地,您在
    显示中使用的逗号也不符合您的要求

    cout<< temp->Intx, Inty, Intz;
    
    coutIntx,Inty,Intz;
    
    应该是

    cout<< temp->Intx << "," << temp->Inty << "," << temp->Intz;
    

    coutinx虽然我同意评估和意图,但你应该总结一下链接,使之成为一个真正的答案。目前这只是一个评论,而不是一个评论answer@user4581301我很抱歉,我会提供它作为评论,但没有足够的声誉发表评论。我试图修复我的回答。但如果你建议我将很高兴flag my answere用于保持堆栈干净。在结构和类定义后需要分号“;”。main必须返回int.函数,返回类型为非void(例如创建_节点)必须为函数中的所有路径返回一个值。答案中已经包含了逗号运算符滥用。在获得有效帮助之前,您需要回到教科书中,学习一些基本的程序构建。请注意,不要简单地剪切和粘贴答案,因为这将导致您学习成为一名程序员。
    
    cout<< temp->Intx << "," << temp->Inty << "," << temp->Intz;