在循环单链表开头插入节点的函数会产生错误:第 21 行的[警告]来自不兼容指针类型的赋值
。
这是什么意思以及如何解决它?
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int info;
struct node *next;
};
typedef struct Node node;
node *head = NULL;
node *tail = NULL;
void insertAtBeginning()
{
int i;
node *temp = head;
node *NewPtr = NULL;
NewPtr = (node *)malloc(sizeof(node));
printf("\nEnter a value\n");
scanf("%d", &i);
NewPtr -> info = i;
NewPtr -> next = NewPtr; //Line 21
if(head == NULL)
{
head = NewPtr;
NewPtr-> next = NewPtr;
}
else
{
while(temp -> next != head)
{
temp = temp->next;
}
NewPtr -> next = head;
temp -> next = NewPtr;
head = NewPtr;
}
}
最佳答案
您混合了类型和 typedef
。 struct node
不存在,但您尝试在 struct Node
定义中使用它。
要解决该问题,您可以将 node
的 typedef
放在 struct Node
的实际定义之前并在任何地方使用node
(不是struct node
,因为它不存在)。
typedef struct Node node;
struct Node
{
int info;
node *next; // note: node, not struct node
};
关于c - 链表产生警告的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76662920/