这是我的代码。一个简单的FILO链接列表,但我得到的错误很奇怪。它在Insert函数中我声明了temp,说Node是未声明的。不要理解我是如何完成这样的10次而且没有错误。谢谢你的时间。
//链接列表:在开头插入节点(FILO)
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head; //Global
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; //We use a temporary because we dont want to lose the reference to the head
printf ("List is: ");
while(temp != NULL)
{
printf (" %d", temp->data);
temp = temp -> next;
}
printf ("\n");
}
int main(){
head = NULL; //empty list
int n,i,x;
printf ("How many numbers?\n");
scanf ("%d",&n);
for (i=0; i<n; i++)
{
printf ("Enter the Number\n");
scanf ("%d",&x);
Insert(x);
Print();
}
}
答案 0 :(得分:4)
您的问题的解决方案只是为了避免转换malloc返回或使用正确的类型:struct node*
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head; //Global
void Insert (int x)
{
struct Node* temp = malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
head = temp;
}
void Print()
{
struct Node* temp = head; //We use a temporary because we dont want to lose the reference to the head
printf ("List is: ");
while(temp != NULL)
{
printf (" %d", temp->data);
temp = temp -> next;
}
printf ("\n");
}
int main(){
head = NULL; //empty list
int n,i,x;
printf ("How many numbers?\n");
scanf ("%d",&n);
for (i=0; i<n; i++)
{
printf ("Enter the Number\n");
scanf ("%d",&x);
Insert(x);
Print();
}
return 0;
}