好文档 - 专业文书写作范文服务资料分享网站

《C语言程序设计》课后习题答案(第四版)谭浩强23019

天下 分享 时间: 加入收藏 我要投稿 点赞

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 (pb1->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!=NULL) && (pb1->num>pa1->num) && (pa1->next==NULL)) pa1->next=pb1; return(ah); }

void print(struct student *head) //输出函数 {struct student *p;

printf(\ \\n\ p=head;

if (p !=NULL) do

{printf(\ p=p->next;

}while (p !=NULL); } 9-11

#include #include #define LA 4 #define LB 5 struct student

{int num;

char name[8];

struct student *next; } a[LA],b[LB];

int main()

{struct student a[LA]={{101,\

struct student b[LB]={{103,\ int i;

struct student *p,*p1,*p2,*head1,*head2;

head1=a; head2=b;

printf(\ \\n\

for (p1=head1,i=1;i<=LA;i++) {if(inext=a+i; else p1->next=NULL;

printf(\ if(inext; }

printf(\

for (p2=head2,i=1;i<=LB;i++) {if (inext=b+i; else p2->next=NULL;

printf(\ if (inext; }

p1=head1;

while(p1!=NULL) {p2=head2;

while ((p1->num != p2->num) && (p2->next!=NULL)) p2=p2->next; if (p1->num == p2->num) {if (p1==head1) head1=p1->next; else

{p->next=p1->next;p1=p1->next;} } else {p=p1;p1=p1->next;} }

printf(\

p1=head1;

while(p1!=NULL)

{printf(\ \\n\ p1=p1->next; } return 0; } 9-12

#include #include

#define LEN sizeof(struct student) struct student { char num[6]; char name[8]; char sex[2]; int age;

struct student *next; } stu[10];

int main()

{ struct student *p,*pt,*head; int i,length,iage,flag=1;

int find=0; //找到待删除元素 find=1,否则find=0 while (flag==1)

{printf(\ scanf(\ if (length<10) flag=0; }

//建立链表

for (i=0;i

{p=(struct student *) malloc(LEN); if (i==0) head=pt=p; else pt->next=p; pt=p;

printf(\

scanf(\ printf(\

scanf(\

printf(\

scanf(\ printf(\

scanf(\ }

p->next=NULL; p=head;

printf(\ name sex age\\n\ //显示 while(p!=NULL)

{printf(\ p=p->next; }

// 删除

printf(\ //输入待删年龄 scanf(\ pt=head; p=pt;

if (pt->age==iage) //链头是待删元素 {p=pt->next; head=pt=p; find=1; }

else //链头不是待删元素 pt=pt->next; while (pt!=NULL) {if (pt->age==iage) {p->next=pt->next; find=1; }

else // 中间结点不是待删元素 p=pt; pt=pt->next; } if (!find)

printf(\ %d.\

p=head;

printf(\ name sex age\\n\显示结果 while (p!=NULL)

{printf(\ printf(\ p=p->next; }

return 0; }

第10章对文件的输入输出331 10.1C文件的有关基本知识331 10.1.1什么是文件331 10.1.2文件名332 10.1.3文件的分类332 10.1.4文件缓冲区333 10.1.5文件类型指针333 10.2打开与关闭文件335

10.2.1用fopen函数打开数据文件335 10.2.2用fclose函数关闭数据文件337 10.3顺序读写数据文件338 10.3.1怎样向文件读写字符338

10.3.2怎样向文件读写一个字符串341 10.3.3用格式化的方式读写文件344

10.3.4用二进制方式向文件读写一组数据345 10.4随机读写数据文件349

10.4.1?募恢帽昙羌捌涠ㄎ?349 10.4.2随机读写 352

10.5文件读写的出错检测353

习题354第11章常见错误分析355附录370附录A在Visual C++ 6.0环境下运行C程序的方法370

附录B常用字符与ASCII代码对照表377 附录CC语言中的关键字378 附录D运算符和结合性378 附录EC语言常用语法提要380 附录FC库函数384 参考文献390 10-3

#include #include #include int main () {

FILE *fp; char str[100]; int i=0;

if ((fp=fopen(\ { printf(\ exit(0);

《C语言程序设计》课后习题答案(第四版)谭浩强23019

structstudent*insert(structstudent*ah,structstudent*bh)//插入函数{structstudent*pa1,*pa2,*pb1,*pb2;pa2=pa1=ah;pb2=pb1=bh;do{while((pb1->num>pa1->num)
推荐度:
点击下载文档文档为doc格式
87azb13lxw6x2111f20r4n7xz5ee5l00bm8
领取福利

微信扫码领取福利

微信扫码分享