結(jié)構(gòu)體:
- 1,基本定義:
{
//成員列表
}轨域;
成員列表:
有基本數(shù)據(jù)類型定義的變量或者構(gòu)造類型的變量
example:
struct student
{
int grade;
int age;
char name[32]; };
`student`:結(jié)構(gòu)體名稱
`struct student`:結(jié)構(gòu)數(shù)據(jù)類型雕沿,相當于`int,double迫横,char`等基本的數(shù)據(jù)類型鸥拧。
`struct student stu`;
`stu`:結(jié)構(gòu)體變量
訪問結(jié)構(gòu)成員:“.”,`stu.age;stu.grade`尺迂;
2,結(jié)構(gòu)體變量的初始化
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 studetn gril = {"jack",24,'m'}`;
和成員列表一一必須對應骗露。。
(3)定義的同時初始化(不建議)
struct student{
char name[32];
char sex;
int age;
}stu = {"fuck",'W',24};
* 嵌套定義結(jié)構(gòu)體:
struct student{
int a;
char b;
struct student stu;
};
這種方法不可以怪瓶。。
struct student{
int a;
char b;
struct student *ps;
};
指針大小的是固定的
* 3 無名結(jié)構(gòu)體
struct{
int age;
char name[16];
}stu;
**結(jié)構(gòu)體變量可以賦值給相同類型的變量**
* 4 ,宏定義結(jié)構(gòu)體名稱
`#define STU struct student`
* 5,結(jié)構(gòu)體的嵌套
struct date{
int year;
int month;
int day;
};
struct student{
char name[32];
int age;
struct date birthday;
};```
- 6,結(jié)構(gòu)體指針:
struct student stu;
struct student *p = &stu;
strcpy(p->name,"hello");
p->age = 13;
p->birthday.year = 1990;
p->birthday.month = 2;
p->birthday.day = 13;
printf("%s,%d,%d,%d,%d\n",p->name,p->age,p->birthday.year,p->birthday.month,p->birthday.day);
malloc()
//申請堆空間
free()
//釋放空間
//申請堆空間践美,大小為sizeof(struct student)
pa = (struct date *)malloc(sizeof(struct date))
;
free(pa)
;//申請釋放的堆空間
- 7,typedef
重新命名
typedef int I
;
即給int取別名為I洗贰;
結(jié)構(gòu)體的字符串代替;
typedef struct student{
int age;
char name[32];
}STU;
/*
struct student{
int age;
char name[32];
};
typedef struct student STU;*/
和宏定義的區(qū)別
typedef struct student* STT;
#define STD struct student*
STT stu1,stu2
;//stu1 和stu2都是指針
STD stu3,stu4
;//stu3是指針陨倡,stu4不是指針
define
只是簡單的字符串替換
- 8,結(jié)構(gòu)體的大小
內(nèi)存對齊
Linux:4字節(jié)
Windows:8字節(jié)
struct student{ //默認從零開始
char a; //1 【0~8】
long age; //8 【9~16】
char name[31]; //3 【17~48】
};
int main(){
printf("%ld",sizeof(struct student));//48
return 0;
}```//最長字節(jié)數(shù)的倍數(shù)
* 9 聯(lián)合體
union untype{
int a;
long b;
};
特點:每次只能操作一個成員變量敛滋。
分配空間:按最大數(shù)據(jù)類型分配空間兴革。
* 11,枚舉類型
enum entype{
A,//0
B = 12,//12
C//13
};
枚舉類型中都是具體的數(shù)據(jù)杂曲,不能直接使用`.`的調(diào)用。擎勘。是一種數(shù)據(jù)類型咱揍,而不是構(gòu)造類型
* 12,鏈表
鏈式存儲結(jié)構(gòu)煤裙,線性存儲結(jié)構(gòu)
其大小可動態(tài)改變蟹地,鏈表是有一個個節(jié)點串起來的數(shù)據(jù)鏈
**節(jié)點**:由數(shù)據(jù)域和指針域構(gòu)成积暖;
數(shù)據(jù)域:存儲數(shù)據(jù)藤为;
指針域:存放下一個節(jié)點的地址怪与。
(1)創(chuàng)建鏈表
struct student{
int id;
struct student *next;
};
struct student *head;
malloc();
free()
創(chuàng)建一個頭節(jié)點:
struct student *head;
head = (struct student *)malloc(sizeof(struct student));
頭節(jié)點標識一個鏈表缅疟,即鏈表名稱
**頭節(jié)點的數(shù)據(jù)域不存放數(shù)據(jù),指針域存放第一個節(jié)點的地址**耘斩;
#######總結(jié):利用結(jié)構(gòu)體產(chǎn)生鏈表桅咆,感覺程序員都好聰明。。薛夜。
####作業(yè);
操作頭節(jié)點
include<stdio.h>
include<stdlib.h>
typedef struct student{
int ID;
char name[32];
struct student next;
}STU,pSTU;
//創(chuàng)建節(jié)點
pSTU createNewNode(){
pSTU newLink;
newLink = (pSTU)malloc(sizeof(STU));
newLink->next = NULL;
return newLink;
}
//從頭部添加節(jié)點
pSTU addHeadLink(pSTU head){
pSTU temp = createNewNode();
printf("input ID:");
scanf("%d",&temp->ID);
printf("input name:");
scanf("%s",temp->name);
temp->next = head->next;
head->next = temp;
temp = NULL;
return head;
}
//顯示鏈表
void showLink(pSTU head){
pSTU p = head->next;
printf("\tID\tname\n");
while(p != NULL){
printf("\t%d\t%s\n",p->ID,p->name);
p = p->next;
}
return;
}
//從頭節(jié)點刪除
pSTU deleteHeadLink(pSTU head){
pSTU temp = head->next;
head->next = temp->next;
free(temp);
temp = NULL;
return head;
}
int main(int argc,char *argv[]){
pSTU head = createNewNode();
addHeadLink(head);
showLink(head);
deleteHeadLink(head);
showLink(head);
return 0;
}
操作尾節(jié)點
include<stdio.h>
include<stdlib.h>
typedef struct student{
int ID;
char name[32];
struct student next;
}STU,pSTU;
//創(chuàng)建一個新節(jié)點
pSTU createNode(){
pSTU link;
link = (pSTU)malloc(sizeof(STU));
link->next = NULL;
return link;
}
//在尾部添加一個節(jié)點
pSTU addTailNode(pSTU head){
pSTU p = head;
while(p->next != NULL)
p = p->next;
/*
pSTU temp;
temp = (pSTU)malloc(sizeof(STU));
temp->next = NULL;
*/
pSTU temp = createNode();
printf("input ID:");
scanf("%d",&temp->ID);
printf("input name:");
scanf("%s",temp->name);
p->next = temp;
temp = NULL;
return head;
}
void showLink(pSTU head){
pSTU p = head->next;
printf("\tID\tname\n");
while(p != NULL){
printf("\t%d\t%s\n",p->ID,p->name);
p = p->next;
}
}
//尾刪;
pSTU deleteTailLink(pSTU head){
pSTU p1 = head;
pSTU p2 = head->next;
while(p2->next != NULL){
p2 = p2->next;
p1 = p1->next;
}
p1->next = NULL;
free(p2);
p2 = NULL;
return head;
}
int main(int argc,char *argv[]){
pSTU head = createNode();
int i = 0;
for(i = 0;i < 4;i++){
addTailNode(head);
}
showLink(head);
for(i = 0;i<2;i++)
deleteTailLink(head);
showLink(head);
return 0;
}