/ 【例11 10】建立一個學生成績信息(包括學號、姓名、成績)的單向鏈表,學生數據按學號由小到大順序排列,要求實現對成績信息的插入、修改、刪除和遍歷操作。 / / 用鏈表實現學生成績信息的管理 / include include include struct stud_node{ int num; ...
/【例11-10】建立一個學生成績信息(包括學號、姓名、成績)的單向鏈表,學生數據按學號由小到大順序排列,要求實現對成績信息的插入、修改、刪除和遍歷操作。/
/* 用鏈表實現學生成績信息的管理 */
include<stdio.h>
include<stdlib.h>
include<string.h>
struct stud_node{
int num;
char name[20];
int score;
struct stud_node next;
};
struct stud_node Create_Stu_Doc(); /* 新建鏈表 /
struct stud_node InsertDoc(struct stud_node * head, struct stud_node stud); / 插入 /
struct stud_node DeleteDoc(struct stud_node * head, int num); /* 刪除 /
void Print_Stu_Doc(struct stud_node head); /* 遍歷 */
struct stud_node * Find(struct stud_node * head, int num); /查找/
struct stud_node * Modify(struct stud_node * head, int num, int score); /修改成績/
struct stud_node * Max(struct stud_node * head); /求最高分數的學生記錄/
struct stud_node * Min(struct stud_node * head); /求最低分數的學生記錄/
double Ave(struct stud_node * head); /求平均分/
void Save(struct stud_node * head);
struct stud_node *Read();
typedef struct stud_node *LNode;
LNode head,p;
int main(void)
{
struct stud_node head=NULL, p;
int choice, num, score;
char name[20];
int size = sizeof(struct stud_node);
do{
printf("1:Create 2:Insert 3:Delete 4:Print 5:Modify 6:Find 7:Max 8:Save 9:Read 10:Min 0:Exit\n");
scanf("%d", &choice);
switch(choice){
case 1:
head = Create_Stu_Doc();
break;
case 2:
printf("Input num,name and score:\n");
scanf("%d%s%d", &num,name, &score);
p = (struct stud_node *) malloc(size);
p->num = num;
strcpy(p->name, name);
p->score = score;
head = InsertDoc(head, p);
break;
case 3:
printf("Input num:\n");
scanf("%d", &num);
head = DeleteDoc(head, num);
break;
case 4:
Print_Stu_Doc(head);
break;
case 5:
printf("Input num:\n");
scanf("%d", &num);
printf("Input score:\n");
scanf("%d", &score);
head=Modify(head,num,score);
break;
case 6:
printf("Input num:\n");
scanf("%d", &num);
p=Find(head,num);
if(p!=NULL)
printf("%d\t%s\t%d \n", p->num, p->name, p->score);
else
printf("Not Found %d\n",num);
break;
case 7:
p=Max(head);
if(p!=NULL)
printf("%d\t%s\t%d \n", p->num, p->name, p->score);
else
printf("Not Found\n");
break;
case 8:
Save(head);
break;
case 9:
head=Read();
break;
case 10:
p=Min(head);
if(p!=NULL)
printf("%d\t%s\t%d \n", p->num, p->name, p->score);
else
printf("Not Found\n");
break;
case 0:
break;
}
}while(choice != 0);
return 0;
}
/新建鏈表/
struct stud_node * Create_Stu_Doc()
{
struct stud_node * head,*p;
int num,score;
char name[20];
int size = sizeof(struct stud_node);
head = NULL;
printf("Input num,name and score:\n");
scanf("%d%s%d", &num,name, &score);
while(num != 0){ /* 學號0表示輸入結束,且學號0對應的姓名和成績都要輸入 */
p = (struct stud_node *) malloc(size);
p->num = num;
strcpy(p->name, name);
p->score = score;
head = InsertDoc(head, p); /* 調用插入函數 */
scanf("%d%s%d", &num, name, &score);
}
return head;
}
/* 插入操作 /
struct stud_node InsertDoc(struct stud_node * head, struct stud_node stud)
{
struct stud_node ptr ,ptr1, ptr2;
ptr2 = head;
ptr = stud; /* ptr指向待插入的新的學生記錄結點 */
if(head == NULL){ /* 原鏈表為空時的插入 */
head = ptr; /* 新插入結點成為頭結點 */
head->next = NULL;
}
else{ /* 原鏈表不為空時的插入 */
while((ptr->num > ptr2->num) && (ptr2->next != NULL)){
ptr1 = ptr2; /* ptr1, ptr2各後移一個結點 */
ptr2 = ptr2->next;
}
if(ptr->num <= ptr2->num){
if(head == ptr2) head = ptr;/* 新插入結點成為頭結點 */
else ptr1->next = ptr; /* 在ptr1與ptr2之間插入新結點 */
ptr->next = ptr2;
}
else{ /* 新插入結點成為尾結點 */
ptr2->next = ptr;
ptr->next = NULL;
}
}
return head;
}
/* 刪除操作 /
struct stud_node DeleteDoc(struct stud_node * head, int num)
{
struct stud_node ptr1, ptr2;
if(head == NULL) /*鏈表空 */
return NULL;
else if(head->num == num){
/* 要被刪除結點為表頭結點 */
while(head != NULL && head->num == num){
ptr2 = head;
head = head->next;
free(ptr2);
}
}
else{
/* 要被刪除結點為非表頭結點 */
ptr1 = head;
ptr2 = head->next; /*從表頭的下一個結點搜索所有符合刪除要求的結點 */
while(ptr2 != NULL){
if(ptr2->num == num){ /* ptr2所指結點符合刪除要求 */
ptr1->next = ptr2->next;
free(ptr2);
}
else
ptr1 = ptr2; /* ptr1後移一個結點 */
ptr2 = ptr1->next; /* ptr2指向ptr1的後一個結點 */
}
}
return head;
}
/遍歷操作/
void Print_Stu_Doc(struct stud_node * head)
{
struct stud_node * ptr;
if(head == NULL){
printf("\nNo Records\n");
return;
}
printf("\nThe Students' Records Are: \n");
printf("Num\tName\tScore\n");
for(ptr = head; ptr != NULL; ptr = ptr->next)
printf("%d\t%s\t%d \n", ptr->num, ptr->name, ptr->score);
}
struct stud_node * Find(struct stud_node * head, int num) /查找/
{
struct stud_node *ptr;
if(head==NULL)
return NULL;
else
{
for(ptr=head;ptr!=NULL;ptr=ptr->next)/*for迴圈從head開始到NULL迴圈*/
if(ptr->num==num)
break;
return ptr;
}
}
struct stud_node * Modify(struct stud_node * head, int num, int score)/修改通過查找學號修改成績/
{
struct stud_node *ptr;
if(head==NULL)
return NULL;
else
{
for(ptr=head;ptr!=NULL;ptr=ptr->next)
if(ptr->num==num)
{
ptr->score=score;
break;
}
return head;
}
}
struct stud_node * Max(struct stud_node * head)
{
struct stud_node maxp,ptr;
if(head==NULL)
return NULL;
else
{
maxp=head;
for(ptr=head->next;ptr!=NULL;ptr=ptr->next)
if(maxp->score<ptr->score)
maxp=ptr;
return maxp;
}
}
struct stud_node * Min(struct stud_node * head)
{
struct stud_node minp,ptr;
if(head==NULL)
return NULL;
else
{
minp=head;
for(ptr=head->next;ptr!=NULL;ptr=ptr->next)
if(minp->score>ptr->score)
minp=ptr;
return minp;
}
}
void Save(struct stud_node * head)
{
FILE fp;
if((fp=fopen("data.txt","w")) == NULL){ /打開文件(套用)*/
printf("File open error!\n");
exit(0);
}
struct stud_node * ptr;
if(head == NULL){
return;
}
for(ptr = head; ptr->next != NULL; ptr = ptr->next)
fprintf(fp,"%d %s %d\n", ptr->num, ptr->name, ptr->score);
fprintf(fp,"%d %s %d", ptr->num, ptr->name, ptr->score);/*輸出的最後沒有換行*/
if( fclose(fp) ){/*關閉文件(套用)*/
printf( "Can not close the file!\n" );
exit(0);
}
}
struct stud_node Read()
{
FILE fp;
if((fp=fopen("data.txt","r")) == NULL){
printf("File open error!\n");
exit(0);
}
struct stud_node * head,*tail,*p;
//int num,score;
//char name[20];
int size = sizeof(struct stud_node);
head = tail=NULL;
while(!feof(fp)){ /* 讀到結束標誌結束 */
p = (struct stud_node *) malloc(size);
fscanf(fp,"%d%s%d", &p->num, p->name, &p->score);
//head = InsertDoc(head, p); /* 調用插入函數 */
p->next = NULL;
if(head == NULL)
head = p;
else
tail->next = p;
tail = p;
}
if( fclose(fp) ){
printf( "Can not close the file!\n" );
exit(0);
}
return head;
}