




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
#include<stdio.h>#include<stdlib.h>/*intdays(inty,intm,intd)//計算天數{ intdays=0,i; inta[12]={31,28,31,30,31,30,31,31,30,31,30,31}; if(y%4==0&&y%100!=0||y%400==0)//判斷是否為閏年 a[1]+=1; if(m==1) returndays; else { for(i=0;i<m-1;i++) days+=a[i]; days+=d; returndays; }}structdate{ intyear; intmonth; intday; intdays;}a;intmain(){ printf("enterdate:"); scanf("%d%d%d",&a.year,&a.month,&a.day); a.days=days(a.year,a.month,a.day); printf("%d年%d月%d日是該年的第%d天\n",a.year,a.month,a.day,a.days);}*//*#defineN10//第3、4題時N為5,第5題時N為10structstudent{ intnum; charname[20]; floatscore[3]; floatave;//第3、4、5題共用一個結構體類型}stu[N];*//*voidprint(structstudenta[]){ inti; printf("學號姓名\t三門課成績\n"); for(i=0;i<N;i++) printf("%ld%s\t%-5.1f%-5.1f%-5.1f\n",a[i].num,a[i].name,a[i].score[0],a[i].score[1],a[i].score[2]);}intmain(){ inti; printf("請輸入%d個學生的信息:學號、姓名、三門課成績:\n",N); for(i=0;i<N;i++) scanf("%d%s%f%f%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]); print(stu);}*//*voidprint(structstudenta[]){ inti; printf("學號姓名\t三門課成績\n"); for(i=0;i<N;i++) printf("%ld%s\t%-5.1f%-5.1f%-5.1f\n",a[i].num,a[i].name,a[i].score[0],a[i].score[1],a[i].score[2]);}voidinput(structstudenta[])//在上一題的基礎上編寫input函數{ inti; printf("請輸入%d個學生的信息:學號、姓名、三門課成績:\n",N); for(i=0;i<N;i++) scanf("%d%s%f%f%f",&stu[i].num,&stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);}intmain(){ input(stu); print(stu);{ structstudent*head; structstudent*p1,*p2; n=0; p1=p2=malloc(L); scanf("%ld,%f",&p1->num,&p1->score); head=NULL; while(p1->num!=0) { n+=1; if(n==1)head=p1; elsep2->next=p1; p2=p1; p1=malloc(L); scanf("%ld,%f",&p1->num,&p1->score); } p2->next=NULL; returnhead;}voidprint(structstudent*head)//輸出鏈表的函數{ structstudent*p=head; printf("\nnow,theserecordsare:\n"); while(p!=NULL) { printf("%ld%5.1f\n",p->num,p->score); p=p->next; }}structstudent*del1(structstudent*head,longnum)//刪除指定節點的函數,方法一,指定刪除節點的數據{ structstudent*p1,*p2; p1=head; if(p1->num==num) returnhead=p1->next; else { while(p1->num!=num) { p2=p1; p1=p1->next; } p2->next=p1->next; p1->next=NULL; returnhead; }}structstudent*del2(structstudent*head,intn)//方法二,指定刪除節點序號{ structstudent*p1,*p2; inti=1; p1=head; if(i==n) returnhead=p1->next; else { while(i++<n) { p2=p1; p1=p1->next; } p2->next=p1->next; p1->next=NULL; returnhead; }}intmain(){ structstudent*head; longnum;//intn; head=creat(); print(head); printf("\n輸入要刪除學生的學號:");//printf("\n輸入要刪除節點序號:"); scanf("%ld",&num);//scanf("%d",&n); head=del1(head,num);//head=del2(head,n); print(head);}*//*#defineLsizeof(structstudent)structstudent{ longnum; floatscore; structstudent*next;};intn;structstudent*creat(void)//生成單向動態鏈表的函數{ structstudent*head; structstudent*p1,*p2; n=0; p1=p2=malloc(L); scanf("%ld,%f",&p1->num,&p1->score); head=NULL; while(p1->num!=0) { n+=1; if(n==1)head=p1; elsep2->next=p1; p2=p1; p1=malloc(L); scanf("%ld,%f",&p1->num,&p1->score); } p2->next=NULL; returnhead;}voidprint(structstudent*head)//輸出鏈表的函數{ structstudent*p=head; printf("\nnow,theserecordsare:\n"); while(p!=NULL) { printf("%ld%5.1f\n",p->num,p->score); p=p->next; }}structstudent*del1(structstudent*head,longnum)//刪除指定節點的函數,num為指定刪除節點的數據{ structstudent*p1,*p2; p1=head; if(p1->num==num) returnhead=p1->next; else { while(p1->num!=num) { p2=p1; p1=p1->next; } p2->next=p1->next; p1->next=NULL; returnhead; }}structstudent*insert(structstudent*head,structstudent*p,intn)//插入節點的函數,n為新節點序號{ structstudent*p1,*p2; inti=1; p1=head; if(i==n) { p->next=p1; head=p; } else { while(i++<n) { p2=p1; p1=p1->next; } p2->next=p; p->next=p1; } returnhead;}intmain(){ structstudent*head,*p; intn; longnum; head=creat(); print(head); printf("\n輸入要刪除學生的學號:"); scanf("%d",&num); head=del(head,num); print(head); p=malloc(L);//為插入的新節點開辟單元,否則p的值不確定 printf("\n輸入要添加學生的學號,成績,序號:"); scanf("%ld,%f,%d",&p->num,&p->score,&n); head=insert(head,p,n); print(head);}*//*#defineLsizeof(structstudent)structstudent{ intnum; floatscore; structstudent*next;};intn;structstudent*creat(void)//生成單向動態鏈表的函數{ structstudent*head; structstudent*p1,*p2; n=0; p1=p2=malloc(L); scanf("%d%f",&p1->num,&p1->score); head=NULL; while(p1->num!=0) { n+=1; if(n==1)head=p1; elsep2->next=p1; p2=p1; p1=malloc(L); scanf("%d%f",&p1->num,&p1->score); } p2->next=NULL; returnhead;}voidprint(structstudent*head)//輸出鏈表的函數{ structstudent*p=head; printf("\nnow,theserecordsare:\n"); while(p!=NULL) { printf("%d%5.1f\n",p->num,p->score); p=p->next; }}structstudent*sort(structstudent*head)//建立鏈表排序函數{ structstudent*p1,*p2; intt; floats; p1=head; p2=p1->next; while(p1&&p2)//不能寫為p1,否則當p1指向最后一個結點時,最后一句循環語句出問題 { while(p2) { if(p1->num>p2->num) { t=p1->num; s=p1->score; p1->num=p2->num; p1->score=p2->score; p2->num=t; p2->score=s; p2=p2->next; } else//不能省略else語句,否則遇到p1->num<p2->num時無限循環 p2=p2->next; } p1=p1->next; p2=p1->next; } returnhead;}structstudent*cat(structstudent*head1,structstudent*head2)//建立鏈表合并函數{ structstudent*p,*t; p=head1; while(p) { t=p;//循環結束時t將指向a鏈表的最后一個指針 p=p->next; } t->next=head2; returnhead1;}intmain(){ structstudent*a,*b; printf("輸入鏈表a:\n"); a=creat(); printf("輸入鏈表b:\n"); b=creat(); print(sort(cat(a,b)));}*//*測試數據100188910016641001481100129400100176710015681001376100118500*//*#defineLsizeof(structstudent)structstudent{ longnum; charname[20]; structstudent*next;};intn;structstudent*creat(void)//生成單向動態鏈表的函數{ structstudent*head; structstudent*p1,*p2; n=0; p1=p2=malloc(L); scanf("%ld%s",&p1->num,p1->name); head=NULL; while(p1->num!=0) { n+=1; if(n==1)head=p1; elsep2->next=p1; p2=p1; p1=malloc(L); scanf("%ld%s",&p1->num,p1->name); } p2->next=NULL; returnhead;}voidprint(structstudent*head)//輸出鏈表的函數{ structstudent*p=head; printf("\n現在鏈表a為:\n"); while(p!=NULL) { printf("%ld%s\n",p->num,p->name); p=p->next; }}structstudent*delsame(structstudent*a,structstudent*b)//從a中刪去與b相同學號的節點{ structstudent*p1,*p2,*p3; longnum; ints,k=0; p3=p1=a;p2=b; while(p1) { s=0; num=p1->num; while(p2) { if(p2->num!=num) p2=p2->next; else { s=1; break; } } if(s==1) p3->next=p1->next;//若a最后一個相同,p3的指針數據為null else { p3=p1;//每找到一個與b不相等的指針時,p3指向它 k+=1;//每找到一個與b不相等的指針時,k累加1 if(k==1)//找到第一個與b不相等的指針時,將頭指針賦值給a a=p1; } p1=p1->next;//p1指向下一個指針 p2=b;//p2重新指向鏈表b開頭 } if(k==0)//k=0表明a,b鏈表相同,返回null returna=NULL; else returna;}intmain(){ structstudent*a,*b; printf("輸入鏈表a:\n"); a=creat(); printf("輸入鏈表b:\n"); b=creat(); a=delsame(a,b); print(a);}*//*#defineLsizeof(structinf)structinf{ longnum; intage; charname[20]; charsex; structinf*next;};intn;structinf*creat(void)//生成單向動態鏈表的函數{ structinf*head; structinf*p1,*p2; n=0; p1=p2=malloc(L); scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age); head=NULL; while(p1->num!=0) { n+=1; if(n==1)head=p1; elsep2->next=p1; p2=p1; p1=malloc(L); scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 山東省棗莊市現代實驗學校2024-2025學年高考原創信息試卷物理試題(三)含解析
- 山西省忻州市岢嵐縣中學2025屆高三下學期教學質量調研考試(二模)歷史試題試卷含解析
- 四川托普信息技術職業學院《NoSQ數據庫原理》2023-2024學年第二學期期末試卷
- 上海外國語大秀洲外國語校2024-2025學年初三4月階段性測試語文試題含解析
- 西北師范大學《地方文化研究》2023-2024學年第二學期期末試卷
- 武漢科技大學《印度文化遺產賞析》2023-2024學年第二學期期末試卷
- 上海師范大學《中國古代文學I》2023-2024學年第一學期期末試卷
- 江蘇省高郵市陽光雙語初中重點名校2025年初三最后一考生物試題試卷含解析
- 西安高新科技職業學院《外國史學史》2023-2024學年第一學期期末試卷
- 江蘇省蘇州市東山中學2025年初三5月月考英語試題理試題含答案
- 變電站通信中斷處理經驗分享
- 光伏治沙的方案
- 農藝師考試試題及答案(種植業卷)
- 醫療器械(耗材)項目投標服務投標方案(技術方案)
- 鄉村醫生從業管理條例全面解讀
- DB4409-T 38-2023 奇楠沉香栽培技術規程
- 光伏工程光伏場區箱式變壓器安裝方案
- 進度款支付報告
- 上市公司財務質量綜合評價-以伊利公司為例
- 原發性月經稀少的護理
- 中建項目機電工程系統聯動調試方案
評論
0/150
提交評論