求高手帮忙改下错,链表问题
题目:1.建立n个结点(结点数据域的值由键盘输入)构成的单链表。
2.查找数据域值为x的结点,若找到,返回该结点的地址。
3.在数据域值为x的结点之前插入一数据域值为y的结点,若表中无数据域值为x的结点,则将数据域值为y的结点插入表尾。
4.删除数据域值为x的结点,并返回成功与否的标志。
建立链表
我编的程序帮忙看看错在哪里????
#define LEN sizeof(strcut student)
struct student
{int num;
float score;
struct student *next;
};
struct student *creat(int n)
{struct student *head,*p1,*p2;
int i;
for(i=0;i<n;i++)
if (*p1 = (struct student *)malloc(sizeof(LEN))) != NULL);
printf("input number and score:\n");
scanf("%d%f",*p1->num,&p1->score);
if(i==0)
p2=head=p1;
else p2->next=p1;
p1->next=NULL;
p2=p1;
}
return head;
}
void printf(struct student *head)
{stuct student *p;
p=head;
if(head!=NULL)
do
{printf("%d,%f\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}
查找链表
struct student *search(struct student *head,int n)
{struct student *p;int i;
p=head;
while(p->num!=n&&p->next!=NULL)
p=p->next;
if(p->num==n)
return p;
if(p->num!=n&&p->next=NULL)
printf("Node %d has not been found\n",n);
}
插入链表
struct student *insert(struct student *head,struct student *head *stud,int n)
{struct student *p0,*p1,*p2;
p1=head;
p0=head;
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(head==p1)
{head=p0;p0->next=p1;}
else {p2->next=p0;p0->next=p1;}
}
else {p1->next=p0;p0->next=NULL;}
}
n=n+1;
return head;
}
删除链表
struct student *del(struct student *head,int num,int n)
{struct student *p1,*p2;
if(head==Null)
{printf("\n list null!\n");go to 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("delete:%d\n",num);
n=n-1;
}
else printf("%d not be been found!\n",num);
return head;
}
主函数
main()
{struct student *head,stu;
int del-num,n;
scanf("%d",&n);
printf("input record:\n");
head=creat(n);
printf(head);
printf("\ninput record num:");
scanf("%d",&m);
printf("\ninput the deleated number:");
scanf("%d",&del-num);
head=del(head,del-num);
printf(head);
printf("\ninput the inserted record:");
scanf("%d,%f",&stu.num,&stu.score);
head=insert (head,*stu,n);
printf(head);
}