




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、#include #include 頭文件struct student( long num;char name12;char age;char sex;定義結構體char addr3O;struct student *next;#defineNULLO#define LEN sizeof(struct student)定義 NULL=0確定結構體的長度struct student *pHead;指針定義(為內存中建立鏈表做準備)void load();void save();void insert();void modify();void delet();void disp();各函數的聲明i
2、nt main(int argc, char* argv)char c;while(l)死循環printf(tlnput selection:ntH); printf(nl: Load Data ;ntH); printf(H2: Insert Record ;nt); printf(3: Modify Record ;ntH); printf(H4: Delete record ;ntH); printf(H5: Display All record ;ntH); printf(6: Save data ;ntH); printffO: Exit.nt);顯示提示信息flushall();c
3、 = getchar();c = c0 x30;switch(c)清空緩存提取指令數字字符轉化為數字執行相應的指令case 0:exit(O);break;case 1:load();break;case 2:insert();break;case 3:modify();break;case 4:delet();break;case 5:disp();break;case 6:save();(return 0;/*死循環條件不同) void load()FILE *fp;struct student *pl=NULL/*p2=NULL;/*p2=0,p2 #定指向 0000if(fp=fope
4、n(,studentData,;lrb,)=NULL) 打開文件若失敗就退出printf(Hcan not open filenH); exit(O);fgetc(fp);while(!feof(fp)從fp所指定的文件中(開頭)取得下一個字符到緩存判斷從文件中取的字符是否為結束符fseek(fp,l,l);/(向前一個字符位)將fp所指定的文件的位置指針以(0:開頭,1:當前位置2結尾)為起點的移動多少個單位 if(pl=(struct student *)malloc(LEN)=NULL)申請一個結構體的空間不成功就退出 return;退回到主函數if(p2=NULL) pHead=pl;
5、(條件只在鏈表頭建立是成立)建立動態鏈表開頭申請下一個(從第二個空間開始)結else p2-next = pl;構體空間與上一個的鏈接p2 = pl;的指針指向自己本身fread(pl, sizeof(struct student),l,fp);位)的數據存到pl所指的內存區fgetc(fp);fclose(fp);void save()(FILE *fp;struct student *pl=pHead;if(fp=fopen(,studentData,/,wb,)=NULL)(printf(ncan not open filenH);exit(O);while( pl!=NULL)fwri
6、te(pl, sizeof(struct student),fp);所指向的文件pl = pl-next;fclose(fp);)指針后移*最后的結構體中從指定文件中讀取指定的長度(1各單從文件中取一下一個字符關閉文件定義pl指向Pheadf地址)打開出錯,退出程序不到鏈表結尾不停止循環把pl所指向的一個結構體的數據輸出到fppl在鏈表中后移關閉文件void insert()(struct student *pO/pl/p2;定義指針if(pO=(struct student *)malloc(LEN)=NULL)/申請內存空間,不成功就退出return;printf(tlnput num:t
7、”);提示輸入相應信息(數據),數組不用取地址scanf(,%d,&pO-num);flushallf);printf(Htlnput name:tH);gets(pO-name);flushallf);printf(tlnput age:tM);scanf(,%d,&pO-age);flushallf);printf(tlnput sex:tH);scanf(c,&pO-sex);flushall();printf(,tlnputaddr:tH);gets(pO-addr);printfCXn);pl=pHead;if(pHead=NULL)pHead=pO;flushall();/pl和P
8、Head 一樣沒初始化,都為0的地址(不可寫) /PHead沒初始化時指OopO-next=NULL;開始組建鏈表else(畫圖(鏈表示意圖)好理解while(pO-numpl-num) & (pl-next!=NULL) 找到 pO 在頭之后的位置(p2后,pl前)*排序:學號從小到大(先于頭比).(當輸入了兩個結構體以后有效) void modify()p2=pl;pl=pl-next;)if(pO-numnum)號小if(pHead=pl) pHead=pO;有一個現成的結構體)中學號還小,使po為頭 else p2-next=pO;pO-next=pl;(else按從小到大排pl-ne
9、xt=pO;pO-next=NULL;)return;)新建的結構體中學號比pl里的學新建的結構體中學號比.頭.(為僅新建的結構體接在p2、pl之間新建的結構體中學號比pl里的學號大,返回到主函數中的死循環中int i;char c;struct student *pl;pl=pHead;printf(HtPlease input the number of the stu. you want to modify:n); scanf(d,&i);while(l)if(pl-num=i)提示信息printf(Mtlnput selection to modify the information
10、of the student.:nt”);printf(nl: modify number;ntM);printf(n2: modify name;ntH);printf(3: modify age;nt);printf(H4: modify sex;ntH);printf(H5: modify address;ntH);printf(H6: modify all information.ntH);flushallf);c = getchar();提取指令c = c-0 x30;數字字符轉化為數字switch(c)執行相應的指令case 1:printf(tlnput num:tH);flus
11、hall();scanf(%d,&pl-num);return;case 2:printf(Htlnput name:tM);flushall();gets(pl-name);flushall();return;case 3:printf(Htlnput age:tu);flushall();scanf(%d,&pl-age);return;case 4:printf(Htlnput sex:tn);flushall();scanf(%c”,&plsex);return;case 5:printf(Htlnput addr:tH);flushall();gets(pl-addr);return
12、;case 6:flushall();printf(Htlnput num:tM);scanf(,%d,&pl-num);flushall();printf(Htlnput name:tH);gets(pl-name);flushall();printf(Htlnput age:tM);scanf(,%d,&pl-age);flushall();printf(Htlnput sex:tH);scanf(%c,&plsex);flushall();printf(Htlnput addr:tH);gets(pl-addr);flushall();return;if (pl-next=NULL)pr
13、intf(Htthere isnt that student.nnn);return;if(pl-num!=i)pl = pl-next;void delet()(int i;struct student *pl,*p2;pl=pHead;printf(HtPlease input the number of the stu. you want to delete:ntH);scanf(%d“,&i);while(l)循環尋找指定的結構體if(pl-num=i)if(pl=pHead)刪除鏈表開頭pHead = pHead-next;elsep2-next = pl-next;刪除非鏈表開頭free(pl);釋放空間break;跳出循環if (pl-next=NULL)printf(Htthere isnt that student.nnH);return;if(pl-num!=i)p2 = pl;pl = pl-next;)void disp()(struct student *pl=pHead;定義結構體指針printf(tnum t
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 無線廣播電視傳輸中的信號傳輸帶寬管理考核試卷
- 船舶改裝工程技術難點與創新解決方案考核試卷
- 病房護理設備的人機交互設計原則與應用考核試卷
- 橡膠制品行業的質量管控與優化考核試卷
- 航班乘客舒適度提升的全方位關懷與滿足考核試卷
- 四川省成都市東辰國際學校2024-2025學年高考押題卷(2)英語試題試卷含解析
- 吉林省長春市外國語校2024-2025學年初三第四次四校聯考語文試題含解析
- 珠海三中高二下學期期中考試(文科)生物試題
- 山西省太原市小店區第一中學2025年高三物理試題二模沖刺試題(六)含解析
- 唐山科技職業技術學院《武術理論與實踐》2023-2024學年第一學期期末試卷
- 2024年重慶市初中學業水平考試地理試卷試題真題(含答案詳解)
- DL-T5153-2014火力發電廠廠用電設計技術規程
- HYT 241-2018 冷卻塔飄水率測試方法 等速取樣法(正式版)
- JTJ-294-1998斜坡碼頭及浮碼頭設計與施工規范
- 急性肺栓塞的應急預案及流程
- 水土保持工程質量評定規程
- 醫用被服洗滌服務方案
- 蓄水池可行性方案
- 政務服務中心物業服務投標方案
- 小兒循環系統解剖生理特點
- 青島海爾納稅籌劃課程設計
評論
0/150
提交評論