结构体、共用体和用户自定义类型

一、用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,510,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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值