卓越14U14716彭佳伟第十九周.docx
《卓越14U14716彭佳伟第十九周.docx》由会员分享,可在线阅读,更多相关《卓越14U14716彭佳伟第十九周.docx(15页珍藏版)》请在冰豆网上搜索。
卓越14U14716彭佳伟第十九周
12.5编写一个测试一个字符串是否为回文的递归函数,是回文,返回1;不是回文,返回0。
voidjudge(char*str,intn)
{
intstart=strlen(str)-n;
intend=n-1;
if(str[start]==str[end])
if(end-start>1)
judge(str,n-1);
}
else
printf("1\n");
printf("0\n");
13.1编写带哨兵的直接插入排序算法,将哨兵放在a[0]中,被排序的纪录放在a[1]~a[n-1]中。
#include
#ifndefMAX
#defineMAX8
#endif
intmain(intargc,charconst*argv[])
inta[MAX]={0,1,2,5,3,6,3,-2};
intj,i;
inttmp;
for(i=2;i{a[0]=a[i];for(j=i-1;a[0]{a[j+1]=a[j];}a[j+1]=a[0];}for(i=1;i{printf("%d\n",a[i]);}return0;}13.6改写快速排序算法,要求采用三者(首元素、中间元素、尾元素)取中的方式选择分区元素;若当前被排序的去见长度小雨等于3,无需划分而是采用直接插入方式对其排序。voidQuickSort(inta[],intleft,intright){intsplit;if(right-left>3){split=Partition(a,left,right);QuickSort(a,left,split-1);QuickSort(a,split+1,right);}elseif(right-left<=3&&right-left>0){InsertSort(a,left,right);}}intPartition(inta[],intleft,intright){intsplit=(left+right)/2;swap(a,left,split);inti=left;intj=right+1;while(1){while(a[++i]<=a[left]&&i<=right);while(a[--j]>a[left]);if(i>=j){break;}swap(a,i,j);}swap(a,j,left);returnj;}voidswap(inta[],inti,intj){inttemp;temp=a[i];a[i]=a[j];a[j]=temp;}voidInsertSort(inta[],intleft,intright){inti;intj;inttemp;for(i=left+1;i<=right;i++){temp=a[i];for(j=i-1;j>=0&&a[j]>temp;j--){a[j+1]=a[j];}a[j+1]=temp;}}14.4输入一行长度无限制超长字符串,用一个先进先出且每个节点接受一个输入字符的单向链表接受这个字符串。再完成下列任务:(1)遍历输出链表中的所有字符;(2)将该超长字符串无冗余地存放到一个通过动态存储分配创建地字符数组中,并且通过puts函数或printf函数输出该超长字符串。#include#includetypedefstructStringString;structString{charch;String*next;};voidSaveSring(String**head);voidPrintString(String*head);voidSaveToArray(String*head);intmain(void){String*head=NULL;printf("Pleaseinputastring\n");SaveSring(&head);PrintString(head);SaveToArray(head);return0;}voidSaveSring(String**head){String*current=NULL;String*previous=NULL;while(1){current=(String*)malloc(sizeof(String));if(*head==NULL){*head=current;}if(previous!=NULL)previous->next=current;previous=current;current->next=NULL;scanf("%c",¤t->ch);if(current->ch=='\n'){current->ch='\0';current->next=NULL;break;}}}voidPrintString(String*head){while(head!=NULL){printf("%c",head->ch);head=head->next;}printf("\n");}voidSaveToArray(String*head){intcnt;String*temp=head;for(cnt=1;temp->next!=NULL;cnt++){temp=temp->next;}charstring[cnt];for(inti=0;i{string[i]=head->ch;head=head->next;}puts(string);}14.5用后进先出单向链表重做第14.3。#include#include#include#includetypedefstructStudentStudent;structStudent{intnumber;charname[10];floatsubject1;floatsubject2;floatsubject3;floataverage;Student*next;};voidInput(Student**head);voidOutput(Student*head);voidChangeInfo(Student*head);voidSort(Student*head);intmain(void){Student*head=NULL;intn;intflag=1;while(flag==1){printf("Pleaseinputthenumberofoptions:\n");printf("1:InputInformation\t2:ChangeInformation\n");printf("3:OutputInformation\t4:SortInformation\n");printf("5:Quit\n");scanf("%d",&n);switch(n){case1:Input(&head);break;case2:ChangeInfo(head);break;case3:Output(head);break;case4:Sort(head);break;case5:flag=0;break;default:printf("IllegalInput!Pleaseinputagain.");break;}printf("\n\n");}return0;}voidInput(Student**head){//采用的是后进先出的单向链表charanswer='y';while(answer=='y'){Student*current=NULL;current=(Student*)malloc(sizeof(Student));current->next=*head;*head=current;printf("Pleaseinputthestudent'snumber:");scanf("%d",¤t->number);printf("Pleaseinputthestudent'sname:");scanf("%s",current->name);printf("Pleaseinputthescoreofsubject1:");scanf("%f",¤t->subject1);printf("Pleaseinputthescoreofsubject2:");scanf("%f",¤t->subject2);printf("Pleaseinputthescoreofsubject3:");scanf("%f",¤t->subject3);current->average=(current->subject1+current->subject2+current->subject3)/3;printf("Doyouwanttoinputtheinfomationofanotherstudent?(Y/N)");scanf("%c",&answer);answer=tolower(answer);}}voidOutput(Student*head){if(head==NULL){printf("Youhavenotinputanyinformation!");}else{printf("\n\n\t\tTheInfomationOfStudents\n");do{printf("name:%s\t\t",head->name);printf("number:%d\t\t",head->number);printf("average:%.2f\n",head->average);printf("subject1:%.2f\t",head->subject1);printf("subject2:%.2f\t",head->subject2);printf("subject3:%.2f\n\n",head->subject3);head=head->next;}while(head!=NULL);}}voidChangeInfo(Student*head){if(head==NULL){printf("Youhavenotinputanyinformation!");}else{Student*current=head;charname[10];intflag=0;intn;printf("Pleaseinputthestudent'sname:");scanf("%s",name);while(current->next!=NULL){if(strcmp(name,current->name)!=0){current=current->next;}else{flag=1;printf("Pleaseinputthenumberofoptions:\n");printf("1:subject1\t2:subject2\t3:subject3\t4:name\t5:number\n");scanf("%d",&n);switch(n){case1:printf("Pleaseinputthescoreofsubject1:");scanf("%f",¤t->subject1);break;case2:printf("Pleaseinputthescoreofsubject2:");scanf("%f",¤t->subject2);break;case3:printf("Pleaseinputthescoreofsubject3:");scanf("%f",¤t->subject3);break;case4:printf("Pleaseinputthestudent'sname:");scanf("%s",current->name);break;case5:printf("Pleaseinputthestudent'snumber:");scanf("%d",¤t->number);default:break;}}if(n<=3)current->average=(current->subject1+current->subject2+current->subject3)/3;break;}}}voidSort(Student*head){//采用的是冒泡排序//交换的是数据域,不是指针if(head==NULL){printf("Youhavenotinputanyinformation!");}else{Studenttemp;while(head->next!=NULL){Student*current=head;while(current->next!=NULL){if(current->averagenext->average){temp=*current;temp.next=current->next->next;current->next->next=current->next;*current=*current->next;*current->next=temp;}current=current->next;}head=head->next;}}}
a[0]=a[i];
for(j=i-1;a[0]{a[j+1]=a[j];}a[j+1]=a[0];}for(i=1;i{printf("%d\n",a[i]);}return0;}13.6改写快速排序算法,要求采用三者(首元素、中间元素、尾元素)取中的方式选择分区元素;若当前被排序的去见长度小雨等于3,无需划分而是采用直接插入方式对其排序。voidQuickSort(inta[],intleft,intright){intsplit;if(right-left>3){split=Partition(a,left,right);QuickSort(a,left,split-1);QuickSort(a,split+1,right);}elseif(right-left<=3&&right-left>0){InsertSort(a,left,right);}}intPartition(inta[],intleft,intright){intsplit=(left+right)/2;swap(a,left,split);inti=left;intj=right+1;while(1){while(a[++i]<=a[left]&&i<=right);while(a[--j]>a[left]);if(i>=j){break;}swap(a,i,j);}swap(a,j,left);returnj;}voidswap(inta[],inti,intj){inttemp;temp=a[i];a[i]=a[j];a[j]=temp;}voidInsertSort(inta[],intleft,intright){inti;intj;inttemp;for(i=left+1;i<=right;i++){temp=a[i];for(j=i-1;j>=0&&a[j]>temp;j--){a[j+1]=a[j];}a[j+1]=temp;}}14.4输入一行长度无限制超长字符串,用一个先进先出且每个节点接受一个输入字符的单向链表接受这个字符串。再完成下列任务:(1)遍历输出链表中的所有字符;(2)将该超长字符串无冗余地存放到一个通过动态存储分配创建地字符数组中,并且通过puts函数或printf函数输出该超长字符串。#include#includetypedefstructStringString;structString{charch;String*next;};voidSaveSring(String**head);voidPrintString(String*head);voidSaveToArray(String*head);intmain(void){String*head=NULL;printf("Pleaseinputastring\n");SaveSring(&head);PrintString(head);SaveToArray(head);return0;}voidSaveSring(String**head){String*current=NULL;String*previous=NULL;while(1){current=(String*)malloc(sizeof(String));if(*head==NULL){*head=current;}if(previous!=NULL)previous->next=current;previous=current;current->next=NULL;scanf("%c",¤t->ch);if(current->ch=='\n'){current->ch='\0';current->next=NULL;break;}}}voidPrintString(String*head){while(head!=NULL){printf("%c",head->ch);head=head->next;}printf("\n");}voidSaveToArray(String*head){intcnt;String*temp=head;for(cnt=1;temp->next!=NULL;cnt++){temp=temp->next;}charstring[cnt];for(inti=0;i{string[i]=head->ch;head=head->next;}puts(string);}14.5用后进先出单向链表重做第14.3。#include#include#include#includetypedefstructStudentStudent;structStudent{intnumber;charname[10];floatsubject1;floatsubject2;floatsubject3;floataverage;Student*next;};voidInput(Student**head);voidOutput(Student*head);voidChangeInfo(Student*head);voidSort(Student*head);intmain(void){Student*head=NULL;intn;intflag=1;while(flag==1){printf("Pleaseinputthenumberofoptions:\n");printf("1:InputInformation\t2:ChangeInformation\n");printf("3:OutputInformation\t4:SortInformation\n");printf("5:Quit\n");scanf("%d",&n);switch(n){case1:Input(&head);break;case2:ChangeInfo(head);break;case3:Output(head);break;case4:Sort(head);break;case5:flag=0;break;default:printf("IllegalInput!Pleaseinputagain.");break;}printf("\n\n");}return0;}voidInput(Student**head){//采用的是后进先出的单向链表charanswer='y';while(answer=='y'){Student*current=NULL;current=(Student*)malloc(sizeof(Student));current->next=*head;*head=current;printf("Pleaseinputthestudent'snumber:");scanf("%d",¤t->number);printf("Pleaseinputthestudent'sname:");scanf("%s",current->name);printf("Pleaseinputthescoreofsubject1:");scanf("%f",¤t->subject1);printf("Pleaseinputthescoreofsubject2:");scanf("%f",¤t->subject2);printf("Pleaseinputthescoreofsubject3:");scanf("%f",¤t->subject3);current->average=(current->subject1+current->subject2+current->subject3)/3;printf("Doyouwanttoinputtheinfomationofanotherstudent?(Y/N)");scanf("%c",&answer);answer=tolower(answer);}}voidOutput(Student*head){if(head==NULL){printf("Youhavenotinputanyinformation!");}else{printf("\n\n\t\tTheInfomationOfStudents\n");do{printf("name:%s\t\t",head->name);printf("number:%d\t\t",head->number);printf("average:%.2f\n",head->average);printf("subject1:%.2f\t",head->subject1);printf("subject2:%.2f\t",head->subject2);printf("subject3:%.2f\n\n",head->subject3);head=head->next;}while(head!=NULL);}}voidChangeInfo(Student*head){if(head==NULL){printf("Youhavenotinputanyinformation!");}else{Student*current=head;charname[10];intflag=0;intn;printf("Pleaseinputthestudent'sname:");scanf("%s",name);while(current->next!=NULL){if(strcmp(name,current->name)!=0){current=current->next;}else{flag=1;printf("Pleaseinputthenumberofoptions:\n");printf("1:subject1\t2:subject2\t3:subject3\t4:name\t5:number\n");scanf("%d",&n);switch(n){case1:printf("Pleaseinputthescoreofsubject1:");scanf("%f",¤t->subject1);break;case2:printf("Pleaseinputthescoreofsubject2:");scanf("%f",¤t->subject2);break;case3:printf("Pleaseinputthescoreofsubject3:");scanf("%f",¤t->subject3);break;case4:printf("Pleaseinputthestudent'sname:");scanf("%s",current->name);break;case5:printf("Pleaseinputthestudent'snumber:");scanf("%d",¤t->number);default:break;}}if(n<=3)current->average=(current->subject1+current->subject2+current->subject3)/3;break;}}}voidSort(Student*head){//采用的是冒泡排序//交换的是数据域,不是指针if(head==NULL){printf("Youhavenotinputanyinformation!");}else{Studenttemp;while(head->next!=NULL){Student*current=head;while(current->next!=NULL){if(current->averagenext->average){temp=*current;temp.next=current->next->next;current->next->next=current->next;*current=*current->next;*current->next=temp;}current=current->next;}head=head->next;}}}
a[j+1]=a[j];
a[j+1]=a[0];
for(i=1;i{printf("%d\n",a[i]);}return0;}13.6改写快速排序算法,要求采用三者(首元素、中间元素、尾元素)取中的方式选择分区元素;若当前被排序的去见长度小雨等于3,无需划分而是采用直接插入方式对其排序。voidQuickSort(inta[],intleft,intright){intsplit;if(right-left>3){split=Partition(a,left,right);QuickSort(a,left,split-1);QuickSort(a,split+1,right);}elseif(right-left<=3&&right-left>0){InsertSort(a,left,right);}}intPartition(inta[],intleft,intright){intsplit=(left+right)/2;swap(a,left,split);inti=left;intj=right+1;while(1){while(a[++i]<=a[left]&&i<=right);while(a[--j]>a[left]);if(i>=j){break;}swap(a,i,j);}swap(a,j,left);returnj;}voidswap(inta[],inti,intj){inttemp;temp=a[i];a[i]=a[j];a[j]=temp;}voidInsertSort(inta[],intleft,intright){inti;intj;inttemp;for(i=left+1;i<=right;i++){temp=a[i];for(j=i-1;j>=0&&a[j]>temp;j--){a[j+1]=a[j];}a[j+1]=temp;}}14.4输入一行长度无限制超长字符串,用一个先进先出且每个节点接受一个输入字符的单向链表接受这个字符串。再完成下列任务:(1)遍历输出链表中的所有字符;(2)将该超长字符串无冗余地存放到一个通过动态存储分配创建地字符数组中,并且通过puts函数或printf函数输出该超长字符串。#include#includetypedefstructStringString;structString{charch;String*next;};voidSaveSring(String**head);voidPrintString(String*head);voidSaveToArray(String*head);intmain(void){String*head=NULL;printf("Pleaseinputastring\n");SaveSring(&head);PrintString(head);SaveToArray(head);return0;}voidSaveSring(String**head){String*current=NULL;String*previous=NULL;while(1){current=(String*)malloc(sizeof(String));if(*head==NULL){*head=current;}if(previous!=NULL)previous->next=current;previous=current;current->next=NULL;scanf("%c",¤t->ch);if(current->ch=='\n'){current->ch='\0';current->next=NULL;break;}}}voidPrintString(String*head){while(head!=NULL){printf("%c",head->ch);head=head->next;}printf("\n");}voidSaveToArray(String*head){intcnt;String*temp=head;for(cnt=1;temp->next!=NULL;cnt++){temp=temp->next;}charstring[cnt];for(inti=0;i{string[i]=head->ch;head=head->next;}puts(string);}14.5用后进先出单向链表重做第14.3。#include#include#include#includetypedefstructStudentStudent;structStudent{intnumber;charname[10];floatsubject1;floatsubject2;floatsubject3;floataverage;Student*next;};voidInput(Student**head);voidOutput(Student*head);voidChangeInfo(Student*head);voidSort(Student*head);intmain(void){Student*head=NULL;intn;intflag=1;while(flag==1){printf("Pleaseinputthenumberofoptions:\n");printf("1:InputInformation\t2:ChangeInformation\n");printf("3:OutputInformation\t4:SortInformation\n");printf("5:Quit\n");scanf("%d",&n);switch(n){case1:Input(&head);break;case2:ChangeInfo(head);break;case3:Output(head);break;case4:Sort(head);break;case5:flag=0;break;default:printf("IllegalInput!Pleaseinputagain.");break;}printf("\n\n");}return0;}voidInput(Student**head){//采用的是后进先出的单向链表charanswer='y';while(answer=='y'){Student*current=NULL;current=(Student*)malloc(sizeof(Student));current->next=*head;*head=current;printf("Pleaseinputthestudent'snumber:");scanf("%d",¤t->number);printf("Pleaseinputthestudent'sname:");scanf("%s",current->name);printf("Pleaseinputthescoreofsubject1:");scanf("%f",¤t->subject1);printf("Pleaseinputthescoreofsubject2:");scanf("%f",¤t->subject2);printf("Pleaseinputthescoreofsubject3:");scanf("%f",¤t->subject3);current->average=(current->subject1+current->subject2+current->subject3)/3;printf("Doyouwanttoinputtheinfomationofanotherstudent?(Y/N)");scanf("%c",&answer);answer=tolower(answer);}}voidOutput(Student*head){if(head==NULL){printf("Youhavenotinputanyinformation!");}else{printf("\n\n\t\tTheInfomationOfStudents\n");do{printf("name:%s\t\t",head->name);printf("number:%d\t\t",head->number);printf("average:%.2f\n",head->average);printf("subject1:%.2f\t",head->subject1);printf("subject2:%.2f\t",head->subject2);printf("subject3:%.2f\n\n",head->subject3);head=head->next;}while(head!=NULL);}}voidChangeInfo(Student*head){if(head==NULL){printf("Youhavenotinputanyinformation!");}else{Student*current=head;charname[10];intflag=0;intn;printf("Pleaseinputthestudent'sname:");scanf("%s",name);while(current->next!=NULL){if(strcmp(name,current->name)!=0){current=current->next;}else{flag=1;printf("Pleaseinputthenumberofoptions:\n");printf("1:subject1\t2:subject2\t3:subject3\t4:name\t5:number\n");scanf("%d",&n);switch(n){case1:printf("Pleaseinputthescoreofsubject1:");scanf("%f",¤t->subject1);break;case2:printf("Pleaseinputthescoreofsubject2:");scanf("%f",¤t->subject2);break;case3:printf("Pleaseinputthescoreofsubject3:");scanf("%f",¤t->subject3);break;case4:printf("Pleaseinputthestudent'sname:");scanf("%s",current->name);break;case5:printf("Pleaseinputthestudent'snumber:");scanf("%d",¤t->number);default:break;}}if(n<=3)current->average=(current->subject1+current->subject2+current->subject3)/3;break;}}}voidSort(Student*head){//采用的是冒泡排序//交换的是数据域,不是指针if(head==NULL){printf("Youhavenotinputanyinformation!");}else{Studenttemp;while(head->next!=NULL){Student*current=head;while(current->next!=NULL){if(current->averagenext->average){temp=*current;temp.next=current->next->next;current->next->next=current->next;*current=*current->next;*current->next=temp;}current=current->next;}head=head->next;}}}
printf("%d\n",a[i]);
return0;
13.6改写快速排序算法,要求采用三者(首元素、中间元素、尾元素)取中的方式选择分区元素;若当前被排序的去见长度小雨等于3,无需划分而是采用直接插入方式对其排序。
voidQuickSort(inta[],intleft,intright)
intsplit;
if(right-left>3)
split=Partition(a,left,right);
QuickSort(a,left,split-1);
QuickSort(a,split+1,right);
elseif(right-left<=3&&right-left>0)
InsertSort(a,left,right);
intPartition(inta[],intleft,intright)
intsplit=(left+right)/2;
swap(a,left,split);
inti=left;
intj=right+1;
while
(1)
while(a[++i]<=a[left]&&i<=right);
while(a[--j]>a[left]);
if(i>=j)
break;
swap(a,i,j);
swap(a,j,left);
returnj;
voidswap(inta[],inti,intj)
inttemp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
voidInsertSort(inta[],intleft,intright)
inti;
intj;
for(i=left+1;i<=right;i++)
for(j=i-1;j>=0&&a[j]>temp;j--)
a[j+1]=temp;
14.4输入一行长度无限制超长字符串,用一个先进先出且每个节点接受一个输入字符的单向链表接受这个字符串。
再完成下列任务:
(1)遍历输出链表中的所有字符;
(2)将该超长字符串无冗余地存放到一个通过动态存储分配创建地字符数组中,并且通过puts函数或printf函数输出该超长字符串。
typedefstructStringString;
structString
charch;
String*next;
};
voidSaveSring(String**head);
voidPrintString(String*head);
voidSaveToArray(String*head);
intmain(void)
String*head=NULL;
printf("Pleaseinputastring\n");
SaveSring(&head);
PrintString(head);
SaveToArray(head);
voidSaveSring(String**head)
String*current=NULL;
String*previous=NULL;
current=(String*)malloc(sizeof(String));
if(*head==NULL)
*head=current;
if(previous!
=NULL)
previous->next=current;
previous=current;
current->next=NULL;
scanf("%c",¤t->ch);
if(current->ch=='\n')
current->ch='\0';
voidPrintString(String*head)
while(head!
printf("%c",head->ch);
head=head->next;
printf("\n");
voidSaveToArray(String*head)
intcnt;
String*temp=head;
for(cnt=1;temp->next!
=NULL;cnt++)
temp=temp->next;
charstring[cnt];
for(inti=0;i{string[i]=head->ch;head=head->next;}puts(string);}14.5用后进先出单向链表重做第14.3。#include#include#include#includetypedefstructStudentStudent;structStudent{intnumber;charname[10];floatsubject1;floatsubject2;floatsubject3;floataverage;Student*next;};voidInput(Student**head);voidOutput(Student*head);voidChangeInfo(Student*head);voidSort(Student*head);intmain(void){Student*head=NULL;intn;intflag=1;while(flag==1){printf("Pleaseinputthenumberofoptions:\n");printf("1:InputInformation\t2:ChangeInformation\n");printf("3:OutputInformation\t4:SortInformation\n");printf("5:Quit\n");scanf("%d",&n);switch(n){case1:Input(&head);break;case2:ChangeInfo(head);break;case3:Output(head);break;case4:Sort(head);break;case5:flag=0;break;default:printf("IllegalInput!Pleaseinputagain.");break;}printf("\n\n");}return0;}voidInput(Student**head){//采用的是后进先出的单向链表charanswer='y';while(answer=='y'){Student*current=NULL;current=(Student*)malloc(sizeof(Student));current->next=*head;*head=current;printf("Pleaseinputthestudent'snumber:");scanf("%d",¤t->number);printf("Pleaseinputthestudent'sname:");scanf("%s",current->name);printf("Pleaseinputthescoreofsubject1:");scanf("%f",¤t->subject1);printf("Pleaseinputthescoreofsubject2:");scanf("%f",¤t->subject2);printf("Pleaseinputthescoreofsubject3:");scanf("%f",¤t->subject3);current->average=(current->subject1+current->subject2+current->subject3)/3;printf("Doyouwanttoinputtheinfomationofanotherstudent?(Y/N)");scanf("%c",&answer);answer=tolower(answer);}}voidOutput(Student*head){if(head==NULL){printf("Youhavenotinputanyinformation!");}else{printf("\n\n\t\tTheInfomationOfStudents\n");do{printf("name:%s\t\t",head->name);printf("number:%d\t\t",head->number);printf("average:%.2f\n",head->average);printf("subject1:%.2f\t",head->subject1);printf("subject2:%.2f\t",head->subject2);printf("subject3:%.2f\n\n",head->subject3);head=head->next;}while(head!=NULL);}}voidChangeInfo(Student*head){if(head==NULL){printf("Youhavenotinputanyinformation!");}else{Student*current=head;charname[10];intflag=0;intn;printf("Pleaseinputthestudent'sname:");scanf("%s",name);while(current->next!=NULL){if(strcmp(name,current->name)!=0){current=current->next;}else{flag=1;printf("Pleaseinputthenumberofoptions:\n");printf("1:subject1\t2:subject2\t3:subject3\t4:name\t5:number\n");scanf("%d",&n);switch(n){case1:printf("Pleaseinputthescoreofsubject1:");scanf("%f",¤t->subject1);break;case2:printf("Pleaseinputthescoreofsubject2:");scanf("%f",¤t->subject2);break;case3:printf("Pleaseinputthescoreofsubject3:");scanf("%f",¤t->subject3);break;case4:printf("Pleaseinputthestudent'sname:");scanf("%s",current->name);break;case5:printf("Pleaseinputthestudent'snumber:");scanf("%d",¤t->number);default:break;}}if(n<=3)current->average=(current->subject1+current->subject2+current->subject3)/3;break;}}}voidSort(Student*head){//采用的是冒泡排序//交换的是数据域,不是指针if(head==NULL){printf("Youhavenotinputanyinformation!");}else{Studenttemp;while(head->next!=NULL){Student*current=head;while(current->next!=NULL){if(current->averagenext->average){temp=*current;temp.next=current->next->next;current->next->next=current->next;*current=*current->next;*current->next=temp;}current=current->next;}head=head->next;}}}
string[i]=head->ch;
puts(string);
14.5用后进先出单向链表重做第14.3。
typedefstructStudentStudent;
structStudent
intnumber;
charname[10];
floatsubject1;
floatsubject2;
floatsubject3;
floataverage;
Student*next;
voidInput(Student**head);
voidOutput(Student*head);
voidChangeInfo(Student*head);
voidSort(Student*head);
Student*head=NULL;
intn;
intflag=1;
while(flag==1)
printf("Pleaseinputthenumberofoptions:
\n");
printf("1:
InputInformation\t2:
ChangeInformation\n");
printf("3:
OutputInformation\t4:
SortInformation\n");
printf("5:
Quit\n");
scanf("%d",&n);
switch(n)
case1:
Input(&head);
case2:
ChangeInfo(head);
case3:
Output(head);
case4:
Sort(head);
case5:
flag=0;
default:
printf("IllegalInput!
Pleaseinputagain.");
printf("\n\n");
voidInput(Student**head)
//采用的是后进先出的单向链表
charanswer='y';
while(answer=='y')
Student*current=NULL;
current=(Student*)malloc(sizeof(Student));
current->next=*head;
printf("Pleaseinputthestudent'snumber:
");
scanf("%d",¤t->number);
printf("Pleaseinputthestudent'sname:
scanf("%s",current->name);
printf("Pleaseinputthescoreofsubject1:
scanf("%f",¤t->subject1);
printf("Pleaseinputthescoreofsubject2:
scanf("%f",¤t->subject2);
printf("Pleaseinputthescoreofsubject3:
scanf("%f",¤t->subject3);
current->average=(current->subject1+current->subject2+current->subject3)/3;
printf("Doyouwanttoinputtheinfomationofanotherstudent?
(Y/N)");
scanf("%c",&answer);
answer=tolower(answer);
voidOutput(Student*head)
if(head==NULL)
printf("Youhavenotinputanyinformation!
printf("\n\n\t\tTheInfomationOfStudents\n");
do{
printf("name:
%s\t\t",head->name);
printf("number:
%d\t\t",head->number);
printf("average:
%.2f\n",head->average);
printf("subject1:
%.2f\t",head->subject1);
printf("subject2:
%.2f\t",head->subject2);
printf("subject3:
%.2f\n\n",head->subject3);
}while(head!
=NULL);
voidChangeInfo(Student*head)
Student*current=head;
intflag=0;
scanf("%s",name);
while(current->next!
if(strcmp(name,current->name)!
=0)
current=current->next;
flag=1;
subject1\t2:
subject2\t3:
subject3\t4:
name\t5:
number\n");
if(n<=3)
voidSort(Student*head)
//采用的是冒泡排序
//交换的是数据域,不是指针
Studenttemp;
while(head->next!
if(current->averagenext->average)
temp=*current;
temp.next=current->next->next;
current->next->next=current->next;
*current=*current->next;
*current->next=temp;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1