【问题标题】:Linked List in C++ using StructC++中使用Struct的链表
【发布时间】:2021-12-26 10:24:13
【问题描述】:

我没有得到 create_node() 函数中的 else 部分..
正如您在 else 部分中看到的那样,为 r 分配了内存块,并分配了 coeff 和 power...但是他们什么时候将 r 节点分配给链表的最后一个.. 他们什么时候遍历到链表的末尾
我的意思是它是如何在链表的最后分配的。


#include <bits/stdc++.h>
using namespace std;

struct Node {
    int coeff;
    int pow;
    struct Node* next;
};

// Function to create new node
void create_node(int x, int y, struct Node** temp)
{
    struct Node *r, *z;
    z = *temp;
    if (z == NULL) {
        r = (struct Node*)malloc(sizeof(struct Node));
        r->coeff = x;
        r->pow = y;
        *temp = r;
        r->next = (struct Node*)malloc(sizeof(struct Node));
        r = r->next;
        r->next = NULL;
    }
    else {
        r->coeff = x;
        r->pow = y;
        r->next = (struct Node*)malloc(sizeof(struct Node));
        r = r->next;
        r->next = NULL;
    }
}

// Display Linked list
void show(struct Node* node)
{
    while (node->next != NULL) {
        printf("%dx^%d", node->coeff, node->pow);
        node = node->next;
        if (node->coeff >= 0) {
            if (node->next != NULL)
                printf("+");
        }
    }
}

// Driver code
int main()
{
    struct Node *poly1 = NULL, *poly2 = NULL, *poly = NULL;

    // Create first list of 5x^2 + 4x^1 + 2x^0
    create_node(5, 2, &poly1);
    create_node(4, 1, &poly1);
    create_node(2, 0, &poly1);

    // Create second list of -5x^1 - 5x^0
    create_node(-5, 2, &poly2);
    create_node(-5, 0, &poly2);

    printf("1st Number: ");
    show(poly1);

    printf("\n2nd Number: ");
    show(poly2);

    return 0;
}

只有我认为 create_node() 函数应该比上面的代码更像这样吗?

void create_node(int x, int y, struct Node** temp)
{

    struct Node *r, *z;
    z = *temp;
    if (z == NULL) {
       r = (struct Node*)malloc(sizeof(struct Node));
        r->coeff = x;
        r->pow = y;
        r->next=NULL;
        *temp = r;
    }
    else {
        r = (struct Node*)malloc(sizeof(struct Node));
        r->coeff = x;
        r->pow = y;
        r->next=NULL;
        while(z->next!=NULL)
        {
            z=z->next;
        }
        z->next=r;
    }
}

我真的很想知道它如何产生正确的输出,即使没有将 newnode 分配给链表的最后一个

【问题讨论】:

  • 我们需要召集一个理事会来确定你是否是唯一一个这样认为的人。
  • 首先这不是 C++ 而是 C,所以这里的标签可能会产生误导。其次,链表的要点是您可以以相同的成本插入/前置/附加到它。 create_node 接受一个节点并附加到它。 else 分支用于当作为temp 传入的节点不是最后一个也称为tail(或者更确切地说它根据此实现存在)时。

标签: c++ struct linked-list singly-linked-list


【解决方案1】:

如果您要编写 C++ 程序,请使用运算符 new 分配内存,而不是调用 C 函数 malloc

如果你想将一个节点附加到链表的尾部,那么最好使用双边单链表。

第一个函数create_node 没有意义,因为附加了一个具有除数据成员next 之外的未初始化数据成员的虚拟节点。

    r->next = (struct Node*)malloc(sizeof(struct Node));
    r = r->next;
    r->next = NULL;

函数show 可以调用未定义的行为,因为它不检查传递的指针是否等于nullptr

// Display Linked list
void show(struct Node* node)
{
    while (node->next != NULL) {
    //...

作为 C++ 函数的函数可以通过以下方式声明和定义,而无需使用重复代码。

void create_node( Node **head, int x, int y )
{
    Node *new_node = new Node { x, y, nullptr };

    while ( *head ) head = &( *head )->next;

    *head = new_node;
}

std::ostream & show( const Node *head, std::ostream &os = std::cout )
{
    for ( ; head != nullptr; head = head->next )
    {
        os << head->coeff << '^' << head->pow;

        if ( head->next != nullptr ) os << " + ";
    }

    return os;
}

【讨论】:

  • 是的,谢谢你的解释......它有很大帮助......但我仍然很好奇虚拟节点如何自动分配到链表的最后一个而不遍历结束??
  • @RuchitaWagh 如果没有虚拟节点,函数 show 将无法正常工作。例如,如果列表仅包含一个节点且没有虚拟节点,则 while 语句 while (node->next != NULL) 的条件将评估为 false,并且不会输出任何内容。
【解决方案2】:

我没有得到 create_node() 函数中的 else 部分。你可以 参见 else 部分,内存块分配给 r 和 coeff 和 权力已分配...但他们何时将 r 节点分配给最后一个 链接列表..他们什么时候遍历到链接列表的末尾我的意思是 它是如何在链表的最后分配的

事实并非如此。当您传递一个空的Node* 时,z 将为空,并且您的else 代码正在取消引用甚至尚未初始化的r

// Function to create new node
void create_node(int x, int y, struct Node** temp)
{
    struct Node *r, *z;
    z = *temp;
    if (z == NULL) {
        r = (struct Node*)malloc(sizeof(struct Node));
        r->coeff = x;
        r->pow = y;
        *temp = r;
        r->next = (struct Node*)malloc(sizeof(struct Node));
        r = r->next;
        r->next = NULL;
    }
    else {
        r->coeff = x;
        r->pow = y;
        r->next = (struct Node*)malloc(sizeof(struct Node));
        r = r->next;
        r->next = NULL;
    }
}

只有我一个认为 create_node() 函数应该是 比上面的代码更像这个?

这似乎解决了上面的代码问题。在这里,您将创建一个Node 并将其分配给r,然后将其附加到z 的末尾(输入Node*)。请注意,尽管您在两个代码块中都复制了用于创建 r 的代码。您至少可以从if-else 中取出那部分。

void create_node(int x, int y, struct Node** temp)
{

    struct Node *r, *z;
    z = *temp;
    if (z == NULL) {
        r = (struct Node*)malloc(sizeof(struct Node));
        r->coeff = x;
        r->pow = y;
        r->next=NULL;

        *temp = r;
    }
    else {
        r = (struct Node*)malloc(sizeof(struct Node));
        r->coeff = x;
        r->pow = y;
        r->next=NULL;

        while(z->next!=NULL)
        {
            z=z->next;
        }
        z->next=r;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多