struct part { //此处part 指的是结构体标记
int value;
char name [20] //结构体成员
int paice ;
} part1 ; //创建了结构体标记part之后 part1 是在申明变量;
申明变量的另外一种方式:
struct part part1,part2;
//结构体初始化及其引用;
main(){
struct part part1 = { 5," jiahina " , 66 };//初始化
访问:part1.name;
printf("%c",part1.name);
int[];
struct part part1_2 [2] ={{5," jiahina " , 66 }.{5," jiahina " , 67 }}; // 结构体数组
printf("%s,%d",part1_2 [0].name , part1_2 [1]. price);
}
指向结构体的指针变量
struct part *w;
w = &part1;
printf("name = %s\n",(*p).name);
指向结构体数组的指针
struct part *p ;
p = part1_2;指向第一个数组元素;
p++ ; // 等价于
链表
数据+ 下一个节点的地址
静态链表:
struct part {
int price ;
int atk;
struct part *next;
};
int main{
//定义三个part的结构体变量,作为链表的节点;还需要一个头指针head
struct part a , b , c , *head;
a.price = 5;
a.atk = 1;
b.price = 5;
b.atk = 1;
c.price = 5;
c.atk = 1;
//将节点连成链表
head = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
struct part *p;
p = head ;
while (p != NULL){
printf("%d\n",p.price);
p = p ->next;
动态链表(单向);
//需要创建链表的函数,函数的返回值是链表的头指针; 返回值需要定义成* 类型;
struct part {
int price ;
int atk;
}
struct part *creat(){
struct part *head;
struct part *p1 ,*p2;//一个数用来保存当前的一个用来保存当前之前的一个节点;
p1 = p2 = (struct part *)malloc (sizeof( struct part));
scanf("%d%d",p1->price;p1->atk);
head = NULL;
while(p -> price != 0){
if(n = = 1){
head = p1;
}
p2 -> next = p1;
p1 = p1->next;
p1 = (struct part *)malloc(sizeof(struct part ));
scanf("%d%d",p1->price;p1->atk);
}
int main ()
{
creat();
printf("%d,%d",)
}