复杂数据类型
1.结构体
基本定义:
struct 结构名
{
//成员列表
};
成员列表:
由基本数据类型定义的变量或者是构造类型的变量
example:
struct student
{
int grade;
int age;
char name[32];
};
student:结构体名称
struct student:结构数据类型,类似于:int等
struct student stu;
stu:结构体变量
访问结构成员:"."
访问结构体成员:
stu.name;
stu.grade;
stu.age;
2.结构体变量的初始化
struct student
{
char name[32];
char sex;
int age;
};
(1)初始化1
struct student boy;
strcpy(boy.name,"jack");
boy.age = 24;
boy.sex = 'm';
(2)初始化2
struct student girl = ("locy",'f',20);
(3)初始化3(不建议)
声明结构体时,定义结构体变量
声明结构体时,定义结构体变量
struct student
{
char name[32];
char sex;
int age;
}stu,stu1;
struct student
{
char name[32];
char sex;
int age;
}stu{"xiaoming",'m',22};
3.无名结构体
struct
{
int age;
char name[16];
}stu;
无名结构体一般不使用
4.宏定义结构体
宏是原地替换
struct student
{
char name[32];
char sex;
int age;
};
#define STU struct student
STU stu,stu1; <------> struct student stu,stu1;
5.结构体的嵌套
struct date
{
int year;
int month;
int day;
};
struct student
{
char name[32];
int age;
struct date birthday;
};
嵌套定义结构体
//不可以
struct student
{
char name[32];
int age;
struct student stu; //stu大小不确定
};
//可以
struct student
{
char name[32];
int age;
struct student *p; //因为指针的大小固定
};
6.结构体数组
struct student arr[3];
7.结构体指针
struct date *pa;
struct student *pb;
#include<stdlib.h>
//空间的申请和释放
malloc(); //申请堆空间
free(); //释放空间
//申请一块堆空间,大小为:sizeof(struct student)
pa = (struct date *)malloc(sizeof(struct date));
free(pa); //释放申请的堆空间
8.typedef
重新取名
typedef int I;
给int取名为I
结构体
typedef struct student
{
//成员列表
}STU;
两者等价:
struct student
{
//成员列表
};
typedef struct student STU;
和宏定义的区别
typedef struct student STU;
#define STU struct student;
9.结构体大小
内存对齐:
Linux: 4字节
Windows:8字节
默认从偏移量为0的位置开始存储
每个成员所占字节数是其自身大小的整数倍
是最大成员所在字节的整数倍
10.联合体
union untype
{
int a;
long b;
};
特点:
不管是赋值还是访问每次只能操作一个变量
分配空间按最大数据类型分配
11.枚举类型
enum entype
{
A, //默认从0开始,一般是大写
B
};
枚举类型的值可以拿过来直接用
12.链表
链式存储结构,线性存储结构
其大小可动态改变,链表是由一个个结点串起来的数据链
结点:
由数据域和指针域组成,
数据域:存放数据
指针域:存放下一个结点的地址
(1)创建链表
struct student
{
int id;
struct student *next;
};
struct student *head;
malloc()
free()
创建一个头结点:
struct student *head;
head = (struct student *)mallocsizeof((struct student));
头结点标识一个链表,即链表名称
头结点的数据域不存放数据,指针域存放第一个结点的地址,
头结点只是为了标识这个链表
链表例子
#include<stdio.h>
#include<stdlib.h> //malloc,free
struct student
{
int ID;
char name[32];
struct student *next;
};
#define LEN sizeof(struct student)
//=============头插=============================================
struct student *add_link(struct student *head)
{
struct student *temp = (struct student*)malloc(LEN);
printf("input ID:");
scanf("%d",&temp->ID);
printf("input name:");
scanf("%s",temp->name);
//temp->next = NULL;
temp->next = head->next;
head->next = temp;
temp = NULL;
return head;
}
//==============头删============================================
struct student *sub_link(struct student *head)
{
struct student *temp = head->next;
head->next = temp->next;
free(temp);
temp = NULL;
return head;
}
//=============尾删============================================
struct student *subwei(struct student *head)
{
struct student *temp = head->next;
struct student *temp1 = head;
while(temp->next != NULL)
{
temp1 = temp;
temp = temp->next;
}
temp1->next=NULL;
free(temp);
temp = NULL;
return head;
}
//===============尾插========================================
struct student *addwei(struct student *head)
{
struct student *p = head;
struct student *temp= (struct student*)malloc(sizeof(LEN));
printf("input ID:");
scanf("%d",&temp->ID);
printf("input name:");
scanf("%s",temp->name);
while(p->next != NULL)
{
p = p->next;
}
temp->next = p->next;
p->next = temp;
temp = NULL;
return head;
}
//==============打印============================================
void show_link(struct student *head)
{
struct student *p = head->next;
printf("\tID\tname\n");
while(p != NULL)
{
printf("\t%d\t%s\n",p->ID,p->name);
p = p->next;
}
return;
}
//=============主函数=========================================
int main()
{
//创建头结点
struct student *head;
struct student *p;
head = (struct student*)malloc(sizeof(struct student));
head->next = NULL;
//添加
int i;
for(i=0;i<4;i++)
add_link(head);
show_link(head);
sub_link(head);
show_link(head);
subwei(head);
show_link(head);
addwei(head);
show_link(head);
return 0;
}
总结
学到现在综合性开始很强了,要注意把以前的知识点串起来
链表和结构体的运用要熟练,注意理解。