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
{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(i
printf(\ if(i
printf(\
for (p2=head2,i=1;i<=LB;i++) {if (i
printf(\ if (i
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
#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 FILE *fp; char str[100]; int i=0; if ((fp=fopen(\ { printf(\ exit(0);