第6章 结构体与链表.pptVIP

  • 5
  • 0
  • 约6.68千字
  • 约 32页
  • 2018-03-08 发布于河南
  • 举报
第6章 结构体与链表

第6章 结构体与链表 崔雪冰 计算机科学与工程系 6.2 结构体的定义及应用 结构体类型属于用户自定义类型,与指针类型有类似之处,必须先定义数据类型,然后再定义该种类型的变量。 6.2.1 定义结构体类型 struct 结构体类型名 { 成员列表; }; 6.2.1 定义结构体类型 6.2.2 定义结构体类型的变量 1.先定义结构体类型再定义变量 例如:student John, Merry; 2.在定义结构体类型的同时定义结构体变量 struct student { int ID; char name[20]; char gender; date birthday; double score[3]; } John, Merry; 6.2.2 定义结构体类型的变量 3.使用无名结构体类型定义结构体变量 struct { int ID; char name[20]; char gender; date birthday; double score[3]; } John, Merry; 6.2.3 初始化结构体类型的变量 初始化方法有两种: 一种是用花括号“{ }”括起来的值对结构体变量初始化;例如: student John = {801,Joe,f,{1990,6,16},{88,99,80}}; 另一种是用同类型的结构体变量初始化另外一个结构体变量。 student Merry = John; 6.2.4 结构体类型变量及其成员的引用 6.2.5 结构体数组与指针 1.定义结构体数组和初始化 定义的形式: student studOne[4]; 初始化的形式: student studTwo[4] = { {801, Joe, F, {1990, 6, 16}, {88, 99, 80}}, {802, Smith,F, {1991, 6, 1}, {68, 79, 87}}, {805, Merry,M, {1992, 3, 23}, {78, 89, 79}}, {808, Anna, M, {1993, 7, 6}, {98, 69, 68}} }; 6.2.5 结构体数组与指针 6.3 用typedef定义类型 语法格式为: typedef 类型名1 类型名2 ; 例如: typedef int WorkDay; WorkDay day; 定义新类型的步骤:1~3. 6.4 单向链表 6.4.1 链表的概念 6.4 单向链表 6.4.2 带头结点的单向链表常用算法 6.4.2 带头结点的单向链表常用算法 作业 1,4~7 * 例如1: struct date { int year; int month; int day; }; 例如2: struct student { int ID; char name[20]; char gender; date birthday; double score[3]; }; 1.引用结构体变量的成员 例如:John.ID=901; 2.整体引用结构体变量 student John = {801,Joe,f,{1990,6,16},{88,99,80}}; student Merry; Merry = John; // 对结构体变量整体赋值 注意: (1) 不能将结构体变量作为一个整体进行输入或输出,例如: cout John; // 错误 cin Merry; // 错误 (2) 结构体变量可以用作函数的参数,属于按值传递。 (3) 函数可以返回一个结构体变量。 【6.1简略版】在main函数中输入一个学生的学号、姓名、性别、出生日期和3门课程的成绩,在另一函数print中输出这个学生的信息,采用结构体变函数参数。 void print( student ); void main ( ) { student John; cin John.ID John.name John.gender; cin John.birthday.year John.score[2]; print(John); } void print( student s) { cout 学号: setw(5) s.ID endl;

文档评论(0)

1亿VIP精品文档

相关文档