c - 单链表 : newNode function doesn't point to next Node

标签 c linked-list int nodes singly-linked-list

我目前正在用 C 语言试验单链表。我写了一个 newNode 函数用于创建节点,printNodes 函数用于打印所有节点 - 它看起来像这样:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node 
{
  int data;
  struct Node *next;
}; 

void printNodes(struct Node *current_node)
{
  while(current_node != NULL)
  {
    printf("Node is: %d\n", current_node->data);
    current_node = current_node->next;
  }
}

int main()
{
  int number_1 = 2;
  int number_2 = 3;
  int number_3 = 4;

  struct Node *head;
  struct Node *second;
  struct Node *third;

  head = (struct Node*)malloc(sizeof(struct Node));  
  second = (struct Node*)malloc(sizeof(struct Node)); 
  third = (struct Node*)malloc(sizeof(struct Node));

  head->data = number_1;      
  head->next = second; 

  second->data = number_2;      
  second->next = third; 

  third->data = number_3;     
  third->next = NULL; 

  printNodes(head);

}

输出正确:

Node is: 2
Node is: 3 
Node is: 4

现在我想编写一个用于创建新节点的函数newNode,我将代码更改为:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Node 
{
    int data;
    struct Node *next;
};

struct Node *newNode(int number_x, struct Node *nextnode)
{
    struct Node *tmp_node;

    tmp_node = malloc(sizeof(struct Node));
    tmp_node->data = malloc(sizeof(struct Node));
    tmp_node->data = number_x;
    tmp_node->next = nextnode;

    return tmp_node;
}   

void printNodes(struct Node *current_node)
{
    while(current_node != NULL)
    {
        printf("Node is: %d\n", current_node->data);
        current_node = current_node->next;
    }
}

int main()
{
    int number_1 = 2;
    int number_2 = 3;
    int number_3 = 4;

    struct Node *head;
    struct Node *second;
    struct Node *third;

    head = newNode(number_1, second);
    second = newNode(number_2, third);
    third = newNode(number_3, NULL);

    printNodes(head);

}

编译后我首先收到此警告消息:

test.c:16:20: warning: incompatible pointer to integer conversion 
assigning to 'int' from 'void *' [-Wint-conversion]
tmp_node->data = malloc(sizeof(struct Node));
               ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~

输出如下所示:

Node is: 2

它只显示节点head,我猜next指向有问题 (例如 head->next = secondary),但是出了什么问题呢?我无法解决这个问题。

谢谢

最佳答案

这里

tmp_node->data = malloc(sizeof(struct Node)); /* remove this statement */

datastruct Node 的成员,它是一个整数,为此您不必单独分配内存。您已经在这里分配了完整的结构

tmp_node = malloc(sizeof(struct Node)); /* this is enough */

也在这里

head = newNode(number_1, second);

什么是第二?它应该用 NULL 初始化,例如

struct Node *second = NULL;

然后仅在 newNode() 函数 tmp_node->next 中分配正确的值

tmp_node->next = nextnode; /* now nextnode contains NULL, that's correct */

或者你可以像下面这样

head = newNode(number_1, NULL);
second = newNode(number_2, head);
third = newNode(number_3, second);

然后在调用printNodes()时传递third而不是head。例如

printNodes(third);

示例代码:

struct Node  {
    int data;
    struct Node *next;
};

struct Node *newNode(int number_x, struct Node *nextnode) {
    struct Node *tmp_node;
    tmp_node = malloc(sizeof(struct Node));
    tmp_node->data = number_x;
    tmp_node->next = nextnode;
    return tmp_node;
}

void printNodes(struct Node *current_node) {
    while(current_node != NULL) {
        printf("Node is: %d\n", current_node->data);
        current_node = current_node->next;
    }
}

int main(void) {
    int number_1 = 2;
    int number_2 = 3;
    int number_3 = 4;

    struct Node *head = NULL;
    struct Node *second = NULL;
    struct Node *third = NULL;

    head = newNode(number_1, NULL);
    second = newNode(number_2, head);
    third = newNode(number_3, second);
    printNodes(third);
        return 0;
}

关于c - 单链表 : newNode function doesn't point to next Node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53838425/

相关文章:

c - 有没有一种可移植的方法来定义 INT_MAX?

转换为无符号整数,然后转换回来,C

c++ - G++ 提示 "undefined reference to"而 GCC 成功

java - C++/C 和 Java 之间共享数据

c - c语言如何保持链表的头部?

当数组值为零而不是空时,c 循环停止

python - 类似于Python的 "if x in list"的C命令

C:使用指向 uint8_t[] 的指针时遇到一些问题

C 链表 : Segmentation fault on Windows, 适用于 Mac

c - 使用两个指针在 C 中查找列表是否是非循环的 - 为什么要与 faster->next 进行比较?