计算机教程
st score is:%s,score total:%d.”stu[maxi].name,max); } 11.6 编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针,指向字 符串开始的空间。New(n)表示分配n个字节的内存空间。 解:new函数如下: #define NULL 0 #define NEWSIZE 1000 char newbuf[NEWSIZE]; char *newp=newbuf; char *new(int n) {if (newp+n<=newbuf+ NEWSIZE) { newp= newp+n; return(newp-n); } else return(NULL); } 11.7写一函数free,将上题用new函数占用的空间释放。Free(p)表示将p指向的单元以后的
内存段释放。 解: #define Null o #define NEWSIZE 1000 char newbuf[NEWSIZE]; char *newp=newbuf; free(char *p) {if((p>=newbuf)&&(p<newbuf+NEWSIZE)) newp=p; } 11.8已有a、b亮光链表,每个链表中的结点包括学好、成绩。要求把两个链表合并,按学 号升序排列。 解: #include #define NULL 0 #define LENsizeof(struct student) strut student {long num; int scor; struct student *next }; struct student listA,listB; int n,sum=0; main() {struct student *creat(void); struct student *insert(struct student *,struct student *); void print(struct student *); stuct student *ahead , *bhead,*abh; printf(“\ninput list a:\n”); ahead=creat(); sum=sum+|n; abh=insert(ahead,bhead); print(abh); } struct student *creat(void) {struct student *p1,*p2,*head; n=0; p1=p2=(struct student *)malloc(LEN); printf(“input number&scores of student:\n”); printf(“if number Is 0,stop inputing.\n”); scanf(“%ld,%d”,&p1->num,&p1->score); head=NULL; while(p1->num!=0) {n=n+1; if(n==1)head=p1; else p2->next =p1; p2=p1; p1=(struct student *)malloc(LEN); scanf(“%ld,,%d”,&p1->num,&p1->score); } p2->next=NULL; return(head); } struct student *insert(struct student *ah,struct student *bh) {struct student *pa1 , *pa2,*pb1,*pb2; pa2=pa1=ah; pb2=pb1=bh;
do {while((pb1->num>pa1->num)&&(pa1->next!=NULL)) {pa2=pa1; pa1=pa1->next; } if(pb->num<=pa1->num) {if(ah=pa1) ah=pb1; else pa2->next=pb1; pb1=pb1->next; pb2->next=pa1; pa2=pb2; pb2=pb1; } } while((pa1->next!=NULL)||(pa1==NULL&&pb1!=NULL)); if((pb1->num>pa1->num)&&(pa1->next==NULl)) ap1->next=pb1; return(ah); } void print(struct student *head) {struct student *p; printf(“%ld%d\n”,p->num,p->score); p=p->next; while(p!=NULL); } 11.9 13个人围成一圈,从第1个人开始顺序报号1、2、3。凡报到“3”者退出圈子。找出 最后留在圈子中的人原来的序号。 解: #define N 13 struct person {int number; int nextop; }link[N+1]; main() {int I,count,h; for(I=1;I<=N;I++) {if(I==N) link[i].nextp=1; else link[i].nextp=I+1; link[i].number=I; } printf(“\n”); count=0; h=N; printf(“sequence that person2 leave the circle:\n”); while(count<N-1) {I=0; while(I!=3) {h=link[h].nextp; if(link[h].number) I++; } printf(“%4d”,link[h].number); link[h].number=0; count++;
} printf(“\nThe last one is”); for(I=1;ii<=N;I++) if(link[i].number) printf(“%3d”,lin[i].number); } 11.10有两个链表a和b,设结点中包含学号、姓名。从1链表中删去与b链表中有相同学号的 那些结点。 解: #define LA 4 #define LB 5 #define NULL 0 struct student {char nump[6]; char name[8]; struct student *next; }A[LA],b[LB]; main() {struct student a[LA]={{“101”,”Wang”},{“102”,”LI”},{“105”,”zhang”},{“106”,”Wei”}}; struct studentb[LB]={{“103”,”Zhang”},{“104”,”Ma”},{“105”,”Chen”},{“107”,”Guo”}, {“108”,”Lui”}}; int I,j; struct student *p, *p1,*p2,*pt,*head1,*head2; head1=a; head2=b; printf(“list a :\n”); for(p1=head1,i=1;p1next=a+I; printf(“%8s%8s\n”,p1->num,p1->name); p1=p1->next; } p->next=NULL; printf(“\n list b:\n”); for(p2=head2,I=1;p2next=b+I; printf(“%8s%8s\n”,p2->num,p2->name); p2=pa->next; } p->next=NULL; printf(“\n”); p1=head1; while(p1!=NULL) {p2=head2; while(p2!=NULL&&strcmp(p1->num,p2->num)!=0) p2=p2->next; if(strcmp(p1->num,p2->num==0)) if(p1==head1) head1=p1->next; else p->next=p1->next; p=p1; p1=p1->next; } p1=hedad1; printf{“\n result:\n”
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库C语言课后习题答案-第四版- 谭浩强(11)在线全文阅读。
相关推荐: