链表函数出错
#include<stdio.h>#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct worker)
struct worker
{
long num,age;
char name[8];
char sex[2];
struct worker *next;
};
int n;
struct worker *creat(void) /*建立链表函数*/
{
struct worker *head;
struct worker *p1,*p2;
n=0;
p1=p2=(struct worker *)malloc(LEN);
scanf("%ld %s %ld %s",&p1->num,&p1->name,&p1->age,&p1->sex);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
{
p2->next=p1;
p2=p1;
p1=(struct worker *)malloc(LEN);
scanf("%ld %s %ld %s",&p1->num,&p1->name,&p1->age,&p1->sex);
}
p2->next=NULL;
return(head);
}
void print(struct worker *head) /*建立输出函数*/
{
struct worker *p;
p=head;
printf("\nThese %d records are:\n",n);
if(head!=NULL)
do{
printf("%ld %s %ld %s",p->num,p->name,p->age,p->sex);
p=p->next;
}
while(p!=NULL);
}
struct worker *del(struct worker *head,long num) /*删除结点函数*/
{
struct worker *p1,*p2;
if(head=NULL)
{
printf("\nList is NULL\n");
goto end;
}
p1=head;
while(num!=p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("\ndelete list item:%ld\n",num);
n=n-1;
}
else
printf("%ld not been found!\n",num);
end:
return(head);
}
struct worker *insert(struct worker *head,struct worker *stud)/*插入结点函数*/
{
struct worker *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num)>(p1->num)&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if((p0->num)<=p1->num)
{
if(p1==head)
head=p0;
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return(head);
}
void main()
{
struct worker *head,*wor;
long del_num;
printf("Input records:\n");
head=creat();
print(head);
printf("\ninput the deleted number:\n");
scanf("%ld",&del_num);
while(del_num!=0) /*删除函数*/
{
head=del(head,del_num);
print(head);
printf("\ninput the deleted number:\n");
scanf("%ld",&del_num);
}
printf("\ninput the inserted record:\n ");
wor=(struct worker *)malloc(LEN);
scanf("%ld %s %ld %s",&wor->num,&wor->name,&wor->age,&wor->sex);
while(wor->num!=0)
{
head=insert(head,wor);
print(head);
printf("\ninput the inserted record:\n ");
scanf("%ld %s %ld %s",&wor->num,&wor->name,&wor->age,&wor->sex);
}
}
错误是local function definitions are illegal