资源描述
5-2 单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,上页,下页,节,末页,结束,共 24 页 第,*,页,问题及作业说明:,9.2,将输入字符串,(,有效字符,),按逆序复制到字符串,str1,。按题意,原字符串似不应改变,,使用库函数要明确其详细功能(查,baidu,或测试),且不使用标准库函数也要会。,#include,#include,int str,R,everse(char*str1,char*str2),char*p;,p=strrev(str2);/,翻转,str2,并返回首址,strcpy(str1,p);,return strlen(str1);,void main(),char str1100,str2100;,/,必须是数组,int length;,gets(str2);,length=str,R,everse(str1,str2);,puts(str,2,);puts(str,1,);,printf(“%d”,length,);,/,按此程序源串也被反转!,int str,R,everse(char*str1,char*str2),char*p=str2;,int count=0;,while(*p!=0)+p;,/,定位结束符,-p;,/,指向最末一个字符,while(p=str2),/,从后向前逐个拷贝,*str1=*p;+count;,+str1;-p;,*str1=0;,/,注意,str1,当前位置与,0,return count;,/,较调用,strlen,高效!,问题及作业说明:,作业,9.3,字符串连接,.,其一不应引入新数组,str3,,其二注意,0,的处理,其三,测试时注意字符数组的长度,int strCatch(char*str1,char*str2),int count=0;,while(*str1!=0),+,str1;,+,count;,while(*str2!=0),*str1=*str2;count+;,+str1;+str2;,*str1=0;,return count;,#include,int strCatch(char*str1,char*str2),int count=0;,while(*str1),+,str1;,+,count;,while(*str1+=*str2+),+,count;,return count;,void main(),char str1200,str2100;,/,注意长度,int length;,gets(str1);,gets(str2);,length=strCatch(str1,str2);,puts(str1);puts(str2);,printf(%d,length);,作业,9.4,字符串裁剪,/,裁剪尾部空格或制表符和换行符,int trim(char*str),int count=0;,while(*str),+,str;,+,count;,-str;,while(*str=|*str=t|*str=n)-str;,-count;,*(str+1)=0;,return count;,/,裁剪头部和尾部空格,制表符和换行符,int ntrim(char*str),char*first=str,*last;,int count=0;,while(*first=|*first=t|*first=n)+first;,last=first;,while(*last)+last;,-last;,while(*last=|*last=t|*last=n)-last;,*(last+1)=0;,while(first”,引用结构体变量的成员。,struct,student,stu,*p=&,stu,;,p-num=10001;,scanf(%s,p-name);,scanf(%f,&p,-score);,printf(age,of%s,is%d,stu.name,age,);,(4),将结构体变量作为一个整体,取地址或赋值,。如,struct,student stu1,stu2,*p=,stu2=stu1;printf(“%X,%X,p,“(*p).,成员名”、“,p-,成员名”与“,stu,.,成员名”等价,后两种更直观,成员运算符“,.”,与指向运算符“,-”,的优先级同,高于指针运算符*,不能将结构体变量当作一个整体进行输入、输出或“,赋值”,struct,date date1,date2;,date1=1988,8,5;,scanf(“%d%d%d”,printf(%d%d%d,date2);,【,思考,】“(*p).,成员名”中的圆括号能否省略?,3,结构体变量的初始化,定义结构体变量的同时依次写出全部或部分成员变量的初始值。如,:,struct,student stu1=1001,Zhang San,M,1988,8,10,580;,struct,student stu2=1002,Li Ping,F,1989,2,5,595;,struct,student stu3=1002,“Li Ping”,F,1989;/,其余默认零,【,说明,】,初始化前,结构体变量各成员的取值是随机的。,花括号内初值的顺序、类型要与结构体成员的顺序和类型一致。,【,注意,】,初始化时,花括号内的数据不能包含变量。如以下程序片段的最后一行不正确:,struct,date,int,year;int,month;int,day;date1=1988,8,10;,struct,student,int,num;,char name20;,char sex;,struct,date birthday;,float score;,stu,=10010,zhangsan,M,date1,580;,10.1.3,结构体程序举例,例,10.1,输入一个学生的信息并显示。,#include,void main(),struct,dateint,year;int,month;int,day;,struct,studentint,num;char,name20;,char sex;,struct,date,birthday;float,score;,;,struct,student,stu,;,scanf(%d,&stu.num,);,scanf(%s,stu.name,);,scanf,(“%,c”,/%c,前有一空格,若上一句是,gets(stu.name,),则不必!,scanf(%d%d%d,&stu.birthday.year,&,stu.birthday.month,&,stu.birthday.day,);,scanf(%f,&stu.score,);,printf,(,学号,%,dn,姓名,%,sn,性别,%,cn,生日,%d,年,%d,月,%d,日,n,成绩,%.1fn,stu.num,stu.name,stu.sex,stu.birthday.year,stu.birthday.month,stu.birthday.day,stu.score,);,例,10.2,函数,input,中输入一学生的信息,函数,list,中显示,#include,struct,date,int,year;,int,month;,int,day;,struct,student,int,num;char,name20;char sex;,struct,date birthday;float score;,;,struct,student,input(),struct,student,stu,;,printf,(“,请输入学生学号,:”);/,后文提示略,scanf(%d,&stu.num,);,scanf(%s,stu.name,);,scanf,(%,c,&stu.sex,);,scanf(%d%d%d,&stu.birthday.year,&stu.birthday.month,&,stu.birthday.day,);,scanf(%f,&stu.score,);,return(stu,);,void,list(,struct,student,stu,),printf,(,学号,:%,dn,姓名,:%,sn,性别,:%,cn,出生日期,:%d,年,%d,月,%d,日,n,成绩,:%6.1fn,stu.num,stu.name,stu.sex,stu.birthday.year,stu.birthday.month,stu.birthday.day,stu.score,);,void main(),struct,student,stu,;,stu,=input();,list(stu,);,3,个函数中,stu,对应内存空间是否相同?,将结构体类型定义放到,main,函数如何,?,例,10.3,用指针作参数输出结构体数组内的学生信息,#include,struct,student,int,num;,char name20;,float score;,;,void,print(struct,student*p),int,i;,printf,(,学号 姓名 成绩,n);,for(i,=0;i,num,p,-,name,p,-score);,void main(),struct,student stu3=101,li,583,102,wu,590,103,han,560;,void,print(struct,student*);,print(stu,);,教材图,10.4,中算法与程序不符,区分,p-,num+;(p,+)-,num;(+p,)-num,例,10.4,多人通过输入候选人姓名投票,统计各候选人票数,#include,#include,#define N 3,#define M 11,void main(),struct,person,char name20;,int,count;,leaderN,=zhangsan,0,lisi,0,wangwu,0;,int i,j;,char name20;,for(i=0;iM;i+),printf(,请输入候选人姓名:,n);,gets(name);,for(j=0;jN;j+),if(strcmp(leaderj.name,name)=0)leaderj.count+;,for(i,=0;i,N;i,+),printf(%8s:%dn,leaderi.name,leaderi.count);,结构体数组初始化的方式?,例,10.5,输入学生学号姓名及成绩,求平均分与最高分学生信息,struct,student,int,num;,char name20;,float score;,;,void,input(struct,student,stu,);,float,aver(struct,student,stu,),int,search(struct,student,stu,),void,list(struct,student student1),void main(),struct,student,stuN,;,float average;,int,max;,input(stu,);,average=,aver(stu,);,max=,search(stu,);,printf,(,平均成绩为,%6.2fn,average);,printf,(,最高分学生为,:n);,list(stumax,);,作业,:,10.1,10.2,10.3,10.4,回顾:,结构体变量,+,结构体类型,+,结构体成员引用,+,结构体作函数参数,+,结构体指针,+,结构体数组,P276input,函数中输入姓名,scanf(“%s”,&stusi.name,)?,#include,struct,Student,int,num;,char name20;,float score;,;,/,分号!,void,print(struct,Student*p),int,i;,printf,(,学号 姓名 成绩,n);,for(i,=0;i,num,p,-,name,p,-score,);,/,-,是,(*).,的缩写,如,(*,p).num,(*,p).name,void main(),struct,student stus3,=101,li,583,102,wu,590,103,han,560;,print(stus,);,10.2,链表,引言,:数组元素存储在一片连续的内存单元中,从而给数组带来两方面问题:其一,个数不确定时需定义一个,最大长度,;其二,向数组增加或删除一个数据时,需要,移动大量元素,。,链表,:一种动态地进行存储分配的数据结构,不需要事先确定最大长度,在插入或者删除一个元素时也不会引起大量数据的移动,要求,:,掌握链表,结构、链表的定义与链表的创建,/,插入,/,删除,/,输出,head/L,1,、链表结构,【,结构,】,“首”结点,+“,尾”结点,+,中间结点,,每个结点包括两部分:一部分是用户需要用的实际数据,称为,数据域;,另一部分是下一个结点的地址,称为,指针域,。,头指针,head,指向链表的首结点,首结点与中间结点的指针域指向后继结点,表尾结点的指针域存放空地址(常表示为,NULL,,是值为,0,的符号常量,编程时需先定义)。,【,说明,】,(,1,)链表中各元素在内存中的存储单元通常不连续,(,2,)查找链表结点必须从头指针开始顺序访问至找到或到表尾,(NULL,不可缺!,),,,不如数组快!,(,3,)头指针至关重要,可标识或者代表整个单链表,,声明链表实际就是声明一个基类型为结点类型的头指针,head/L,2,、链表的定义,A,E,B,C,D,L,struct,student,int,num;,char name20;,float score;,struct,student*next;,;,struct,student,stuA,;,struct,student*head,*L,*p;,stuA.num,=101;,stuA.next,=NULL;,head=&,stuA,;/L=&,stu,;,p=head;,printf(“%d”,p,-num);,struct,LNode,/,表结点,char data;/,数据域,struct,LNode,*next;/,指针域,;,Strcut,LNode,node1,node2;/,定义两表结点,struct,LNode,*,La,*Lb;/,定义两链表,/,或,typedef,struct,LNode,LNode,/,typedef,struct,LNode,*,LinkList,;,/,LNode,node1,node2;,LinkList,La,Lb,;,链表的分类,分类,:创建链表结点有两种方式:其一,使用结构体变量或数组元素充当结点;其二,在程序执行过程中,动态开辟结点,。第一种方式创建的链表称为,静态链表,,第二种方式创建的链表称为,动态链表,。,特点,:静态链表各结点所占用的存储空间在程序执行完毕后由系统释放;动态链表可在程序执行过程中调用动态存储分配函数释放;后者更灵活,适用范围,:链表长度固定且结点个数较少时通常使用“静态链表”,其它情况用动态链表,动态链表操作,:,创建,+,输出,+,删除,+,插入,静态链表示例,eg10.6,含三个学生的静态链表,#include,struct,student,int,num;,char name20;,float score;,struct,student*next;,;,void main(),struct,student a=10011,zhangsan,592,b=10012,lisi,581,c=10013,wangwu,656;,struct,student*L,*p;/,教材中,L,一律用,head,L=,a.next,=,c.next,=,b.next,=NULL;,for(p,=,L;p,!=,NULL;p,=p-next),printf(%5d%8s%6.1fn,p-,num,p,-,name,p,-score);,用数组构造静态链表也可:,struct,student,stus,=.,.,.;,struct,student*L=,stus,;,stus0.next=,stus2.next=,如何”顺序“输出?,动态链表示例,动态创建三个结点构造链表,#include,struct,student,int,num;,char name20;,float score;,struct,student*next;,;,void main(),struct,student*L,*p;,p=(,struct,student*),malloc(sizeof(struct,student);,scanf(“%d%s%f”,&p,-num,p-,name,p,-score);,p-next=NULL;,L=p;,p=(,struct,student*),malloc(sizeof(struct,student);,scanf(“%d%s%f”,&p,-num,p-,name,p,-score);,p-next=NULL;,L-next=p;,for(p,=,L;p,!=,NULL;p,=p-next),printf(%5d%8s%6.1fn,p-,num,p,-,name,p,-score);,如何创建,n,个结点的动态链表?,3,、链表基本操作,动态创建,思路:共循环,n,次,每次循环开辟一个结点,并令,p1,指向该新结点,把,p1,所指向的结点连接到当前表尾结点(,p2,)的后面。,若首次开辟则让表名或头指针指向首结点,此外注意新结点指针域的赋值,eg10.7,设计函数创建含,n,个顶点的链表,#include,#include,#define LEN,sizeof(struct,student);,struct,student*,create(int,n),struct,student*head=NULL,*p1,*p2;,int i;,for(i=1;inext=NULL;/,/,注意!,if(i,=1),head=p1;,p2=p1;,else,p2-next=p1;,p2=p1;,return(head,);,3,、链表基本操作,-,输出,#include,void,print(struct,student*head),struct,student*p=head;,while(p,!=NULL)/,只要,p,不空,用,for,更不容易出错,printf,(,学号,:%d,成绩,:%3fn,p-num,p-score);,p=p-next;,/p+,可否?,3,、链表的基本操作,-,删除,(,指定学号的学生结点,),思路,:,定位需删除结点及其前驱,修改前驱结点指针进行删除,定位,:,设指针,prePtr,和,curPtr,只要,curPtr,还指向一个结点且指向的结点的学号不符合要求则两个指针均后移,。循环结束后若,curPtr,为,NULL,说明未找到,否则说明找到。,struct,student*,ListDelete(struct,student*,head,int,num),/,删除指定学号的学生结点,,返回删除该结点后的链表的头指针,struct,student*,prePtr,=NULL,*,curPtr,=head;,while(curPtr,!=,NULL&curPtr,-num!=num),/,常用技巧,注意短路运算,prePtr,=,curPtr,;,/,注意改错,不能写成,prePtr,=,prePtr,-next,curPtr,=,curPtr,-next;,if(curPtr,=NULL),printf,(,学号为,%d,的学生不存在,n,num,);,else,if(curPtr,=head),head=,curPtr,-next;,free(curPtr,);,/,注意及时释放,else,prePtr,-next=,curPtr,-next;,free(curPtr,);,/,删除尾结点也可,printf,(,学号为,%d,的学生已被删除,n,num,);,return(head,);,/,教材,:,只要,curP,非所寻且未到尾结点则继续,循环结束后看,有没有找到,3,、链表基本操作,删除,2-,教材,只要,curP,非所寻且未到尾结点则继续,循环结束后看,有没有找到,若中间找到则,curP,-num=,num,;,若中间没找到则不等,例,10.9,写函数删除指定学号的学生,头指针和学号作参数,struct,student*,del(struct,student*,head,int,num),/,删除指定学号的学生结点,,返回删除该结点后的链表的头指针,struct,student*p1,*p2;,if(head,=NULL),printf,(,原表为空!,n);,return(NULL,);,else,p1=head;,while(p1-num!=num&p1-next!=NULL)/,常用技巧!,p2=p1;,p1=,p1,-next;,if(p1-num=,num,),if(p1=head),head=p1-next;,else,p2-next=p1-next;,printf,(,学号为,%d,的学生已被删除,n,num,);,else,printf,(,学号为,%d,的学生不存在,n,num,);,return(head,);,注意对比,教材程序需单独处理空表,.,函数返回类型能否空,?,3,、链表的基本操作,-,插入,(,指定学号的学生结点),思路,:,定位第一个大于待插入学生的结点,修改其前驱结点指针,定位,:,设指针,prePtr,和,curPtr,只要,curPtr,还指向一个结点且所指结点学号不比,num,大则两指针都后移,。循环结束后通常插入到,prePtr,后即可,但,prePtr,为,NULL,除外,说明插入到表头,注意:,分析有等值结点时的插入情况!,教材,:,curP,逐个遍历并比较到尾结点停,循环结束后看,curPtr,所指结点的学号是否比待插入结点的学号大,是则插入其前,(,此处还要看是否插入到表头,),,否则插入到表尾之后。,struct,student*,ListInsert(struct,student*,head,struct,student*q),/,插入指定学号的学生使仍然有序,,返回插入该结点后链表的头指针,struct,student*,prePtr,=NULL,*,curPtr,=head;,while(curPtr,!=,NULL&curPtr,-num num),/,常用技巧,注意短路,prePtr,=,curPtr,;,/,注意改错,不能写成,prePtr,=,prePtr,-next,curPtr,=,curPtr,-next;,if(prePtr,=NULL),q-next=head;head=q;,else,prePtr,-next=q;q-next=,curPtr,;,/,无论是否插入到表尾均可,return(head,);,/,按此算法即使原表为空也不必特殊处理,,上例也如此!教材算法不可,3,、链表基本操作,在特定位置插入结点,只要,curP,非所寻且未到尾结点则继续,循环结束后看,有没有找到,:,若中间找到则,curP,-num,p0-num;,若中间没找到则此式不成立,例,10,链表结点按学号小到大排列,插入新结点使仍有序,struct,student*,insert(struct,student*head,struct,student*stud),struct,student*p0,*p1,*p2;,p0=stud;p1=head;,if(head,=NULL)/*,原链表为空则将新结点作为头结点处理*,/,head=p0;p0-next=NULL;,else,while(p0-nump1-num)&(p1-next!=NULL),p2=p1;,p1=,p1,-next;,if(p0-numnum),if(head,=p1)head=p0;,elsep2-next=p0;,p0-next=p1;,else,p1-next=p0;p0-next=NULL;,return(head,);,试简化该算法,如尾结点统一化处理,只用一个指针变量,引入头结点,在指定位置插入,回顾,基本操作的综合测试,void main(),struct student*head,*,q,;,int n,k;,printf(,请输入学生个数,:n);,scanf(%d,printf(,请按学号输入各学生的信息,:n);,head=create(n);,/,注意返回值类型,printf(,原链表为,:n);,print(head);,printf(,输入要删除学生的学号,:n);,scanf(%d,head=ListDelete(head,k);,/,注意返回值类型,printf(,删除后链表为,:n);,print(head);,q,=(struct student*)malloc(sizeof(struct student);,printf(,输入欲插入学生的学号及成绩,n);,scanf(%d%f,&,q,-num,&,q,-score);,head=ListInsert(head,q),;,/,注意返回值类型,printf(,插入后链表为,:n);,print(head);,实验报告:,实验,11,指针的应用,实验目的:,掌握用指针进行字符串操作的方法,实验内容:,9.4,三个,Trim,函数,要求:写出代码,给出调试说明和总结,实验报告:,实验,12,结构体,实验目的:,1.,掌握结构体类型与结构体变量的定义、引用和初始化方法,2.,掌握结构体数组的相关操作,实验内容:,作业本上,3 4,题写入实验报告,要求写出代码并给出调试说明和总结,实验报告:,实验,13,链表,实验目的:,1.,理解链表的概念和定义,2,、掌握链表的基本操作,实验内容:,5 6 8,要求写出代码并给出调试说明和总结,为验证正确性需事先将教材中创建链表、输出链表的函数调试好,实验,14,文件的使用,实验,15,综合性实验,10.3,共用体,例,10.12,某门课程,部分学生选修,部分学生必修。对选修学生按等级制打分,分,ABCDE,五级,对必修课学生按百分制打分。定义图示的结构体数组,之后输入输出,struct,Stdent,int,num;,char name20;,char optional;,float,scoreMark,;,char,scoreGrade,;,;,/,若,optional,为,T,/,缺点:空间浪费,方案一,:,对于,score,设置,scoreMark,和,scoreGrade,两个成员,方案二,:,两者占用同一段内存,通过,score.mark,或,score.grade,访问,union Score,float mark;,char grade;,;,struct,Stdent,int,num;,char name20;,char optional;,union Score,score,;,;,struct,student,stuN,;,for(i,=0;i,N;i,+),scanf(%c,&stui.optional,);,if(stui.optional,=T),scanf(%c,&stui.score.grade,);,else,scanf(%f,例,10.2,代码,#include,#define N 3,void main(),struct,student,stuN,;,int,i;,printf,(,请输入,%d,个学生的信息,:,n,N,);,printf,(,学号 姓名 是否选修 成绩,n);,for(i,=0;i,N;i,+),scanf(%d%s,%,c,&stui.num,stui.name,&stui.optional,);,if(stui.optional,=T),scanf,(%,c,&stui.score.grade,);,else,scanf(%f,for(i=0;iN;i+),printf(%d,号,%s,stui.num,stui.name);,if(stui.optional=T),printf(,选修,成绩,%cn,stui.score.grade);,else,printf(,必修,成绩,%5.1fn,stui.score.mark);,union Score,float mark;,char grade;,;,struct,Stdent,int,num;,char name20;,char optional;,union Score,score,;,;,共用体基本概念:,共用体,:使多个类型相同或不同的成员变量占用同一段内存的结构。它与结构体类似,都属于构造数据类型,都由若干类型可以互不相同的成员组成。不同的是,结构体变量的各个成员拥有自己独立的存储单元,而共用体变量的各个成员“共用”一段内存,该内存段允许各成员在不同的,地方,分别起作用。更有效利用内存,共用体变量所占存储空间不是全部成员变量空间之和,而是各成员空间中的最大值,。,共用体各成员变量的存储空间相互覆盖,一个成员值的改变会影响其他成员(,但实际不会出问题,因同一结构体变量中只有一个有意义。,union Score,score,;/,内存内容随机,score.mark,=0;,printf(“%c”,score.grade,);,共用体语法:,共用体类型定义,union,UNode,char c;,short i;,float f;,node1,node2;,union,UNode,char c;,short i;,float f;,;,union,UNode,node1;,struct,Student,int,num;,char name20;,char optional;,union,float mark;,char grade;,score;,;,共用体变量的使用,union UNode data1,data2;,data1.c=A;,scanf(%f,data2.f=data1.c+2;,共用体变量不能整体初始化,如,union UNode data=A,5,2.3;,union,char c2;,short i;,data;,data.i=0 x4241;,printf(“%c%cn”,data.c0,data.c1);/,常为,AB,10.4,枚举类型,引,:表示性别的变量,sex,最好能且只能用,Male,和,Famale,赋值,赋予其它值时都应报错;表示工作日的变量,workday,最好能且只能用,Mon,Tue,Web,Thur,Fri,中之一进行赋值。如此能使程序清晰且不易出错,上述变量应属于一种特殊的类型,其取值被限定为几个特定的量。这种类型是一种自定义类型,称为,枚举类型,enum,Sex,Male,Female,;,enum,Sex,mySex,=Male;,enum,Workday,mon,tue,wed,thr,fri,;,enum,Workday,curDay,preDay,;,curDay,=Mon;,说明,:类似,Male/Female,等常称为枚举常量,他们本质上是一些,取值为整数的标识符,默认值从,0,开始依次递增。也可显式指定其值,如,enmu,SexMale,=1,Female=-1;,enum,Sex,mysex,=Male;,printf(“%d”,mysex,),enum,WorkdayMon,=1,Tue,Wed,Thur,FricurDay=Mon;,枚举相关语法:,1.,枚举变量的赋值,用枚举常量赋值,如,enum,Workday d=,mon,。,把一个整数进行强制类型转换后再赋给枚举变量,如,enum,Workday,curDay,=(,enum,Workday)1;,枚举常量是一个标识符,可在定义枚举类型时为其赋值,但不能在程序中为其赋值。,枚举常量既非字符常量,也非字符串常量,使用时不可加单引号或双引号,也不可用,%s,输出一个枚举量常,/,变量,2.,枚举变量的输出:常用,switch,语句块或,if-else,if-else,块,补充:,枚举变量的输入,int,n;scanf(“%d”,&n,);,d=(,enum,Workday)n,;,enum,WorkdayMon,Tue,Wed,Thur,Fri,;,enum,Workday d=,mon,;,switch(d,),case mon:printf(%-6s,mon);break;,case tue:printf(%-6s,tue);break;,case wed:printf(%-6s,wed);break;,case thr:printf(%-6s,thr);break;,case fri:printf(%-6s,fri);break;,default:printf(%-6s,error!);break;,例,10.13,假期中周一到周五三人轮流值班,一次一天,输入,n,求当天是周几,且当天谁值班,思路:编程模拟值班的过程,1-n,,到第,n,天直接输出即可,void main(),enum WeekdayMon=1,Tue,Wed,Thur,Fri,Sat,Sun;,enum WorkerZhangSan,LiSi,WangWu;,enum Weekday curDay=Tue;,enum Worker onDuty=ZhangSan;,int i,n;scanf(%d,for(i=2;i=n;i+),if(curDay=Sat)curDay=,Sun,;,else if(curDay=Sun),curDay=Mon;,else,curDay=(enum Weekday)(curDay+1);,onDuty=(enum Worker)(onDuty+1)%3);,switch(curDay),/*,输出今天是周几*,/,case Mon:printf(%-6sn,Mon,day,);break;,/,case Sun:printf(%-6sn,Sun,day,);break;,default:break;,if(curDaynext),sum=0;,for(i,=0;i,scorei,;,aver=sum/N;,if(aver-85-1e-6),/,注意浮点数相等的比较办法,printf(,学号,:%ld,姓名,:%s,p-num,p-name);,for(i=0;iscorei);,printf(n,);,例,10.15 N,个学生围成一圈,数到,M,就出圈,最后剩谁,?,思路:用循环链表,(,尾结点的指针指向首元结点,),模拟该圈,之后通过循环模拟出圈的整个过程,改进:避免重复报数,order1;len-),p=head;,for(order=1;ordernext;,p-next=p-next-next;/,未释放!,head=p-next;/,从下一同学重新报数,10.16,学生链表,按姓名排序,(,冒泡法,注意比较教材,),head=,create(N,);,for(i=0;iN-1;i+),p=head;/p,始终指向两两比较时的前者,,prep,指向,p,的前驱,for(j=0;j,name,p,-next-name)0),if(p,=head),head=p-next;,p-next=p-next-next;head-next=p;,prep=head;/p,已指向第二结点,教材先,p=head;,后,p=p-next,e
展开阅读全文