在 C 中将项目强制转换到链表末尾

发布于 12-29 19:42 字数 878 浏览 1 评论 0原文

编辑*(8:14 PM) - 抱歉,我更正了我的代码,并将其改为一种方法,以便更容易理解。

我不确定在添加到链表末尾时如何正确转换结构。编译这段代码在最后一行给了我一个强制转换警告。这可能是我的其余代码无法正常运行的原因。

例如:

#include <stdlib.h>

typedef struct {
    int data;
    struct node *next;
} node;

node *HEAD = NULL;

node *addNode(int num)
{
    if (HEAD == NULL) {
        HEAD = (node *)malloc(sizeof(node));
        HEAD->next = NULL;
        HEAD->data = num;
    }
    else {
        node *newNode;
        newNode = (node *)malloc(sizeof(node));
        newNode->data = num;
        newNode->next = NULL;

        node *iter;
        iter = (node *)malloc(sizeof(node));
        iter = (node *)HEAD;

        while(iter->next != NULL)
            iter = (node *)iter->next;

        iter->next = newNode; //warning : warning: assignment from incompatible pointer type
    } 
    return HEAD;
}

EDIT*(8:14 PM) - Sorry I corrected my code and made this instead a method so it can be more easily understood.

I am not sure how to properly cast a struct when adding to the end of a linked list. Compiling this code gives me an cast warning at the very last line. This may be the reason why the rest of my code does not properly function.

For example:

#include <stdlib.h>

typedef struct {
    int data;
    struct node *next;
} node;

node *HEAD = NULL;

node *addNode(int num)
{
    if (HEAD == NULL) {
        HEAD = (node *)malloc(sizeof(node));
        HEAD->next = NULL;
        HEAD->data = num;
    }
    else {
        node *newNode;
        newNode = (node *)malloc(sizeof(node));
        newNode->data = num;
        newNode->next = NULL;

        node *iter;
        iter = (node *)malloc(sizeof(node));
        iter = (node *)HEAD;

        while(iter->next != NULL)
            iter = (node *)iter->next;

        iter->next = newNode; //warning : warning: assignment from incompatible pointer type
    } 
    return HEAD;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑梦风尘2025-01-05 19:42:21
  • 确保包含 stdlib.h - 需要使用 malloc
  • 将所有出现的 wordNode 修复为节点 - wordNode 在程序中未定义
  • 创建一个结构体和 typedef 都命名节点 - 自引用结构体的标准技巧

,然后是所有警告消失;

#include <stdlib.h>
struct node{
  int data;
  struct node *next;
};
typedef struct node node;


node *HEAD = NULL;

int main(int argc, char*argv[]) {

  int x = 1;
  int y = 2;

  if(HEAD == NULL)
    {
      HEAD = (node *)malloc(sizeof(node));
      HEAD->next = NULL;
      HEAD->data = x;
    }
  else
    {
      node *newNode;
      newNode = (node *)malloc(sizeof(node));
      newNode->data = y;
      newNode->next = NULL;

      node *iter;
      iter = (node *)malloc(sizeof(node));
      iter = (node *)HEAD;

      while(iter->next != NULL)
    iter = (node *)iter->next;

      iter->next = newNode; //warning : warning: assignment from incompatible pointer type
      return 0;
    }
}
  • Make sure to include stdlib.h -- needed to use malloc
  • fix all occurance of wordNode to be node -- wordNode is undefined in your program
  • create a struct and typedef both named node -- standard trick for self referential structs

and then all your warnings goes away;

#include <stdlib.h>
struct node{
  int data;
  struct node *next;
};
typedef struct node node;


node *HEAD = NULL;

int main(int argc, char*argv[]) {

  int x = 1;
  int y = 2;

  if(HEAD == NULL)
    {
      HEAD = (node *)malloc(sizeof(node));
      HEAD->next = NULL;
      HEAD->data = x;
    }
  else
    {
      node *newNode;
      newNode = (node *)malloc(sizeof(node));
      newNode->data = y;
      newNode->next = NULL;

      node *iter;
      iter = (node *)malloc(sizeof(node));
      iter = (node *)HEAD;

      while(iter->next != NULL)
    iter = (node *)iter->next;

      iter->next = newNode; //warning : warning: assignment from incompatible pointer type
      return 0;
    }
}
节枝2025-01-05 19:42:21

问题在于,在结构完全定义之前,您将“next”声明为指向“结构节点”的指针,因此“next”指向未定义的结构。如果将“typedef struct{”更改为“typedef struct node{”,该错误就会消失。

The problem is that you declare "next" to be a pointer to "struct node" before the struct is completely defined, so "next" points to an undefined structure. If you change "typedef struct{" to "typedef struct node{", that error will be gone.

煮酒2025-01-05 19:42:21

您的代码存在许多问题。第一个是转换 malloc 的返回值,并错误地引用您想要为其分配一些空间的类型的大小:

HEAD = (node *)malloc(sizeof( node));

应替换为

HEAD = malloc(sizeof(*HEAD))

因为从 void* 到任何其他类型的转换始终已定义并且隐含在 C 中,你不会得到任何关于所需演员阵容的警告。指定 sizeof(*HEAD) 使编译器在编译时自动选择 HEAD 的类型,从而减少类型发生变化时所需的工作。

您还应该记住,某些编译器不喜欢匿名结构(即没有声明名称的结构)。因此,该代码

typedef struct{
int data;
struct node *next;
} node;

应替换为

typedef struct _node {
int data;
struct _node *next;
} node;

Which statements 声明一个名为 _node 的结构,typedef 为名为 node 的类型。并且还修复了循环引用。

最重要的是,您不需要为 iter malloc 任何空间。

There is a number of problems with your code. The first one would be casting the return value of malloc and improperly referring to the size of the type for which you want some space to be allocated :

HEAD = (node *)malloc(sizeof(node));

should be replaced by

HEAD = malloc(sizeof(*HEAD))

Since the conversion from void* to any other type is always defined and implicit in C, you don't get any warnings about a needed cast. Specifying sizeof(*HEAD) makes the compiler automatically choose the type of HEAD at compile time, thus reducing the needed work should the type ever change.

You should also remember that some compilers don't like anonymous structures (i.e. structures without a name declared). Therefore, the code

typedef struct{
int data;
struct node *next;
} node;

should be replaced by

typedef struct _node {
int data;
struct _node *next;
} node;

Which declares a structure called _node, typedefed to the type called node. And also fixes the circular reference.

On top of all that, you don't need to malloc any space for the iter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文