#includestruct student
{
char name[20];
long int num;
int score1,score2;
char sex;
struct student *next;
};
int n;
long num;
struct student *head=NULL,*stu;
struct student *creat()
{
struct student *head,*p,*q;
n=0;
head=NULL;
p=q=(struct student *)malloc(sizeof(struct student));
do{
printf("enter date(input 0 for end):\n");
printf("please input Student num:");
scanf("%ld%*c", &p->num);
if (0 == p->num)
break;
printf("please input Student name:");
scanf("%s", &p->name);
printf("please input Student score1:");
scanf("%d", &p->score1);
printf("please input Student score2:");
scanf("%d", &p->score2);
printf("please input Student sex(m/w):");
scanf("%*c%c%*c", &p->sex);
n++;
if(n==1)
{
head=p;
head->next=NULL;
}
else
q->next=p;
q=p;
p=(struct student *)malloc(sizeof(struct student));
}while(p->num!=0);
q->next=NULL;
return(head);
}
struct student *insert(struct student *head)
{
struct student *p0,*p1,*p2;
struct student *stud = (struct student *)malloc(sizeof(struct student));
printf("please input Student num:");
scanf("%ld%*c", &stud->num);
printf("please input Student name:");
scanf("%s", &stud->name);
printf("please input Student score1:");
scanf("%d", &stud->score1);
printf("please input Student score2:");
scanf("%d", &stud->score2);
printf("please input Student sex(m/w):");
scanf("%*c%c%*c", &stud->sex);
p0 = head;
while( p0->next ){
p0 = p0->next;
}
p0->next = stud;
stud->next = NULL;
return head;
}
struct student *delete(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\nlist null!\n");
return NULL;
}
p1=head;
while(num!=p1->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("delete:%ld\n",num);
free(p1);
n--;
}
else
printf("%ld not been found!\n",num);
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
{
printf("\n\n| Student Num | | Student Name | | Score1 | | Score2 | | sex |\n");
do{
printf("| %11ld | | %12s | | %6d | | %6d | | %c |\n",p->num,p->name,p->score1,p->score2,p->sex);
p=p->next;
}
while(p!=NULL);
}
else
printf("this is a NULL list");
}
struct student *find(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)
{
printf("\n list null!\n");
return NULL;
}
p1=head;
while(p1!=NULL&&p1->num!=num)
{
p1=p1->next;
}
if(p1!=NULL)
{
printf("Find:");
printf("Stu Num:%ld\nStu name:%s\nStu sco1:%d\nStu sco2:%d\nStu sex:%c\n",p1->num,p1->name,p1->score1,p1->score2,p1->sex);
}
else
printf("%ld not been found!\n",num);
return(head);
}
int main()
{
struct student *stud;
int menu();
while(1)
{
switch(menu())
{
case 1:
head=creat();
break;
case 2:
head = insert(head);
printf("\n\tcontinue!");
break;
case 3:
scanf("%d%*c",&num);
delete(head, num);
printf("\n\tcontinue!");
break;
case 4:
print(head);
printf("\n\tcontinue!");
getch();
break;
case 5:
printf("Please input find num:");
scanf("%d%*c",&num);
find(head, num);
printf("\n\tcontinue!");
getch();
break;
case 6:
clrscr();
case 7:
exit(0);
}
}
}
int menu()
{
char d[7];
int c;
printf("\n\n%16s"," ");
printf("*****menu*****\n\n");
printf("\t\t1.creat list\n");
printf("\t\t2.insert list\n");
printf("\t\t3.delete list\n");
printf("\t\t4.print list\n");
printf("\t\t5.find list\n");
printf("\t\t6.clrscr\n");
printf("\t\t7.exit\n");
do{
printf("\n\tenter select(1-7): ");
gets(d);
c=atoi(d);
}
while(c<0||c>7);
return c;
}
- 13 -