求助!!在链表中删除结点!!
#include<stdio.h>#include<stdlib.h>
#define LEN sizeof(struct stu)
struct stu
{
int num;
double score;
struct stu *next;
};
struct stu *del(struct stu *head,int num)
{
struct stu *p0;/*指向要删除的结点*/
struct stu *p1;/*p1的next域指向p0*/
p0=head;
if(head==NULL)
printf("The list is null\n");
else
{
while(p0!=NULL&&p0->num!=num)
{
p1=p0;
p0=p0->next;/*p0向后移位*/
}
if(p0->num==num) /*找到了删除的结点*/
{
if(head==p0) /*要删除的结点为第一个结点*/
head=p0->next;
else
{
p1->next=p0->next;/*p1的next域指向p0的next域*/
free(p0);/*释放p0的内存空间*/
}
}
else
printf("%d not been found!\n",num);/*没找到要删除的结点*/
}
return head;
}
struct stu *list(struct stu *head,struct stu *news)
{
struct stu *p0;/*指向要插入的结点*/
struct stu *p1;/*指向第一个学号比p0指向的结点学号大的结点*/
struct stu *p2;/*p2的next域指向p1*/
p1=head;
p0=news;
if(head==NULL)/*链表为空*/
{
head=p0;
p0->next=NULL;
}
else
{
while((p1!=NULL)&&(p0->num>=p1->num))/*寻找第一个学号比插入结点学号大的结点*/
{
p2=p1;
p1=p1->next;
}
if(p1==NULL) /*没找到比插入结点学号大的结点*/
{
p1->next=p0;
p0->next=NULL;
}
else
{
if(head==p1) head=p0;/*找到的结点为链表的第一个结点*/
else
{
p2->next=p0;
p0->next=p1;
}
}
}
return head;
}
void output(struct stu *head)
{
struct stu *p;
p=head;
if(head==NULL)
printf("The list is a null\n");
else
{
while(p!=NULL)
{
printf("%d %5.1f\n",p->num,p->score);
p=p->next;
}
}
}
int main()
{
struct stu *head;
struct stu *news;
int num;
head=NULL;
scanf("%d",&num);
while(num!=0)/*输入链表中的数据,输入为零结束*/
{
news=(struct stu *)malloc(LEN);/*为指针分配内存空间*/
news->num=num;
scanf("%f",&news->score);
head=list(head,news);
scanf("%d",&num);
}
output(head);
printf("input the number for deletion:");
scanf("%d",&num);
printf("delete:%d\n",num);
head=del(head,num);
output(head);
return 0;
}
没有错误,没有警告,但是运行时一直出错!!代码有点长,辛苦各位了!劳驾各位高手给个正解,谢谢哈!!
[ 本帖最后由 雨汪 于 2013-8-4 12:11 编辑 ]