一、用typedef说明一个新类型
基本类型:int、float,double,char
构造类型:结构体 共用体
结构体变量所占的内存空间就位各成员的总和。
(1)常规定义及使用
struct student
{char name[12];
char sex;
int year,month,day;
float sc[4];
};
struct student std,pers[3],*pstd;
pers[0].year=1998;
pers[0].day=22;
pstd->year=1996;
(2)结构体的定义和定义变量放在一起
struct student
{char name[12];
char sex;
int year,month,day;
float sc[4];
}student std,pers[3],*pstd;
(3)不写标识名,但变量已规定死了
struct
{char name[12];
char sex;
int year,month,day;
float sc[4];
}student std,pers[3],*pstd;
(4)为结构体起别名
typedef struct
{char name[12];
char sex;
int year,month,day;
float sc[4];
}STREC;
STREC student std,pers[3],*pstd;
给结构体变量和数组赋值
变量:
struct student
{char name[12];
char sex;
int year,month,day;
float sc[4];
}std={“Li ming”,‘M’,1962,5,10,88,85,99};
//sc.[0] sc.[1] .
数组:
struct bookcard
{char num[5];
float money;
}bk[3]={{"NO.1","35.5"},{"NO.2","25.0"},{"NO.3","66.7"}};
引用:
struct student
{char name[12];
char sex;
int year,month,day;
float sc[4];
}std,arr[5],*ps;
ps=&std;
std.sex //...的
ps->sex
(*ps).sex
二、共用体 union
区别:结构体变量中的成员各自占有自己的存储空间,而共用体中的所有成员占有同一个存储空间。
三、链表
顺序存储、链式存储
链表的组成
头指针:存放一个地址,该地址指向一个指针
结点:用户需要的实际数据和连接节点的指针
定义一个节点:
struct student
{int num;
float score;
struct student *next;
};
或:
typedef struct node
{int data;
lnode *next;
}lnode;
链表的遍历
#include <stdio.h>
#define NULL 0
struct student
{long num;
float score;
struct student *next;
};
main()
struct student a,b,c,*head,*p;
a.num=99101;a.score=89.5;
b.num=99103;a.score=90;
c.num=99107;a.score=85;
head==&a;
a.next=&b;
b.next=&c;
c.next=NULL;
p=head;
do{
print("%ld,%5.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
在节点p,q之间插入节点s的关键算法:
s->next=p->next;
p->next=s;
删除节点
假设三个节点
p->next=p->next->next;
free(s);