第九章:结构体
- 格式:doc
- 大小:167.50 KB
- 文档页数:25
一、概念题二、判断题1. 结构体中的成员不可以单独使用(F)。
2. 成员名可以与程序中的变量名相同,二者不代表同一对象(T)。
3. 不能将一个结构体变量作为一个整体进行输入输出(T)。
4. 结构体变量所占内存长度是各成员占的内存长度之和(T)。
5. 结构体中的成员不可以单独使用(F )。
9. 一个结构体变量的指针就是该变量所占内存段的起始地址(T)。
10. 用结构体变量作实参,形参也必须是同类型的结构体变量(T )。
三、单选题1. 设变量定义如下,则对其中的结构分量num正确的引用是( )。
struct student {int num ;char name[20];float score;} stud[10];A. stud[1].num=10;B. student.stud.num=10;C. struct.stud.num=10;D. struct student.num=10;2. 已知职工记录描述如下,设变量w中的“生日”是“1993年10月25日”,下列对“生日”的正确赋值方式是()。
struct worker{int no;char name[20];char sex;struct birth{ int day; int month; int year;}a;};struct worker w;A day=25;month=10;year=1993;B w.birth.day=25; w.birth.month=10; w.birth.year=1993;C w.day=25; w.month=10; w.year=1993;D w.a.day=25; w.a.month=10; w.a.year=1993;3. 对于以下的变量定义,语句( )在语法和语义上都是正确的。
struct node {float x,y;char s[10];} point={1,2,”abc”},*p;A. *p=point;B. p=&point;C. point=p;D. p->x=point.y;4. 设有以下语句typedef struct S{ int g;char h;} T;则下面叙述中正确的是()。
什么是结构体?结构体(Struct)是一种在编程中常用的自定义数据类型,用于将不同类型的数据组合在一起。
结构体允许我们创建一个包含多个字段的复合数据类型,每个字段可以是不同的数据类型。
结构体的主要特点如下:1. 自定义数据类型:结构体允许我们创建自定义的数据类型,以便更好地表示和组织数据。
通过结构体,我们可以将多个相关的数据字段组合在一起,形成一个逻辑上的整体。
2. 字段:结构体由多个字段组成,每个字段可以是任何合法的数据类型,包括基本类型(如整数、浮点数、字符等)和其他自定义类型(如结构体、数组等)。
3. 访问字段:可以通过结构体的实例(也称为对象或变量)来访问和操作字段。
通过使用点运算符(.)和字段名,我们可以访问和修改结构体中的各个字段。
4. 内存布局:结构体的字段在内存中是按照声明的顺序依次存储的。
这意味着,结构体的字段在内存中是连续存储的,可以通过偏移量来访问每个字段的值。
5. 初始化:创建结构体实例时,可以通过提供字段的值来初始化结构体的字段。
初始化可以在创建结构体实例的同时进行,也可以在创建后逐个字段进行。
创建结构体的语法因编程语言而异,以下是一些常见的示例:在C语言中,创建一个表示学生的结构体的示例:```cstruct Student {int id;char name[50];int age;};```在C++语言中,创建一个表示点的结构体的示例:```c++struct Point {int x;int y;};```在Java语言中,使用关键字`class`来定义一个表示人的结构体的示例:```javaclass Person {int age;String name;}```在Python语言中,使用`class`关键字来定义一个表示汽车的结构体的示例:```pythonclass Car:def __init__(self, make, model, year):self.make = makeself.model = modelself.year = year```通过结构体,我们可以创建结构体的实例,并访问和操作其字段。
结构体的定义规则一、什么是结构体结构体(Structure)是一种用户自定义的数据类型,它是由一组不同类型的成员变量组成的。
通过结构体,我们可以将不同类型的数据组合在一起,形成一个整体的数据结构。
结构体可以包含多个不同类型的成员变量,每个成员变量都可以有自己的数据类型和名称,类似于一个小型的数据表或者实体。
在C语言中,结构体是一种很重要的数据类型,它可以用于表示复杂的数据结构,提高程序的灵活性和可读性。
二、结构体的定义语法结构体的定义语法如下:struct 结构体名 {成员变量1的数据类型成员变量1的名称;成员变量2的数据类型成员变量2的名称;...};三、结构体成员的访问与初始化结构体定义之后,我们可以通过结构体名加点操作符来访问结构体的成员变量。
例如,我们定义了一个名为Person的结构体,其中包含了name和age两个成员变量,我们可以使用以下方式来访问和修改结构体的成员变量:struct Person {char name[20];int age;};int main() {struct Person p;strcpy(, "Tom");p.age = 20;printf("Name: %s, Age: %d\n", , p.age);return 0;}上述代码中,我们首先定义了一个Person结构体类型的变量p,然后使用strcpy函数将字符串”Tom”复制给了,再将整数20赋值给了p.age。
最后,使用printf函数输出了结构体的成员变量。
另外,我们还可以使用结构体初始化器来为结构体的成员变量赋初值。
例如,我们可以使用以下方法来初始化上述的Person结构体:struct Person p = {"Tom", 20};四、结构体的嵌套与指针结构体可以嵌套定义,即一个结构体中的成员变量也可以是另一个结构体类型的变量。
通过结构体的嵌套,我们可以构建更复杂的数据结构。
第九章 结构体与共用体一、 教案头: 教学内容第九章 结构体与共用体2 学时认知目标(应知)技能目标(应会): 了解结构体的意义和基本概念 : 能够正确定义和引用结构体变量 : 能够采用指针引用结构体变量 : 能够通过指针引用结构体数组 : 使用结构体变量作函数参数 : 使用结构体指针作函数参数 : 掌握共用体的结构和特点 : 掌握枚举的结构和特点 :typedef 定义类型的意义 : 引用结构体变量成员,引用结构体数组元素的成员 : 结构体指针变量与其所指变量的关系 : 结构体指针变量作函数参数 : 通过指针引用结构体数组 : 通过指针操作字符串 : 掌握共用体变量的定义和引用 : 了解在程序中枚举的应用 : 读程序能够理解typedef 定义的类型 情感目标(理解)教学目标(重点难点)教学目标 : 培养学生自主探究学习、求真务实的品德 : 培养学生细致钻研的学风 : 培养学生努力拓展思维;理论与实际相结合的思维习惯 : 指向结构体的指针 : 结构体数组成员的引用 : 结构体变量作函数参数 : 共用体的存储特性 : 链表的结构特点和操作处理 内容概述 Ø 教材章节: 9.1 结构体类型和结构体变量 9.2 结构体数组 9.3 结构体指针 9.4 用指针处理链表 9.5 共用体类型 9.6 枚举类型 9.7 用typedef命名类型 Ø 教授内容: l 本章讲解了什么是结构体类型,以及如何使用结构类型变量、结构体数组、结构体指针,什么是共用体,它和结构体的异同,以及共用体变量的引用 l 本章还讲解了枚举类型、以及如何使用typedef命名类型 能力训练及任务案例 任务9-1: 结构体变量初始化 任务9-2:结构体数组举例 任务9-3: 指向结构体变量的指针变量示例 任务9-4: 指向结构体数组元素的指针的应用 任务9-5: 结构体变量作函数的参数示例 任务9-6: 指向结构体的指针作函数参数示例任务9-7: 建立静态链表示例 任务9-8:建立动态链表示例 任务9-9: 单链表的输出 任务9-10:共用体示例任务9-11:枚举类型示例 任务9-12:学生信息表(单链表)的基本操作参考资料C 程序设计教程,谭浩强 著,清华大学出版社,2007.07C 程序设计教程学习辅导,谭浩强 编著,清华大学出版社,2007.10 网络学堂:考试系统:无忧网络考试系统 http://10.1.89.100/webexam二、 教学设计 1.专业英语词汇 英文词汇中文名structure 结构member 成员tag 标记function 函数Enumerate 枚举 Define 定义 Union 联合(共用体) create 创建Insert 插入delete 删除Modify / update 修改 2.教学方法 引入结构体概念后采用项目拓展、实例分析引导,递进驱动。
《C语言程序设计》课后习题答案高等教育出版社《C语言程序设计》课后习题答案高等教育出版社第一章:C语言概述1. C语言的特点C语言是一种以处理底层任务和系统编程为目标的高级编程语言。
其特点包括语法简洁、执行效率高、可移植性强等。
第二章:C语言基本数据类型1. C语言中的基本数据类型C语言中的基本数据类型包括整型、字符型、浮点型等。
整型可以进一步细分为有符号整型和无符号整型。
第三章:C语言运算符1. C语言中的运算符C语言中常见的运算符包括算术运算符、赋值运算符、关系运算符、逻辑运算符等。
这些运算符用于执行各种数学和逻辑操作。
第四章:C语言控制语句1. C语言中的条件语句C语言提供了if语句和switch语句来实现条件判断。
if语句用于执行基于布尔表达式的条件分支,而switch语句用于根据不同的值执行不同的代码块。
第五章:C语言函数1. C语言中的函数定义和调用函数是C语言中的基本模块,用于封装可重用的代码。
函数定义包括函数返回类型、函数名、参数列表和函数体等部分。
第六章:C语言数组1. C语言中的数组定义和使用数组是一组相同类型的数据元素的集合。
C语言中可以使用数组来存储和操作大量数据。
第七章:C语言指针1. C语言中的指针概念指针是一种变量,它存储了内存地址。
通过指针,可以直接访问和修改对应内存地址中的数据。
第八章:C语言字符串1. C语言中的字符串操作字符串是由一系列字符组成的数据类型。
C语言通过字符数组来表示和操作字符串。
第九章:C语言结构体1. C语言中的结构体定义和使用结构体是一种自定义的复合数据类型,它可以包含多个不同类型的成员变量。
第十章:C语言文件操作1. C语言中的文件读写操作文件操作是一种重要的数据输入和输出方式。
C语言提供了一系列函数来实现文件的读写操作。
总结:通过解答以上习题,我们可以更好地掌握C语言的各个方面,提升我们的编程能力和解决问题的能力。
希望本文对读者有所帮助。
1、定义两个日期结构体变量(包括年、月、日)。
输入两个日期值,比较两个日期的大小(越靠后越大),输出较大的日期。
2、将1题中输入的日期进行验证,保证日期的正确性。
(1)不可为负数;(2)年份1900-2056之间,月份在1-12之间;(3)日期在1-31日以内(注意各月份的日期数判断及闰年的判断)(Year % 4 ==0 && Year % 100 !=0 || Year % 400 ==0)3、输入两个日期,编程交换两个日期值,保证第一个日期是较大的日期(日期的大小比较用函数表达)。
4、计算某日期是一年中的第几天。
5、计算两日期间相差的天数。
6、利用基姆拉尔森计算公式:W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7,计算任意日期是星期几。
说明:1)在公式中d表示日期中的日数,m表示月份数,y表示年数。
2)注意:在公式中有个与其他公式不同的地方:把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10则换算成:2003-13-10来代入公式计算。
7、打印出任意月份的日历表。
8、输入5位同学的一组信息(结构数组),包括学号、姓名、数学成绩、计算机成绩,求得每位同学的平均分和总分,然后按照总分从高到低排序。
9、有一批图书(利用结构数组),每本书有:书名(name),作者(author) , 编号(num),出版日期(date)四个数据,希望输入后按书名的字母顺序将各书的记录排列好,供以后查询。
今输入一本书的书名,如果查询到库中有此书,打印出此书的书名,作者,编号和出版日期。
如果查不到此书,则打印出“无此书”。
10、利用指针重做9题:分别设计函数(1)结构数组的数据输入;(2)结构数组的输出;(3)数组排序;(4)查询;关于链表:11、参考第8题,编写函数,建立动态链表,输入几位同学的信息(学号、姓名、数学、计算机、平均分、总分),然后在屏幕上输出这些数据。
12、将11题中,各同学的总分与平均分计算出来,并输出所有数据;13、建立函数,为上题中的链表添加一个新的节点。
14、建立按姓名查找某同学记录的函数,找到返回该节点的地址,未找到返回NULL;15、在某个同学纪录之前插入一个新的节点。
(某同学记录之后插入一个新的节点)16、删除找到的某个同学记录的节点。
17、单向链表逆置;.18、对单链表按照数学成绩从高到低排序;(只交换除指针成员next之外的其他成员变量的值)19、对两个链表进行连接;两个有序链表合并(保持有序);20、利用指针,交换链表中任意两个节点;(p,q为需要交换的两个节点,首先保证p<=q,并作为传递的参数,函数中分为几种情形:21、利用20题函数,重写链表的排序;(以下例题开始使用带头节点的链表,可以减小编程的难度)22、利用链表完成习题:有17个人围成一圈(编号为0~16),从第 0号的人开始从 1报数,凡报到 3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止。
问此人原来的位置是多少号?23、利用插入法,每输入一个节点数据就按顺序插入到链表的某个位置,输入完成即链表排序完成,输出链表的各节点数据。
24、编写程序,建立环形链表及双向链表(带头节点),并正向、反向输出双向链表中所有节点数据;25、编写双向链表的节点插入、删除、交换、排序函数26、编写一个完整、简易的成绩管理系统:说明:1)结构体定义:Student——学号num,姓名name,班级classes,语文chinese,数学math,英语english,总分sum;2)系统功能:a.成绩录入; b.添加数据(在链表的末尾添加);c.删除某生成绩数据;d.成绩修改;e.成绩排序(第一关键字班级,第二关键字总分);f.成绩输出(指定班级输出或全部输出,要输出一项人数信息);27、参考答案第2题:main(){struct Date{int year;int month;int day;}d1;printf("input date 1(year-month-day):\n");scanf("%d-%d-%d",&d1.year,&d1.month,&d1.day);if(yzyear(d1.year)&&yzmonth(d1.month)&&yzday(d1.year,d1.month,d1.day)) printf("yes");elseprintf("no");getch();}yzyear(int year){if(year<1900||year>2056)return 0;return 1;}yzmonth(int month){if(month<1||month>12)return 0;return 1;}yzday(int year,int month,int day){switch (month){case 1:case 3:case 5:case 7:case 8:case 10:case 12:if(day>=1&&day<=31)return 1;break;case 4:case 6:case 9:case 11:if(day>=1&&day<=30)return 1;break;case 2:if((year%4==0&&year%100!=0||year%400==0)&&day>=1&&day<=29) return 1;else if(!(year%4==0&&year%100!=0||year%400==0)&&day>=1&&day<=28) return 1;break;}return 0;}第3题:struct Date{int year,month,day;};int main(void){struct Date d1,d2,tmp;int x;printf("input d1(yyyy-mm-dd):\n");scanf("%d-%d-%d",&d1.year,&d1.month,&d1.day);printf("input d2(yyyy-mm-dd):\n");scanf("%d-%d-%d",&d2.year,&d2.month,&d2.day);x=later(d1,d2);if(x<0){tmp=d1;d1=d2;d2=tmp;}printf("%d-%d-%d",d1.year,d1.month,d1.day);printf(" %d-%d-%d",d2.year,d2.month,d2.day);getch();return 0;}later(struct Date d1,struct Date d2){if(d1.year!=d2.year)return d1.year-d2.year;else if(d1.month!=d2.month)return d1.month-d2.month;else if(d1.day!=d2.day)return d1.day-d2.day;elsereturn 0;}第4题:struct Date{int year;int month;int day;};int main(void){struct Date d1;int n;printf("input date 1(year-month-day):\n");scanf("%d-%d-%d",&d1.year,&d1.month,&d1.day);n=daysInYear(d1);printf("from %d-1-1 to %d-%d-%d,%d days",d1.year,d1.year,d1.month,d1.day,n); getch();return 0;}int daysInYear(struct Date d){int m[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};int i,sum=0;if(leapyear(d.year))m[2]=29;for(i=1;i<d.month;i++)sum+=m[i];sum+=d.day;return sum;}int leapyear(int year){if(year%4==0&&year%100!=0||year%400==0)return 1;return 0;}第5题:(利用前几题的函数)struct Date{int year;int month;int day;};int main(void){struct Date d1,d2,tmp;int n;printf("input date 1(year-month-day):\n");scanf("%d-%d-%d",&d1.year,&d1.month,&d1.day);printf("input date 2(year-month-day):\n");scanf("%d-%d-%d",&d2.year,&d2.month,&d2.day);if(later(d1,d2)<0){tmp=d1;d1=d2;d2=tmp;}n=datediff(d1,d2);printf("from %d-%d-%d to %d-%d-%d,%ddays",d2.year,d2.month,d2.day,d1.year,d1.month,d1.day,n); getch();return 0;}int datediff(struct Date d1,struct Date d2){int i,sum=0;for(i=d2.year;i<d1.year;i++){if(leapyear(i))sum+=366;elsesum+=365;}sum-=daysInYear(d2);sum+=daysInYear(d1);return sum;}第6题:(0:星期天 1:星期一…………)int weekday(struct Date d1){int y=d1.year,m=d1.month,d=d1.day,w;if(m==1||m==2){m+=12;y--;}w=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) % 7;return (w+1)%7;}第7题:calendar(struct Date d1){struct Date d2=d1;int y=d1.year,m=d1.month,d=d1.day,w,i;int mm[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; if(leapyear(y))mm[2]=29;d2.day=1;w=weekday(d2);printf("SUN\tMON\tTRU\tWEN\tTHU\tFRI\tSAT\n");for(i=0;i<w;i++)printf("\t");for(i=1;i<=mm[m];i++){if(i==d){textcolor(YELLOW);clreol();printf("%d\t",i);normvideo();clreol();}elseprintf("%d\t",i);if((i+w)%7==0)printf("\n");}}第8题:struct Student{int num;char name[20];int math;int computer;float aver;int sum;};int main(void){struct Student stu[5],tmp;int i,j;printf("input student infomation:\n");for(i=0;i<5;i++){printf("----------STUDENT %d--------\nSID:",i+1); scanf("%d",&stu[i].num);printf("NAME:");scanf("%s",stu[i].name);printf("MATH:");scanf("%d",&stu[i].math);printf("COMPUTER:");scanf("%d",&stu[i].computer);}for(i=0;i<5;i++){stu[i].sum=stu[i].math+stu[i].computer;stu[i].aver=stu[i].sum/2;}for(i=0;i<5;i++){for(j=i+1;j<5;j++){if(stu[i].sum<stu[j].sum){tmp=stu[i];stu[i]=stu[j];stu[j]=tmp;}}}for(i=0;i<5;i++){printf("----------STUDENT %d--------\nSID:",i+1); printf("%d\t",stu[i].num);printf("NAME:");printf("%s\t",stu[i].name);printf("MATH:");printf("%d\t",stu[i].math);printf("COMPUTER:");printf("%d\t",stu[i].computer);printf("AVER:");printf("%f\t",stu[i].aver);printf("SUM:");printf("%d\t\n",stu[i].sum);}getch();return 0;}第10题:#include "Stdio.h"#include "Conio.h"#define N 2struct Date{int year,month,day;};struct Book{char name[30];char author[20];int num;struct Date pubdate;};int main(void){struct Book books[10],*p;int i;p=books;inputbook(p,N);sort(p,N);outputbook(p,N);inputname(p);window(1,1,80,25);normvideo();clrscr();getch();return 0;}inputname(struct Book *p){char ch[30],*q;int i;q=ch;while(1){window(20,8+N+3,60,8+N+5);textbackground(WHITE);textcolor(BLACK);clrscr();gotoxy(2,1);cprintf("your found:(*)");gets(q);if(strcmp(q,"*")==0)break;i=found(q,p,N);gotoxy(2,2);clreol();if(i>=0){printf("%s\t%s\t%d\t%d-%d-%d",(p+i)->name,(p+i)->author,(p+i)->num,(p+i)->pubdate.year,(p+i)->pubdate.month,(p+i)->pubdate.day); }elseprintf("not found!!!");getch();}}found(char *q,struct Book *p,int n){int i;for(i=0;i<n;i++){if(strcmp(q,(p+i)->name)==0)return i;}return -1;}inputbook(struct Book *p,int n){int i;for(i=0;i<n;i++){window(20,5,50,10);textbackground(WHITE);textcolor(BLACK);clrscr();gotoxy(8,1);cprintf("--BOOK INFOMATION--\n");gotoxy(2,2);cprintf("book name:");gets(p->name);gotoxy(2,3);cprintf("author:");gets(p->author);gotoxy(2,4);cprintf("num:");scanf("%d",&(p->num));gotoxy(2,5);cprintf("pubdate(yyyy-mm-dd):");scanf("%d-%d-%d",&(p->pubdate.year),&(p->pubdate.month),&(p->pubdate.day)); getchar();p++;}}outputbook(struct Book *p,int n){int i;window(20,12,60,12+N+2);textbackground(WHITE);textcolor(BLACK);clrscr();gotoxy(6,1);printf("book infomation\n");gotoxy(2,2);printf("name\tauthor\tnum\tpubdate\n");for(i=0;i<n;i++){gotoxy(2,i+3);printf("%s\t%s\t%d\t%d-%d-%d\n",p->name,p->author,p->num,p->pubdate.year,p->pubdate.month,p->pubdate.day);p++;}}sort(struct Book *p,int n){int i,j;struct Book tmp;for(i=0;i<n;i++){for(j=i+1;j<n;j++){if(strcmp((p+i)->name,(p+j)->name)>0) {tmp=*(p+i);*(p+i)=*(p+j);*(p+j)=tmp; }}}}第11题:#include "Stdio.h"#include "Conio.h"#define LEN sizeof(struct Student)struct Student {int num;char name[20];int math,computer;float aver;int sum;struct Student *next;};int n;int main(void){struct Student *create();struct Student *head;head=create();outputstu(head);getch();return 0;}struct Student *create(){struct Student *p1,*p2,*head;char yn;p1=p2=head=NULL;n=0;while(1){n++;p1=(struct Student *)malloc(LEN);inputstu(p1);if(n==1)head=p1;elsep2->next=p1;p2=p1;printf("continue?(Y/N)");yn=getchar();if(yn=='Y'||yn=='y')continue;elsebreak;}p2->next=NULL;return head;}outputstu(struct Student *head){struct Student *p;int i=0;p=head;printf("num\tname\tmath\tcomputer\n");while(i<n){printf("%d\t%s\t%d\t%d\n",p->num,p->name,p->math,p->computer); p=p->next;i++;}}inputstu(struct Student *p){printf("num:");scanf("%d",&p->num);getchar();printf("name:");gets(p->name);printf("math:");scanf("%d",&p->math);printf("computer:");scanf("%d",&p->computer);getchar();}第12题:count(struct Student *head) {struct Student *p;p=head;while(p!=NULL){p->sum=p->math+p->computer;p->aver=(float)p->sum/2;p=p->next;}}outputall(struct Student *head){struct Student *p;p=head;printf("num\tname\tmath\tcomput\taver\tsum\n");while(p!=NULL){printf("%d\t%s\t%d\t%d\t%5.2f\t%d\n",p->num,p->name,p->math,p->computer,p->aver,p->sum);p=p->next;}}第13题:addone(struct Student *head){struct Student *p,*q;p=head;while(p->next!=NULL)p=p->next;q=(struct Student *)malloc(LEN);inputstu(q);p->next=q;q->next=NULL;count(q);}第14、15题:(14题:find函数,15题:insertone函数,另对前几题中函数稍做了些调整,下面把所有函数完整列出)#include "Stdio.h"#include "Conio.h"#define LEN sizeof(struct Student)struct Student {int num;char name[20];int math,computer;float aver;int sum;struct Student *next;};int n;int main(void){struct Student *create();struct Student *newone();struct Student *find(struct Student *head,char *ch);struct Student *insertone(struct Student *head,char *ch);struct Student *head,*f;char c[20],*ch;int xx;head=create();outputstu(head);ch=c;getchar();printf("where you insert:");gets(ch);head=insertone(head,ch);if(head!=NULL)outputstu(head);elseprintf("not found!");getch();return 0;}struct Student *insertone(struct Student *head,char *ch){ struct Student *p,*q,*r;q=newone();p=find(head,ch);if(p==NULL){printf("not found!!!\n");return head;}if(p!=head){r=head;while(r->next!=p)r=r->next;r->next=q;q->next=p;return(head);}else{head=q;q->next=p;return(head);}}printhead(int n){if(n==0)printf("num\tname\tmath\tcompute\n");elseprintf("num\tname\tmath\tcomput\taver\tsum\n"); }printbody(struct Student *p,int n){if(n==0)printf("%d\t%s\t%d\t%d\n",p->num,p->name,p->math,p->computer);elseprintf("%d\t%s\t%d\t%d\t%5.2f\t%d\n",p->num,p->name,p->math,p->computer,p->aver,p->sum);}outputone(struct Student *p){printhead(0);printbody(p,0);}struct Student *find(struct Student *head,char *ch){struct Student *p;p=head;while(p){if(strcmp(p->name,ch)==0)return(p);p=p->next;}return(NULL);}struct Student *create(){struct Student *p1,*p2,*head;char yn;p1=p2=head=NULL;n=0;while(1){n++;p1=newone();if(n==1)head=p1;elsep2->next=p1;p2=p1;printf("continue?(Y/N)");yn=getchar();if(yn=='Y'||yn=='y')continue;elsebreak;}p2->next=NULL;return head;}outputstu(struct Student *head){struct Student *p;p=head;printhead(0);while(p){printbody(p,0);p=p->next;}}inputstu(struct Student *p){printf("num:");scanf("%d",&p->num);getchar();printf("name:");gets(p->name);printf("math:");scanf("%d",&p->math);printf("computer:");scanf("%d",&p->computer);getchar();}count(struct Student *head) {struct Student *p;p=head;while(p!=NULL){p->sum=p->math+p->computer; p->aver=(float)p->sum/2;p=p->next;}}outputall(struct Student *head){struct Student *p;p=head;printhead(1);while(p!=NULL){printbody(p,1);p=p->next;}}addone(struct Student *head){struct Student *p,*q;p=head;while(p->next!=NULL)p=p->next;q=newone();p->next=q;q->next=NULL;count(q);}struct Student *newone(){struct Student *p;p=(struct Student *)malloc(LEN);inputstu(p);return(p);}第16题:struct Student *delone(struct Student *head,char *ch){ struct Student *p,*r;p=find(head,ch);if(!p){printf("not found!!!\n");return head;}if(p==head){head=p->next;free(p);}else{r=head;while(r->next!=p)r=r->next;r->next=p->next;free(p);}return head;}第17题:struct Student *reverselink(struct Student *head){struct Student *p,*q;p=head;q=NULL;while(head!=NULL){head=head->next;p->next=q;q=p;p=head;}return q;}第18题:sort(struct Student *head){struct Student tmp;int t;char ch[20];struct Student *p,*q,*r;p=head;while(p!=NULL){q=p->next;while(q!=NULL){if(p->math>q->math){t=p->num;p->num=q->num;q->num=t;strcpy(ch,p->name);strcpy(p->name,q->name);strcpy(q->name,ch); t=p->math;p->math=q->math;q->math=t;t=p->computer;p->computer=q->computer;q->computer=t;}q=q->next;}p=p->next;}}第19题(第二问):struct Student *join(struct Student *head1,struct Student *head2){struct Student *head3,*p,*q,*r;p=head1;q=head2;r=NULL;if(head1->math < head2 ->math){head3=head1;p=p->next;}else{head3=head2;q=q->next;}r=head3;while(p!=NULL&&q!=NULL){if(p->math < q->math){r->next=p;r=p;p=p->next;}else{r->next=q;r=q;q=q->next;}}if(p!=NULL)r->next=p;if(q!=NULL)r->next=q;return head3;}第20题:struct Student *exchange(struct Student *head,struct Student *p,struct Student *q){ struct Student *findprenode(struct Student *head,struct Student *p);struct Student *p_pre,*p_nex,*q_pre,*q_nex;p_pre=p_nex=q_pre=q_nex=NULL;if(p==q)return head;if(p->next!=q){if(p==head){q_pre=findprenode(head,q);q_nex=q->next;head=q;q->next=p->next;q_pre->next=p;p->next=q_nex;}else{p_pre=findprenode(head,p);p_nex=p->next;q_pre=findprenode(head,q);q_nex=q->next;p_pre->next=q;q->next=p_nex;q_pre->next=p;p->next=q_nex;}}else{if(p==head){q_nex=q->next;head=q;q->next=p;p->next=q_nex;}else{p_pre=findprenode(head,p);q_nex=q->next;p_pre->next=q;q->next=p;p->next=q_nex;}}return head;}struct Student *findprenode(struct Student *head,struct Student *p){struct Student *p_pre;p_pre=head;while(p_pre->next!=p)p_pre=p_pre->next;return p_pre;}第21题:交换结点函数,不需要返回值,将最后的return head去除;为方便交换结点函数的调用,sort1函数中,为链表临时增加一个表头,不参与排序;struct Student *newhead(){struct Student *p;p=(struct Student *)malloc(LEN);p->num=0;strcpy(p->name,"head");p->math=p->computer=0;return(p);}struct Student *sort1(struct Student *head){int t=0;struct Student *p,*q,*nhead,*r;nhead=newhead();nhead->next=head;p=head;q=p->next;while(p!=NULL){q=p->next;while(q!=NULL){if(p->math>q->math){exchange1(nhead,p,q);r=p;p=q;q=r;/*r=p;p=q;q=r->next;*/}q=q->next;}p=p->next;}return nhead->next;}第22题:(使用带头节点的链表)#include "stdio.h"#include "conio.h"#define LEN sizeof(struct Node)struct Node {int num;struct Student *next;};main(){struct Node *create();struct Node *newone(int n);struct Node *newhead();struct Node *delsome(struct Node *head); struct Node *head;head=create();output(head);printf("\n");delsome(head);output(head);getch();}struct Node *delsome(struct Node *head){ struct Node *p,*q;int i=0,del=0;p=head->next;while(del<16){i++;if(i%3==0){q=p;if(p->next==NULL)p=head->next;elsep=p->next;delone(head,q);output(head);printf("\n");del++;}else{if(p->next==NULL)p=head->next;elsep=p->next;}}return head;}delone(struct Node *head,struct Node *p){ struct Student *r;r=head;while(r->next!=p)r=r->next;r->next=p->next;free(p);return head;}struct Node *create(){struct Node *p1,*p2,*head;int n=0;head=newhead();p1=p2=NULL;while(n<17){p1=newone(n);if(n==0)head->next=p1;elsep2->next=p1;p2=p1;n++;}p2->next=NULL;return head;}struct Node *newone(int n){struct Node *p;p=(struct Node *)malloc(LEN);p->num=n;return(p);}output(struct Node *head){struct Node *p;p=head;while(p){printf("%3d",p->num);p=p->next;}}struct Node *newhead(){struct Node *p;p=(struct Node *)malloc(LEN);p->num=-1;return(p);第23题:#define LEN sizeof(struct Node) struct Node{int num;struct Node *next;};int main(void){struct Node *head;struct Node *create();struct Node *newone(int n); head=create();output(head->next);getch();return 0;}output(struct Node *head){struct Node *p;p=head;while(p){printf("%3d",p->num);p=p->next;}}struct Node *create(){struct Node *p,*p1,*p2,*head; int n=0,i=2;p1=p2=head=NULL;n=0;head=newone(-1);printf("No 1:");scanf("%d",&n);if(n!=-1){p=newone(n);head->next=p;}else{printf("its over");return NULL;}while(1){printf("No %d:",i);scanf("%d",&n);if(n==-1){break;}else{/***************************************************p1,p2两个指针一前一后同时移动,p节点插入到p1,p2之间****************************************************/ p=newone(n);p1=head->next;p2=head;while(p1!=NULL){if(p1->num < n){p2=p1;p1=p1->next;}elsebreak;}p2->next=p;p->next=p1;}i++;}return head;}struct Node *newone(int n){struct Node *p;p=(struct Node *)malloc(LEN);p->num=n;p->next=NULL;return(p);}struct Node *create1(){struct Node *p,*p1,*head,*tmp;int n=0,i=2;p=p1=head=NULL;n=0;head=newone(-1);printf("No 1:");scanf("%d",&n);if(n!=-1){p=newone(n);head->next=p;}else{printf("its over");return NULL;}while(1){printf("No %d:",i);scanf("%d",&n);if(n==-1){break;}else{/****************************************************************** 这一段只使用一个指针移动,p节点插入到p1->next与p1之间,tmp是临时使用的******************************************************************/ p=newone(n);p1=head;while(p1->next!=NULL&&n>p1->next->num)p1=p1->next;tmp=p1->next;p1->next=p;p->next=tmp;}i++;}return head;}。