已调试成功!根据学生学号删除或插入节点,结束时,所有数据都输入0
#include "stdio.h"
#define N 20
#define M 3
/******************************/
typedef struct node
{
int num;
char name[N];
float score[M];
struct node *next;
}Node,*Link;
/******************************/
Link Creat()
{
int i;
Link head=NULL;
Link p1=NULL;
Link p2=NULL;
p1=p2=(Link)malloc(sizeof(Node));
if ( p1==NULL ) { printf("STOP!\n");exit(0); }
head=p1;
printf("Please enter the student information:\n");
scanf("%d %s",&p1->num,p1->name);
for(i=0;i
scanf("%f",&p1->score[i]);
}
while ( p1->num!=0 )
{
p2->next=p1;
p2=p1;
p1=(Link)malloc(sizeof(Node));
if ( p1==NULL ) { printf("STOP!\n");exit(0); }
scanf("%d %s",&p1->num,p1->name);
for(i=0;i
scanf("%f",&p1->score[i]);
}
}
p2->next=NULL;
return head;
}
/******************************/
Link Sort(Link head)
{
int i;
char temp[N];
int temp_1;
float temp_2;
Link p1=NULL;
Link p2=NULL;
for(p1=head;p1!=NULL;p1=p1->next)
for(p2=p1->next;p2!=NULL;p2=p2->next)
if ( p1->num>p2->num )
{
temp_1=p1->num;
p1->num=p2->num;
p2->num=temp_1;
strcpy(temp,p1->name);
strcpy(p1->name,p2->name);
strcpy(p2->name,temp);
for(i=0;i
temp_2=p1->score[i];
p1->score[i]=p2->score[i];
p2->score[i]=temp_2;
}
}
return head;
}
/******************************/
Link Delete(Link head,int num)
{
int count=0;
Link p1=head;
Link p2=NULL;
if ( p1==NULL ) { printf("STOP!\n");exit(0); }
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; }
free(p1);
}
else { printf("STOP\n");exit(0); }
return head;
}
/******************************/
Link Insert(Link head)
{
int i;
Link p0=(Link)malloc(sizeof(Node));
Link p1=head;
Link p2=NULL;
printf("Please input to insert the students' information:\n");
scanf("%d %s",&p0->num,p0->name);
for(i=0;i
scanf("%f",&p0->score[i]);
}
if ( head==NULL )
{
head=p0;
p0->next=NULL;
}
else
{
while ( p0->num>p1->num && p1->next!=NULL )
{
p2=p1;
p1=p1->next;
}
if ( p0->num<=p1->num )
{
if ( p1==head )
{
head=p0;
p0->next=p1;
}
else
{
p2->next=p0;
p0->next=p1;
}
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return head;
}
/******************************/
void Output(Link head)
{
int i;
Link p=head;
printf("ID\tName\tNo.1\tNo.2\tNo.3\n");
printf("------------------------------------------------------------\n");
while ( p!=NULL )
{
printf("%d\t%s\t",p->num,p->name);
for(i=0;i
printf("%.2f\t",p->score[i]);
}
printf("\n");
p=p->next;
}
printf("------------------------------------------------------------\n");
}
/******************************/
void main()
{
int num;
Link head;
head=Creat();
Sort(head);
Output(head);
printf("Please input to delete student id:\n");
scanf("%d",&num);
Delete(head,num);
Output(head);
Insert(head);
Output(head);
}
如果对你有所帮助,请记得采纳最佳答案,谢谢!