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

Three elements in one node of a linked list

我正在尝试理解链表,但遇到了困难。我想将三个元素放在一个节点中,然后打印出多个节点。但是,我只能打印节点的第一个元素。 例如: 输入: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;
}

而不是讨论代码中的错误。我宁愿建议您了解 linked 列表算法背后的逻辑。这会帮助你成长。

links Link1: Youtube tut and Link 2 将为您提供 link-list 算法工作原理的基础知识。

  1. 因为程序是用C++写的。 Link 1 是一个 youtube 教程,使用 C++ 编程逐步介绍 linked 列表操作中的每个 visual studio。这可能会帮助您理解 -> 运算符的正确使用。此外,它还可以帮助您了解对象与其成员之间的关系。

  2. 而 Link 2 仅在理论方面对 link 列表如何作为数据结构增长以及如何维护它有所帮助。

temp-> Intx = first, Inty = second, Intz = third;

用逗号分隔内容与您认为的不同。您应该使用三个语句并且必须在每个语句中包含 temp->

temp->Intx = first;
temp->Inty = second;
temp->Intz = third;

如果你真的想使用逗号运算符,你可以,但在所有三个作业中你仍然需要 temp->

同样,您在 display 中使用的逗号不符合您的要求

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

应该是

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

或类似的东西,具体取决于您希望它如何格式化