0
我有以下代碼在LinkedList中插入元素。我有以下教程,我無法發現此代碼中的錯誤。未在DevC++中聲明(首次在此函數中使用)'Node'
我使用DEVC++,它給我,說一個編譯時錯誤: [錯誤]「節點」未申報(第一次在這個函數中使用)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
struct Node* head; //global variable
int main(){
head = NULL; //empty list
int n, i, x;
printf("How many numbers would you like to enter");
scanf("%d", &n);
for(i=0; i<n; i++){
printf("Enter the number you want to add to list:");
scanf("%d", &x);
Insert(x);
Print();
}
}
void Insert(int x){
Node* temp= (Node*)malloc(sizeof(struct Node)); //I get error here
temp->data = x;
//temp->next = NULL; //redundant
//if(head!= NULL){
temp->next = head; //temp.next will point to null when head is null nd otherwise what head was pointing to
//}
head = temp;
}
void Print(){
struct Node* temp1 = head; //we dont want tomodify head so store it in atemp. bariable and then traverse
while(temp1 != NULL){
printf(" %d", temp1->data);
temp1= temp1->next;
}
printf("\n");
}
更改'Node * temp =(node *)malloc(sizeof(struct Node));'struct'Node * temp = malloc(sizeof(struct Node));'和'temp1 = temp1.next'末尾缺少';'。 –
解決了這個問題。謝謝 –
可能重複的[未聲明的標識符與結構](http://stackoverflow.com/questions/20182825/undeclared-identifiers-with-structs) –