#include <stdio.h>
#include <malloc.h>
struct students * create(int n);//链表的建立
void print(struct students *head);//链表的输出
struct students * del(struct students *head,int num);
struct students
{
char name[20];
int number;
int sco;
struct students *next;
};
int main()
{
struct students *head;int n,num;
printf("请输入学生人数:");scanf("%d",&n);
head=create(n);
printf("请输入要删除的编号:");scanf("%d",&num);
head=del(head,num);
printf("\n删除后:\n\n");
print(head);
return 0;
}
struct students * del(struct students *head,int num)
{
struct students *p1,*p2;
if(head==NULL)
{
printf("原表为空!\n");
return (NULL);
}
else
{
p1=head;
while(p1->number!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->number==num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("\n学号为%d的学生已经删除\n",num);
}
else
printf("学号为%d的学生不存在\n",num);
return (head);
}
}
struct students * create(int n)
{
struct students *head=NULL,*p1,*p2;
int i;
for(i=1;i<=n;i++)
{
p1=(struct students *)malloc(sizeof(struct students));
printf("请输入第%d个学生的姓名,学号,成绩\n",i);
scanf("%s%d%d",p1->name,&p1->number,&p1->sco);
p1->next=NULL;
if(i==1) head=p1;
else p2->next=p1;
p2=p1;
}
return head;
}
void print(struct students *head)
{
struct students *p=head;
printf("姓名 学号 成绩\n");
while(p!=NULL)
{
printf("%4s %4d %4d\n",p->name,p->number,p->sco);
p=p->next;
}
}