代码如下
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct student
{
int num;
float score;
struct student *next;
};
struct student *creat();
struct student *del(struct student *head,int num);
void print(struct student *head);
int n;
int main()
{
system("color f5");
int a;
struct student *stu;
stu = creat();
print(stu);
printf("输入要删除的学生学号:");
scanf("%d",&a);
del(stu,a);
print(stu);
system("pause");
return 0;
}
struct student *creat()
{
struct student *head,*p1,*p2;
p1 = p2 = (struct student *)malloc(sizeof(struct student));
printf("请输入学生学号:");
scanf("%d",&p1->num);
printf("请输入学生的分数:");
scanf("%f",&p1->score);
head = NULL;
n = 0;
while(p1->num)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(sizeof(struct student));
printf("请输入学生学号:");
scanf("%d",&p1->num);
printf("请输入学生的分数:");
scanf("%f",&p2->score);
}
p2->next = NULL;
return head;
};
void print(struct student *head)
{
struct student *p;
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(head)
{
printf("\n这是一个空表。");
goto END;
}
else
{
p1 = head;
while(p1->num != NULL&&p1->num != num)
{
p2 = p1;
p1 = p1->next;
}
if(p1->num == num)
{
if(p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
n = n-1;
}
else
{
printf("%d找不到。",num);
}
}
END:
return head;
};
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct student
{
int num;
float score;
struct student *next;
};
struct student *creat();
struct student *del(struct student *head,int num);
void print(struct student *head);
int n;
int main()
{
system("color f5");
int a;
struct student *stu;
stu = creat();
print(stu);
printf("输入要删除的学生学号:");
scanf("%d",&a);
del(stu,a);
print(stu);
system("pause");
return 0;
}
struct student *creat()
{
struct student *head,*p1,*p2;
p1 = p2 = (struct student *)malloc(sizeof(struct student));
printf("请输入学生学号:");
scanf("%d",&p1->num);
printf("请输入学生的分数:");
scanf("%f",&p1->score);
head = NULL;
n = 0;
while(p1->num)
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(sizeof(struct student));
printf("请输入学生学号:");
scanf("%d",&p1->num);
printf("请输入学生的分数:");
scanf("%f",&p2->score);
}
p2->next = NULL;
return head;
};
void print(struct student *head)
{
struct student *p;
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(head)
{
printf("\n这是一个空表。");
goto END;
}
else
{
p1 = head;
while(p1->num != NULL&&p1->num != num)
{
p2 = p1;
p1 = p1->next;
}
if(p1->num == num)
{
if(p1 == head)
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
n = n-1;
}
else
{
printf("%d找不到。",num);
}
}
END:
return head;
};