VS2022下编译不通过,NULL显示的是未定义标识符,已经定义的结构体显示的是未定义标识符,各种错误都有
#include
#include
struct Node
{
int data;
struct Node* next; // C++中只需要写Node*
};
struct Node* head;
void Insert(int x)
{
struct Node* temp = (Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
head = temp;
}
void Print()
{
struct Node* temp = head;
printf("List is:");
while (temp != NULL)
{
printf(" %d", temp->next);
temp = temp->next;
}
printf("\n");
}
int main(void)
{
int n, x;
head = NULL;
printf("How many numbers?\n");
scanf("%d\n", &n);
for (int i = 0; i < n; i++)
{
printf("Enter the number:\n");
scanf("%d", &x);
Insert(x);
Print(x);
}
return 0;
}
修改见注释,供参考:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next; // C++中只需要写Node*
};
struct Node* head;
void Insert(int x)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));//修改
//struct Node* temp = (Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
head = temp;
}
void Print()
{
struct Node* temp = head;
printf("List is:");
while (temp != NULL)
{
printf(" %d", temp->data); //修改
// printf(" %d", temp->next);
temp = temp->next;
}
printf("\n");
}
int main(void)
{
int n, x;
head = NULL;
printf("How many numbers?\n");
scanf("%d", &n); //scanf("%d\n", &n);//修改
for (int i = 0; i < n; i++)
{
printf("Enter the number:\n");
scanf("%d", &x);
Insert(x);
Print(); //Print(x); //修改
}
return 0;
}