这个c代码(链表实现)有什么问题?

Aji*_*kar 1 c linked-list singly-linked-list

 //linked list implementation 
 #include<stdio.h>
 #include<stdlib.h>
 struct node
 {
   int data;
   struct node* link;
  };
 struct node* head;
 void insert(int);
 void print();
 int main()
 {
   head=NULL;
   int n,i,x;
   printf("\nEnter the number of elements :");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
     printf("\nEnter the element :");
     scanf("%d",&x);
     insert(x);
     print();
   }
   }

   void insert(int x)
   {
     struct node* temp=(node*)malloc(sizeof(struct node));
     temp->data=x;
     temp->link=head;
     head=temp;
   }
   void print()
   {
     struct node* temp=head;
     int i=0;
     printf("\nThe list is ");
     while(temp!=NULL)
     {
       printf("%d ",temp->data);
       temp=temp->link;
     }
     printf("\n");
     }
Run Code Online (Sandbox Code Playgroud)

在编译代码时:

In function 'insert':
28:24: error: 'node' undeclared (first use in this function)
     struct node* temp=(node*)malloc(sizeof(struct node));
                        ^
28:24: note: each undeclared identifier is reported only once for each function it appears in
28:29: error: expected expression before ')' token
     struct node* temp=(node*)malloc(sizeof(struct node));
                             ^
Run Code Online (Sandbox Code Playgroud)

Dra*_*rgy 6

  1. node*struct node*C语言中的上下文不同,与C++相同.

  2. 尽量避免在C中使用多余的强制转换.实际上,尽量避免在任何不需要的语言中使用多余的强制转换.我是否施放了malloc的结果?