語言第17講共用體鏈表_第1頁
語言第17講共用體鏈表_第2頁
語言第17講共用體鏈表_第3頁
語言第17講共用體鏈表_第4頁
語言第17講共用體鏈表_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

第17講

共用體

鏈表2如何定義數(shù)據(jù)類型存儲下列表格中的數(shù)據(jù)姓名name年齡age所在辦公室office工作job(或班級class)structperson{charname[10];intage;charjob;

depa;};需要一個數(shù)據(jù)單元,但不同情況下存不同的數(shù)據(jù)。李四21學生0600001張三30教師計算機基礎教學部

unionDEPT

{

intclass;

charoffice[10];

}unionDEPT

{

intclass;

charoffice[10];

};/*先定義共用體類型*/structperson{charname[10];intage;charjob;

unionDEPTdepa;};共用體類型unionDEPT3#include<stdio.h>structperson{charname[10];intage;charjob;unionDEPT{intclass;charoffice[10];}depa;};voidmain(){structpersonps[4];intn,i;for(i=0;i<4;i++){scanf("%s%d%c",ps[i].name,&ps[i].age,&ps[i].job);if(ps[i].job==‘s’)/*要先判斷job是什么,然后決定存什么*/scanf("%d",&ps[i].depa.class);elseif(ps[i].job=='t')scanf("%s",ps[i].depa.office);}例6-41printf("\nNameAgeJobClass/office\n");for(i=0;i<4;i++){if(ps[i].job==‘s’)/*輸出的時候同樣先判斷,再決定輸出什么*/printf("%-10s%-6d%-3c%-10d\n",ps[i].name,ps[i].age,ps[i].job,ps[i].depa.class);elseprintf("%-10s%-6d%-3c%-10s\n",ps[i].name,ps[i].age,ps[i].job,ps[i].depa.office);}}4共用體類型數(shù)據(jù)的特點unionexample

{ shortx;

charch[4]; }a;a.ch[0]a.ch[1]a.ch[2]a.ch[3]a.x0F150F160F170F18在內(nèi)存中:sizeof(unionxxx)取決于占空間最多的那個成員變量a.x和a.ch處于同樣的地址同一內(nèi)存單元在每一時刻只能存放一個成員的值;5例6-40讀取16位整型數(shù)據(jù)的高字節(jié)數(shù)據(jù)#include<stdio.h>uniondata{shorti;charc[2];};/*這種類型的變量可以看成一個整型變量,也可以看成兩個字符型變量,字符型變量對應的ASCII碼即對應整型數(shù)的高、低字節(jié)*/typedefuniondataDATA;voidmain(){DATAr,*s=&r;s->i=0x3833;

