找问题实在是苦差事,我按楼主的程序框架重写了全部代码,楼主可以自己比对差别。#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct students {
char name[10] ;
int num ;
float score ;
struct students * next ;
} ;
int count = 0 ;
struct students * findStu(struct students * hd , const int num)
{
struct students * p1 , * p2 , * ret ;
bool f ;
ret = NULL ;
if(count > 0 && hd != NULL) {
for(f = false , p1 = p2 = hd ; p2 != NULL && ! f ;) {
if(p2 -> num == num) {
f = true ;
} else {
p1 = p2 ;
p2 = p1 -> next ;
}
}
if(f) ret = p1 ;
}
return ret ;
}
struct students * addStu(struct students * hd)
{
struct students * p1 , * p2 , * head ;
int num ;
head = hd ;
p1 = hd ;
if(p1 != NULL) while(p1 -> next != NULL) p1 = p1 -> next ;
for(;;) {
printf("\n") ;
printf("input student\'s num to add : ") ;
scanf("%d" , & num) ;
if(! num) break ;
if(findStu(head , num) == NULL) {
if((p2 = (struct students *) malloc(sizeof(struct students))) != NULL) {
printf("input student\'s name : ") ;
scanf("%s" , p2 -> name) ;
printf("input student\'s score: ") ;
scanf("%f" , & p2 -> score) ;
p2 -> num = num ;
p2 -> next = NULL ;
if(! count) head = p2 ;
else p1 -> next = p2 ;
p1 = p2 ;
count ++ ;
} else {
fprintf(stderr , "malloc() cann\'t to alocate the memory !") ;
break ;
}
} else {
printf("\n") ;
printf("the student with num = %d already exists !\n" , num) ;
}
}
return head ;
}
struct students * delStu(struct students * hd)
{
struct students * p1 , * p2 , * head ;
int num ;
head = hd ;
if(count > 0 && hd != NULL) {
while(count > 0) {
printf("\n") ;
printf("input student\'s num to delete : ") ;
scanf("%d" , & num) ;
if(! num) break ;
if((p1 = findStu(head , num)) != NULL) {
if(p1 == head) {
head = head -> next ;
p2 = p1 ;
} else {
p2 = p1 -> next ;
p1 -> next = p2 -> next ;
}
free(p2) ;
count -- ;
printf("the student with num = %d has been successfully deteted !\n" , num) ;
} else {
printf("\n") ;
printf("the student with num = %d doesn\'t exists !\n" , num) ;
}
}
} else {
printf("\n") ;
printf("empty list !\n") ;
}
return head ;
}
void searchStu(struct students * hd)
{
struct students * p ;
int num ;
if(count > 0 && hd != NULL) {
for(;;) {
printf("\n") ;
printf("input student\'s num to search : ") ;
scanf("%d" , & num) ;
if(! num) break ;
if((p = findStu(hd , num)) != NULL) {
printf("p -> num = %d\n" , p -> num) ;
if(p != hd) p = p -> next ;
printf("student's name : %s\n" , p -> name) ;
printf("student's num : %d\n" , p -> num) ;
printf("student's score : %5.2f\n" , p -> score) ;
} else {
printf("\n") ;
printf("the student with num = %d doesn\'t exists !\n" , num) ;
}
}
} else {
printf("\n") ;
printf("empty list !\n") ;
}
}
void showList(struct students * hd)
{
struct students * p ;
if(count > 0 && hd != NULL) {
p = hd ;
while(p != NULL) {
printf("\n") ;
printf("student\'s name : %s\n" , p -> name) ;
printf("student\'s num : %d\n" , p -> num) ;
printf("student\'s score : %5.2f\n" , p -> score) ;
p = p -> next ;
}
} else {
printf("\n") ;
printf("empty list !\n") ;
}
}
int main()
{
struct students * head ;
int op ;
head = NULL ;
count = 0 ;
do {
printf("\n") ;
printf("welcome to students\' information manage system!\n") ;
printf(" 1 add a student.\n") ;
printf(" 2 delete a student.\n") ;
printf(" 3 find a student.\n") ;
printf(" 4 show the list:\n") ;
printf(" 0 exit\n") ;
printf("\n") ;
printf(" your choice : ") ;
scanf("%d" , & op) ;
switch(op) {
case 1 :
head = addStu(head) ;
break ;
case 2:
head = delStu(head) ;
break ;
case 3:
searchStu(head) ;
break ;
case 4:
showList(head) ;
break ;
}
} while(op != 0) ;
}
|