#include <stdio.h> #include <malloc.h> #include <string.h> #include <stdlib.h> #define N 3 #undef _EXAM_ASSERT_TEST_ //禁用 //#define _EXAM_ASSERT_TEST_ //启用 #ifdef _EXAM_ASSERT_TEST_ //启用断言测试 void assert_report( const char * file_name, const char * function_name, unsigned int line_no ) { printf( "\n[EXAM]Error Report file_name: %s, function_name: %s, line %u\n", file_name, function_name, line_no ); abort(); } #define ASSERT_REPORT( condition ) \ do{ \ if ( condition ) \ NULL; \ else \ assert_report( __FILE__, __func__, __LINE__ ); \ }while(0) #else // 禁用断言测试 #define ASSERT_REPORT( condition ) NULL #endif /* end of ASSERT */ typedef enum _SListReturn { SLIST_RETURN_OK }SListReturn; typedef struct node { char name[10]; int score; struct node *link; }stud; stud * creat(int n) { stud *p,*h,*s; int i; if((h=(stud *)malloc(sizeof(stud)))==NULL) { printf("分配内存空间失败!"); exit(0); } h->name[0]='\0'; h->score=0; h->link=NULL; p=h; for(i=0;i<n;i++) { if((s= (stud *) malloc(sizeof(stud)))==NULL) { printf("分配内存空间失败!"); exit(0); } p->link=s; printf("请输入第%d个人的姓名:",i+1); scanf("%s",s->name); printf("请输入第%d个人的成绩:",i+1); scanf("%d",&s->score); s->link=NULL; p=s; } return h; } SListReturn destroy(stud* head) { stud* tmp,*next; tmp=head; while(tmp!=NULL) { next=tmp->link; tmp->link=NULL; free(tmp); tmp=next; } return SLIST_RETURN_OK; } SListReturn print(stud* head) { stud* tmp=head->link; while(tmp!=NULL) { printf("%s的成绩为%d\t",tmp->name,tmp->score); tmp=tmp->link; } return SLIST_RETURN_OK; } void main() { int number; stud *head; number=N; head=creat(number); ASSERT_REPORT(print(head)==SLIST_RETURN_OK); ASSERT_REPORT(destroy(head)==SLIST_RETURN_OK); } |