【正文】
于動(dòng)態(tài)鏈表的函數(shù) ? C標(biāo)準(zhǔn)函數(shù)庫(kù)中動(dòng)態(tài)存儲(chǔ)分配的函數(shù) : malloc( ) calloc( ) – malloc函數(shù) ?作用 : 在內(nèi)存中開辟指定大小的存儲(chǔ)空間 ,并將此存儲(chǔ)空間的起始地址作為函數(shù)返回值 . ?malloc函數(shù)的使用 : malloc(8)開辟長(zhǎng)度為 8個(gè)字節(jié)的存儲(chǔ)空間 ,若其起始地址為 1268,則 malloc(8)的返回值為 1268,且返回的指針值指向 void型 .將此地址賦給一個(gè)指向 long型的指針變量 : p=(long *)malloc(8)。 else pfnext=pb。 if (head==NULL) /*空表插入 */ { head=pi。 while (pbnum!=num amp。 r=(struct NODE *)malloc(sizeof(struct NODE))。c。 8 data next 4 data next 3 data next head ↑p ↑q B The C Programming Language Chapter 9 Structures and Unions ? 共用體 ?構(gòu)造數(shù)據(jù)類型 ,也叫聯(lián)合體 ?用途:使幾個(gè)不同類型的變量共占一段內(nèi)存 (相互覆蓋 ) – 共用體類型定義 定義形式: union 共用體名 { 類型標(biāo)識(shí)符 成員名; 類型標(biāo)識(shí)符 成員名; ……………. }; 例 union data { int i。 }a,b,c。 (?) – 不能 在定義共用體變量時(shí) 初始化 例 union { int i。 (?) x=。 ( 2) 直接定義 例如 , enum [weekdays] {Sun,Mon,Tue,Wed,Thu,Fri,Sat } workday。break。 以下各選項(xiàng)企圖說(shuō)明一種新的類型名,其中正確的是()。a。 /*以整型方式輸入枚舉變量 */ switch (a)。 char position[10]。 =39。 char ch。 union data a,b,c,*p,d[3]。 D) q=qnext。 =b。 struct NODE *next。 ?} The C Programming Language Chapter 9 Structures and Unions 例從鏈表中刪除一個(gè)結(jié)點(diǎn) struct stu * delete(struct stu * head ,int num) { struct stu *pf,*pb。amp。 scanf(%d%d,amp。b。 float score。,19}, {10104,Wang Min,39。 } 例 int n。 int age。 ? int i。 The C Programming Language Chapter 9 Structures and Unions 例 1:計(jì)算學(xué)生的平均成績(jī)和不及格人數(shù) ?struct student ? { ? int num。 int age。 }。 int age。 char name[20]。 char sex。 char name[20]。 char name[20]。 int day。 int age。 char sex。 char name[20]。 struct student stu1,stu2。 The C Programming Language Chapter 9 Structures and Unions – 直接定義結(jié)構(gòu)體變量 一般形式: struct { 類型標(biāo)識(shí)符 成員名; 類型標(biāo)識(shí)符 成員名; ……………. }變量名表列 ; 例 struct { int num。 num name birthday month day year 例 struct student { int num。 +=。 }stu1,stu2。 }stu1,stu2。M39。 形式二 : struct student { int num。 char name[20]。 分行初始化 : struct student { int num。 }str[3]。 ? char phone[10]。 ? 使用結(jié)構(gòu)體指針變量引用成員形式 存放結(jié)構(gòu)體變量在內(nèi)存的起始地址 num name sex age stu p struct student { int num。 printf(Score = %f\n,(*spoint).score)。M39。 – 前一個(gè)結(jié)點(diǎn)指向下一個(gè)結(jié)點(diǎn) ,只有通過(guò)前一個(gè)結(jié)點(diǎn)才能找到下一個(gè)結(jié)點(diǎn)。 建立鏈表: head=amp。in。 pnext!=NULL) p=pnext。 ?} ?else ?{ ?pbnext=pi。 end: return head。 }a,b,c,*p,*q。 C) q=(*q).next。 char ch。 }。 float f。 char sex。 The C Programming Language Chapter 9 Structures and Unions include main( ) { enum weekday { sun , mon , tue , wed , thu , fri , sat } a 。 Int *p。 v2=int。 INTEGER a,b。 ( 2) 取值表中的值稱為枚舉元素 , 其含義由程序解釋 。 }a。 }a={1, 39。a39。 }。 (*q).next=amp。rnum=30。 pb = pbnext。amp。 } return(head)。 int age。 賦予有用數(shù)據(jù): =99101。 } num name sex age stu[0] p stu[1] stu[2] p+1 The C Programming Language Chapter 9 Structures and Unions 動(dòng)態(tài)存儲(chǔ)分配和鏈表的概念 動(dòng)態(tài)存儲(chǔ)分配: 由程序員控制的存儲(chǔ)分配方法,根據(jù)需要臨時(shí)分配內(nèi)存單元以存放有用數(shù)據(jù),當(dāng)不用時(shí),又可以釋放存儲(chǔ)單元,以便其用于分配給其它數(shù)據(jù)使用。 =101。 }student1 = {1001, Zhang, }, *spoint。 ? } ?printf(name\t\tphone\n\n)。 i5 。F39。,19}。 num name sex age num name sex age stu[0] stu[1] 25B The C Programming Language Chapter 9 Structures and Unions – 結(jié)構(gòu)體數(shù)組初始化 例 struct { int num。 The C Programming Language Chapter 9 Structures and Unions ? 結(jié)構(gòu)體數(shù)組 – 結(jié)構(gòu)體數(shù)組的定義 三種形式: 形式一 : struct student { int num。 The C Programming Language Chapter 9 Structures and Unions – 形式二: struct 結(jié)構(gòu)體名 {