/*換算成二進制為00111000

00110011*/printf("\n");printf("c[0]=%d,c[1]=%d\n",r.c[0],s->c[1]);}0011001100111000r.c[0]r.c[1]r.i0F150F166問題:數(shù)組有何缺點?數(shù)組必須占據(jù)連續(xù)內(nèi)存,在數(shù)組元素的插入或刪除時,費時費力。數(shù)組的長度從定義起就固定不變。如果數(shù)據(jù)元素的個數(shù)不可預知時,就要將數(shù)組定義得足夠大以備不時之需,這就會造成空間的浪費;此外,數(shù)組一經(jīng)定義就占據(jù)內(nèi)存,直至程序結束。7引入鏈表的原因最主要的是插入、刪除操作的靈活性能夠根據(jù)需要靈活申請和釋放內(nèi)存空間。缺點?datanextheaddatanextdataNULLdatanextdatanext8鏈表一種數(shù)據(jù)結構:用順序、不連續(xù)的內(nèi)存空間存儲數(shù)據(jù)。鏈表中每個節(jié)點的數(shù)據(jù)類型(Linkedtable)structLink{intdata;

structLink*next;}datanextheaddatanextdatanextdataNULL9例8-22創(chuàng)建鏈表并存入數(shù)據(jù)算法:循環(huán)執(zhí)行下列操作:1、創(chuàng)建新節(jié)點1、申請一個節(jié)點所用的內(nèi)存;2、向該節(jié)點存入數(shù)據(jù);2、將該節(jié)點鏈入鏈表尾部;structstudent{charnum[10];floatscore;structstudent*next;};/*每個節(jié)點的數(shù)據(jù)類型*/10創(chuàng)建一個新節(jié)點structstudent*CreateNode(){ structstudent*p;

p=(structLink*)malloc(sizeof(structLink));/*動態(tài)申請一段內(nèi)存*/ if(p==NULL)/*申請失敗,打印錯誤信息,退出程序*/ { printf("Noenoughmemorytoalloc"); exit(0); /*結束整個程序的運行*/ }/*為新建節(jié)點賦值*/ p->next=NULL; /*新建的節(jié)點指針域賦空指針*/printf(“pleaseinputnumber:”);/*為新建的節(jié)點數(shù)據(jù)區(qū)賦值*/gets(p->num);printf(“pleaseinputscore:”);scanf(“%d”,&p->score); printf("\nsuccessfulcreateanewnode!");

returnp;}11structstudent*createList(void){structstudent*head=NULL,*pr,p;/*開始時是空鏈表*/intcount=0;charc;printf(“開始建立鏈表,請根據(jù)提示輸入數(shù)據(jù):”);do/*循環(huán)實現(xiàn)建立鏈表*/{printf("\nPleasepress'y'toinsertonenewnode,press'n'tofinish:"); c=getchar(); if((c!='y'||c!='Y')&&(c!='n'||c!='N')) {/*如果鍵入既不是'y',又不是'n'則循環(huán)繼續(xù)進行*/ puts("youmustinput'y'or'n'"); continue; } if((c=='n'||c=='N'))/*如果鍵入的是'n'循環(huán)退出*/ break;p=CreateNode(); if(count==0)/*如果是第一個節(jié)點,將新節(jié)點鏈至頭節(jié)點后*/ {head=p; pr=head;//使用pr跟蹤當前節(jié)點的前一個節(jié)點

} else/*不是第一個節(jié)點,將新建節(jié)點接到鏈表的結尾pr處*/ { pr->next=p; pr=pr->next;//使用pr跟蹤當前節(jié)點的前一個節(jié)點

} count++;}while(1);return(head);

/*返回鏈表的頭結點*/}創(chuàng)建鏈表12輸出創(chuàng)建的鏈表voidPrintList(structstudent*head){ structstudent*p;p=head;if(head==NULL)

/*判斷是否為空鏈表*/{printf("Listisempty!\n");else{while(p!=NULL){printf("%5s%4.1f\n",p->num,p->score);

p=p->next;

/*指向下一個節(jié)點*/}}}13鏈表中插入節(jié)點Head=p;p->next=head;Head=p;p->next=pr

->next;pr

->next=p;pppprp14structstudent*InsertNode(structstudent*head,structstudent*p,inti){structstudent*pr;if(head==NULL){head=p;p->next=NULL;}/*插入到空鏈表*/elseif(i==0){p->next=head;head=p;}/*插入到表頭*/else

/*插入到第i(!=0)個位置*/{pr=head;for(;pr!=NULL&&i>1;pr=pr->next,i--);/*找到第i(!=0)個位置*/if(pr==NULL)

printf("Outoftherange,can’tinsertpnode!\n");else{p->next=pr->next;pr->next=p;}}return(head);}插入節(jié)點15鏈表中刪除節(jié)點(1)刪除第一個結點(2)刪除中間結點head=head->next;pr->next=p->next;ppr16structstudent*DeleteNode(structstudent*head,charnum[]){structstudent*pr;structstudent*p;if(head==NULL){printf("\nListisempty\n");return(head);}if(strcmp(head->num,num)==0)

/*刪除第一個結點*/{p=head;

head=head->next;

free(p);/*不要忘了釋放內(nèi)存*/

}else

/*刪除其它位置上滿足條件的結點*/{pr=head;

p=head->next;while(p!=NULL&&strcmp(p->num,num)!=0){pr=p;p=p->next;}

/*找滿足條件的結點*/if(p!=NULL)

/*找到,刪除*/{pr->next=p->next;

free(p);

/*不要忘了釋放內(nèi)存*/

}else{printf("%5snotbeenfound\n",num);}}return(head);}刪除節(jié)點17鏈表中的要點用于建立更加靈活的動態(tài)數(shù)組,每個數(shù)組元素均是一個節(jié)點。因此,首先應聲明每個節(jié)點的數(shù)據(jù)類型(結構體類型的聲明)無論是哪種操作(建立,插入,刪除),都是指針的重新指向問題,即對指針(head或pr,p指向的節(jié)點的next成員)重新賦值的過程。18小結共用

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論