//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");
}
编译代码时:
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));
^
-
node*
与 C 中的struct node*
不同
,因为它与C++不同。 尽量避免在 C 语言中使用多余的转换。实际上,在不需要的任何语言中,尽量避免多余的转换。我是否施放了马洛克的结果?
更改以下语句
struct node* temp=(node*)malloc(sizeof(struct node));
到
struct node* temp=(struct node*)malloc(sizeof(struct node));
有时我确实会犯类似的错误。