|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#define LEN sizeof(struct student) //student结构的大小
struct student *creat(); //创建链表
struct student *del(struct student *head,int num);
void print(struct student *head); //打印链表
struct student
{
int num;
float score;
struct student *next;
};
int n;
void main()
{
struct student *stu,*stu2;
stu=creat();
print(stu);
int a;
printf("please input the number you want to delete\n");
scanf_s("%d",&a);
stu2=del(stu,a);
print(stu2);
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN); //LEN是student结构体的大小
printf("Please enter the num:");
scanf_s("%d",&p1->num);
printf("please enter the score:");
scanf_s("%f",&p1->score);
head =NULL;
n=0;
while(0!=p1->num)
{
n++;
if(1==n)
{
head=p1;
}
else
{
(*p2).next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("\n please enter the num:");
scanf_s("%d",&p1->num);
printf("please enter the score:");
scanf_s("%f",&p1->score);
}
p2->next=NULL;
return head;
}
void print (struct student *head)
{
struct student *p;
printf("\n There are %d records!\n\n",n);
p=head;
if(head)
{
do
{
printf("学号为%d的成绩是:%f\n",p->num,p->score);
p=p->next;
}while(p);
}
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(NULL==head)
{
printf("\n This list is null!\n");
goto END;
}
p1=head;
while(p1->num!=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 NO: %d succeed!",num);
n=n-1;
}
else
{
printf("%d not been found!\n",num);
}
END:
return head;
}
几乎是照着小甲鱼课堂例题程序抄下来的,在视频中他的程序是可以运行的,可是我用VS2012的编译器,总是出错。
1>f:\visual studio 2012\projects\test\struct\struct.cpp(270): error C4703: 使用了可能未初始化的本地指针变量“p2”
|
-
出问题额地方
|