01.C语言关于结构体的学习笔记
我对于学习的C语言的结构体做一个小的学习总结,总结如下:
结构体:structure
结构体是一种用户自己建立的数据类型,由不同类型数据组成的组合型的数据结构。在其他高级语言中称为记录(record)。
声明一个结构体类型的一般形式为:
struct 结构体名{
类型名 成员名1;
类型名 成员名2;
……};
注意: 结构体类型的名字是由一个关键字struct 和结构体名组合而成的如struct Student.
结构体名是由用户指定的,又称“结构体标记”以区别于其他结构体类型。
花括号内是该结构体所包括的子项,称为结构体的成员。对各成员都应进行类型声明。
在定义结构体变量时可以对它的成员变量初始化。是对结构体变量的初始化,而不是对结构体类型。
struct Student b={.name="Zhang Fang"}; //在成员名前有成员运算符“.”
可以引用结构体变量中成员的值,引用方式为
结构体变量名.成员名.如: student1.num=10010;
结构体数组的举例:
例: 有n个学生的信息(包括学号、姓名、成绩),要求按成绩的高低顺序输出各学生的信息。
思路分析:用结构体数组存放n个学生信息,采用选择法择各元素进行排序。
1 #include
2
3 struct Student //声明结构体类型stuct Student
4
5 {int num;
6
7 char name[20];
8
9 float score;
10
11 };
12
13 int main()
14
15 {
16
17 struct Student stu[5]={{10101,"Zhang",78},{10103,"Wang"98.5},{10106,"Li",86},{10108,"Ling",73.5},{10110,"Sun",100}}; //定义结构体数组并初始化
18
19 struct Strudent temp; //定义结构体变量temp用作交换时报的临时变量
20
21 const int n=5; //定义常变量n。这里也可以用符号常量 #define N 5
22
23 int i,j,k;
24
25 printf("The order is :\n");
26
27 for(i=0;i
28
29 {k=i;
30
31 for(j=i+1;j
32
33 {
34
35 if(stu[j].score>stu[k].score) //进行成绩比较。
36
37 k=j;
38
39 }
40
41 temp=stu[k]; stu[k]=stu[i]; stu[i]=temp;//互换stu[k]和stu[i]元素
42
43 }
44
45 for(i=0;i
46
47 printf("m %8s %6.2f\n",stu[i].num,stu[i].name,stu[i].score);
48
49 printf("\n");
50
51 return 0;
52
53 }
结构体指针举例:
1. 指向结构体变量的指针:
1 #include
2
3 #include
4
5 int main()
6
7 {
8
9 struct Student
10
11 {
12
13 long num;
14
15 char name[20];
16
17 char sex;
18
19 float score;
20
21 };
22
23 struct Student stu_1; //定义struct Student 类型的变量stu_1
24
25 struct Student*p; //定义指向struct Student类型数据的指针变量p
26
27 p=&stu_1; //p指向 stu_1
28
29 stu_1.num=10101; //对结构体变量的成员赋值
30
31 strcpy(stu_1.name,"Li Lin"); //用字符串复制函数给stu_1.name赋值
32
33 stu_1.sex='M';
34
35 stu_1.score = 89.5;
36
37 printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f'\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);
38
39 //下面用指针访问成员变量输出结果是一样的。
40
41 printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f'\n", (*p).num,(*p).name,(*p).sex,(*p).score);
42
43 return 0;
44
45 }
46
47 如果p指向一个结构体变量stu,以下三种用法等价:
48
49 1.stu.num;
50
51 2.(*p).num;
52
53 3.p->num;
链表:
链表是动态地进行存储分配的一种结构。
链表有一个头指针变量,它在放一个地址,该地址指向一个元素。链表中每一个元素称为结点,第个结点都应包括两个部分:1.用户需要用的实际数据;2.下一个结点的地址。该元素不再指向其他元素在,它称为表尾,它的地址部分放一个NULL表示空地址,链表到此结束。
建立简单的静态链表:
例:建立一个简链表,它由3个学生数据的结点组成,要求输出各结点为中的数据。
1 #include
2
3 struct Student //声明结构体类型struct Student
4
5 {int num;
6
7 float score;
8
9 struct Student*next;
10
11 };
12
13 int main()
14
15 {
16
17 struct Student a,b,c,*head,*p; //定义3个结构体变量a,b,c作为链表的结点
18
19 a.num=10101;a.score=89; //对结点a的num和score成员赋值
20
21 b.num=10103;b.score=90;
22
23 c.num=10107;c.score=84;
24
25 head=&a; //将结点a的起始地址赋给头指针head
26
27 a.next=&b; //将结点b的起始地址赋给a结点的next成员
28
29 b.next=&c; //将c结点的起始地址赋给b的next成员
30
31 c.next=NULL; //将c结点的next成员不存放其他结点地址
32
33 p=head; //使p也指向a结点
34
35 do {
36
37 printf("%ld %5.1f\n",p->num,p->score);//输出p指向的结点的数据
38
39 p=p->next; //使p指向下一个结点
40
41 }while(p!=NULL); 输出完c结点后p的值为NULL,循环终止。
42
43 return 0;
44
45 }
这种所有结点都 是在程序中定义的,不是临时开辟的,也不能用完后释放,这种链表称为静态链表。
建立动态链表:
例:写一个函数建立一个有3名学生数据的单向动态链表。
1 #include
2
3 #include
4
5 #define LEN sizeof(struct Student)
6
7 struct Student
8
9 {long num;
10
11 float score;
12
13 struct Student*next;
14
15 };
16
17 int n; //n为全局变量,本文件模块中各函数均可使用它
18
19 struct Student*creat(void) //定义函数。此函数返回一个指向链表头的指针
20
21 {struct Student*head;
22
23 struct Student*p1,*p2;
24
25 n=0;
26
27 p1=p2=(struct Student*)malloc(LEN); //开辟一个新单元
28
29 scanf("%ld,%f",&p1->num,&p1->score);//输入第一个学生的学号和成绩
30
31 head=NULL;
32
33 while(p1->num!=0)
34
35 {n=n+1;
36
37 if(n==1)head = p1;
38
39 else p2->next=p1;
40
41 p2=p1;
42
43 p1=(struct Student*)malloc(LEN); //开辟动态存储区,把起始地址赋给p1
44
45 scanf("%ld,%f",&p1->num,&p1->score); //输入其他学生的学号和成绩
46
47 }
48
49 p2->next=NULL;
50
51 return(head);
52
53 }
54
55 int main()
56
57 {struct Student*pt;
58
59 pt=creat(); //调用creat函数,返回链表第一个结点地址。
60
61 printf("\nnum:%ld\nscore:%5.1f\n",pt->num,pt->score);//输出第一个结点的成员值
62
63 return 0;
64
65 }
好啦!今天的学习笔记先写到这里。