【正文】
ree(q)。 C) q=(*q).next。 pnext=q。 char ch。 char ch。 char ch。 形式三 : union { int i。 – 共用體變量的定義 f ch i f ch i a b 共用體 變量定義 分配內(nèi)存 , 長度 =最長成員 所占字節(jié)數(shù) 共用體 變量任何時刻 只有 一個成員 存在 The C Programming Language Chapter 9 Structures and Unions – 共用體變量引用 ? 引用方式: 例 =1。 printf(%d,)。 }。 float f。 char ch。,}。 float f。a39。 (?) The C Programming Language Chapter 9 Structures and Unions – 結(jié)構(gòu)體與共用體 ? 區(qū)別 : 存儲方式不同 struct node { char ch[2]。 int k。 char sex。 }category。 3 . 說明 ( 1) 枚舉型僅適應(yīng)于取值有限的數(shù)據(jù) 。 事實(shí)上 , 枚舉元素用什么表示都可以 。 The C Programming Language Chapter 9 Structures and Unions include main( ) { enum weekday { sun , mon , tue , wed , thu , fri , sat } a 。 { case mon: case tue: case wed: case thu: case fri: printf(工作日 \n)。 default : printf( 輸入錯誤 \n )。 STUDENT studend1,student2, *p。 Int *p。 =。 v1 int。 。 v2=int。 D.*p=data。 A. p=amp。 float b。 INTEGER a,b。 case sun: case sat: printf ( 休息日 \n )。a)。 ( 4) 枚舉元素的值也是可以人為改變的:在定義時由程序指定 。 ( 2) 取值表中的值稱為枚舉元素 , 其含義由程序解釋 。 The C Programming Language Chapter 9 Structures and Unions 1. 枚舉類型的定義 enum 枚舉類型名 {取值表 }; 例如 , enum weekdays {Sun,Mon,Tue,Wed,Thu,Fri,Sat}; 2 . 枚舉變量的定義 ──與結(jié)構(gòu)變量類似 ( 1) 間接定義 例如 , enum weekdays workday。 union { int class。 a ch k b ch k 變量的各成員同時存在 任一時刻只有一個成員存在 ? 聯(lián)系 : 兩者可相互嵌套 The C Programming Language Chapter 9 Structures and Unions 例 結(jié)構(gòu)體中嵌套共用體 name num sex job class position Li Wang 1011 2086 F M S T 501 prof struct { char name[10]。 }a。 b=a。 =1。 union { int i。 }a={1, 39。 a=1。 pi pch pf (*p).i (*p).ch (*p).f d[0].i d[0].ch d[0].f – 共用體變量中起作用的成員是 最后一次存放的成員 例 union { int i。 char ch。a39。 float f。 }。 }a,b。 }。 free(p)。 free(q)。 B) (*p).next=(*q).next。 (*q).next=amp。 C) pnext=amp。則能夠把 c插入到 a和 b 之間并形成新的鏈表的 語句組是 ____ a b data next data next ↑p c data next ↑q A) =c。 } A) 10 B) 20 C) 30 D) 40 D The C Programming Language Chapter 9 Structures and Unions 例:若以下定義 : struct link { int data。rnum=30。 q=(struct NODE *)malloc(sizeof(struct NODE))。 include stdlib struct NODE {int num。 printf(The node is deleted\n)。 pb = pbnext。 } pb =head。 ?} /*在表末插入 */ ?} ?return head。 /*在第一結(jié)點(diǎn)之前插入 */ ?else pfnext=pi。amp。 pb=head。 if (pnum!=namp。 while (pnum!=n amp。 } return(head)。 if(i==0) pf=head=pb。 printf(input Number and Age\n)。 int i。 int age。 =0。 =amp。 =99107。 賦予有用數(shù)據(jù): =99101。 3010 99101 3028 head 3010 99103 90 4016 3028 99107 85 4016 NULL A B C num score next The C Programming Language Chapter 9 Structures and Unions 例 建立一個簡單鏈表 定義結(jié)點(diǎn): struct student {long num。 定義結(jié)點(diǎn): struct student {int num。 – 最后一個結(jié)點(diǎn)的地址項(xiàng)不指向任何結(jié)點(diǎn),賦以值 NULL。 } num name sex age stu[0] p stu[1] stu[2] p+1 The C Programming Language Chapter 9 Structures and Unions 動態(tài)存儲分配和鏈表的概念 動態(tài)存儲分配: 由程序員控制的存儲分配方法,根據(jù)需要臨時分配內(nèi)存單元以存放有用數(shù)據(jù),當(dāng)不用時,又可以釋放存儲單元,以便其用于分配給其它數(shù)據(jù)使用。 main() { struct student *p。M39。 int age。 =101。 *p=10。 printf(Score = %f\n ,spointscore)。 printf(Score = %f\n,)。 }student1 = {1001, Zhang, }, *spoint。stu。 char sex。 ?} ? The C Programming Language Chapter 9 Structures and Unions ? 結(jié)構(gòu)體和指針 – 指向結(jié)構(gòu)體變量的指針 ? 定義形式: struct 結(jié)構(gòu)體名 *結(jié)構(gòu)體指針名 。 ? } ?