2015-04-26 53 views
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"); 
} 
+2

更改'Node * temp =(node *)malloc(sizeof(struct Node));'struct'Node * temp = malloc(sizeof(struct Node));'和'temp1 = temp1.next'末尾缺少';'。 –

+0

解决了这个问题。谢谢 –

+0

可能重复的[未声明的标识符与结构](http://stackoverflow.com/questions/20182825/undeclared-identifiers-with-structs) –

回答

-1

创建更加模块化的链接列表库.. .your程序的可读性较差。当使用c ..中的结构时,尽可能使用typedef,这将使您每次都有更多使用NODE *而不是struct NODE *的权利。

相关问题