在我的 c 程序中,我创建了一个单链表,我们必须在其中使用 prev 节点插入一个节点,我在 main()
中收到此错误功能:
Dereferencing pointer to incomplete type ‘struct Node’
我不确定为什么会出现该错误。
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;
}
最佳答案
你在声明 next 时缺少下划线。
typedef struct _Node
{
int data;
struct _Node *next;
} ListNode;
关于C 错误 : dereferencing pointer to incomplete type linked list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46976208/