【问题标题】:C error: dereferencing pointer to incomplete type linked listC 错误:取消引用指向不完整类型链表的指针
【发布时间】:2018-04-09 03:04:29
【问题描述】:

在我的 c 程序中,我创建了一个单链表,我们必须使用 prev 节点插入一个节点,我在 main() 函数中收到此错误:

取消指向不完整类型“结构节点”的指针

我不知道为什么会出现这个错误。

while((temp->next != NULL) && (temp->next->data < randomNumData)) -- 此处出错

typedef struct _Node
{       
        int data;
        struct Node *next;
} ListNode;

ListNode *newList();
ListNode *insertNode(ListNode *prev, int data);

int main()
{
        ListNode *head = newList();

        int randomNumData;
        ListNode *temp = head;
        int i;

        for(i = 0; i < 11; i++)
        {
                randomNumData = random()%1001;
                while((temp->next != NULL) && (temp->next->data < randomNumData))
                {
                        temp = temp->next;
                }
                temp->data = randomNumData;
                head = insertNode(temp, randomNumData);
        }

        printList(head);
}

// returning the head of a new list using dummy head node
ListNode *newList()
{
        ListNode *head;
        head = malloc(sizeof(ListNode));
        if(head == NULL)
        {
                printf("ERROR");
                exit(1);
        }

        head->next = NULL;
        return head;
}

ListNode *insertNode(ListNode *prev, int data)
{
        ListNode *temp = prev;
        prev->next = temp->next;
        temp->next->data = data;

        return temp;
}

【问题讨论】:

  • 你在哪里定义ListNodestruct Node
  • 请提供一个构建的例子。 ListNode的定义在哪里?
  • struct Node *next; --> struct _Node *next;
  • 对不起,我添加了 ListNode 的结构
  • 您已经声明了struct Node *next;,但没有在任何地方定义struct Node

标签: c


【解决方案1】:

声明下一个时缺少下划线。

typedef struct _Node
{       
    int data;
    struct _Node *next;
} ListNode;

【讨论】:

    【解决方案2】:

    看起来您没有名为 Node.js 的结构。从其他文件导入它或只是实现它。 C 不错,但目前还不是魔术师。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-31
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2011-04-06
      • 1970-01-01
      相关资源
      最近更新 更多