|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 xurui71 于 2011-6-30 08:02 编辑
Debug Error! runtime error (press retry to debug the application)。这是错误的内容。
单步调试时在scanf("%f",&p1->score)这里就出现错误。程序是这样的:
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)//结构体长度
struct student
{
int num;
float score;
struct student *next;
};
struct student *creat();//创建链表
struct student *del(struct student *head);//删除链表
struct student *insert(struct student *head);//插入链表
void print(struct student *head);//打印链表
int n=0;//定义全局变量,链表单元数量
void main()
{
struct student *stu;
stu=creat();//创建
print(stu);//打印
stu=del(stu);//删除
print(stu);//打印
stu=insert(stu);//插入
print(stu);//打印
}
struct student *creat()//创建链表函数
{
struct student *head,*p1,*p2;
p1=p2=head=(struct student *)malloc(LEN);
printf("please enter the num: ");
scanf("%d",&p1->num);
if(0==p1->num)
{
p2=NULL;
}
while(0!=p1->num)
{
n++;
printf("please enter the score: ");
scanf("%f",&p1->score);//单步调试在这里就出错了,我用的是VC++6.0编译器。第一次遇到debug错误,无从下手。
p1=p2;
p1=(struct student *)malloc(LEN);
p2->next=p1;
printf("please enter the num: ");
scanf("%d",&p1->num);
}
if(head!=NULL&&0==p1->num)
{
p2->next=NULL;
}
return head;
}
struct student *del(struct student *head)//删除链表
{
struct student *p1,*p2;
int num=1;
p1=p2=head;
while(num)
{
printf("please enter the delete num: ");
scanf("%d",&num);
if(0!=num)
{
n--;
if(num==p1->num)
{
p1=p1->next;
}
else
{
while(num!=p1->num&&p1!=NULL)
{
p1=p1->next;
p2=p1;
}
if(num==p1->num)
{
p2->next=p1->next;
}
else if(p1==NULL)
{
printf("do not find the num!\n");
}
}
}
}
return head;
}
struct student *insert(struct student *head)//插入链表函数
{
struct student *p1,*p2,*stu_d;
stu_d=(struct student *)malloc(LEN);
stu_d->num=1;
while(stu_d->num)
{
printf("please enter the insert num: ");
scanf("%d",&stu_d->num);
p1=p2=head;
while(0!=&stu_d->num)
{
n++;
printf("please enter the insert score: ");
scanf("%f",&stu_d->score);
if(stu_d->num<head->num)
{
stu_d->next=head;
head=stu_d;
}
else if(stu_d->num==head->num)
{
printf("the num is included!!\n");
}
else
{
while(stu_d->num>p1->num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(stu_d->num==p1->num)
{
printf("the num is included!!\n");
}
else if(stu_d->num<p1->num)
{
p2->next=stu_d;
stu_d->next=p1;
}
else if(stu_d->num>p1->num)
{
p1->next=stu_d;
stu_d->next=NULL;
}
}
}
}
return head;
}
void print(struct student *head)//打印链表函数
{
while(head)
{
printf("num=%d\tscore=%5.2f\n");
head=head->next;
}
}
|
|