2017-06-26 3 views
0

これは私が得るエラーです。私はリンクされたリストをC言語で実装しようとしています。Cでリンクされたリスト、 'ノード'は宣言されていません(この関数では最初に使用します)

prog.c:関数 'Insert':prog.c:33:26:エラー: 'node'宣言されていません(この関数では最初に使用されます) struct node * temp =(node *)malloc(sizeof(structノード));

コードは

#include<stdio.h> 
#include<stdlib.h> 

struct node{ 
    int data; 
    struct node* next; 
}; 

struct node* head; 

void Insert(int x); 
void Print(); 

int main(void){ 

    head = NULL; 
    printf("how many numbers?"); 
    int n,i,x; 
    scanf("%d",&n); 

    for(i=0;i<n;i++){ 
     printf("Enter the number"); 
     sacnf("%d",&x); 
     Insert(x); 
     Print(); 
    } 

    return 0; 
} 

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("\nThe List is "); 
    while(temp!=NULL){ 
     printf(" %d", temp->data); 
     temp=temp->next; 
    } 
} 
+1

全く 'node'はありませんする必要があります。 'struct node'があります。 CはC++ではありません。そして、[Cプログラムで 'malloc'をキャストしないようにする](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)。 – WhozCraig

+0

Cでは、構造体名を型として単独で使用する場合は、 'typedef'を使用する必要があります。 – Barmar

+0

それは働いて..ありがとう。 –

答えて

2

下に問題が機能void Insert(int x)のラインstruct node* temp = (node*)malloc(sizeof(struct node));にあるように、それはstruct node* temp = (struct node*)malloc(sizeof(struct node));であるべきです。訂正された作業コードHereが見つかります。

機能main(void)のラインsacnf("%d",&x);では、scanf("%d",&x);

関連する問題