|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
麻烦大家帮我看一下为啥程序还没走完就结束了,还没到删除节点程序就结束了。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student *creat();
void print(struct student *head);
struct student *del(struct student *head,int num);
struct student
{
int num;
float score;
struct student *next;
};
int n;
void main()
{
struct student *stu;
int num;
stu = creat();
print(stu);
printf("input the number you wanna delete: ");
scanf("%d",&num);
stu = del(stu,num);
print(stu);
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *head,*p1,*p2;
p1 = p2 = (struct student *)malloc(LEN);
printf("please input the num:");
scanf("%d",&p1->num);
printf("please input the score:");
scanf("%f",&p1->score);
n = 0;
while(p1->num != 0)
{
n++;
if(n==1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("please input the num:");
scanf("%d",&p1->num);
printf("please input the score:");
scanf("%f",&p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p = head;
while(p->num != 0)
{
printf("学号为 %d 的成绩为: %f\n",p->num,p->score);
p = p->next;
}
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("this is a empty list!\n");
goto END;
}
p1 = head;
while(p1->num!=num&&p1->next!=NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p1->num==num)
{
if(p1==head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
printf("Delete No:%d completed!\n",num);
n = n - 1;
}
else
{
printf("No:%d is not found!\n",num);
}
END:
return head;
}
|
-
|