1、单链表的创建插入删除以及读取记录#include#include#include#includeint n,id;/int fd;FILE *fp;struct student long num; int age; float score; struct student *next;struct student *head;/struct student *f,*sp;struct student *create() struct student *p,*q; n=0,head=NULL; p=(struct student *)malloc(sizeof(struct student); q
2、=(struct student *)malloc(sizeof(struct student); printf(please input the records:n); scanf(%ld %d %f,&q-num,&q-age,&q-score); if(q-num!=0) if(fp=fopen(/home/linux/shiyan/listfile.c,ab+)=NULL) printf(can not open the file.n); return; if(fwrite(q,sizeof(struct student),1,fp)!=1) printf(can not write
3、file!n); printf(ok!n); fclose(fp); else printf(over write records into file.n); while(q-num!=0) n+=1; if(n=1) head=q; else p-next=q; p=q; q=(struct student *)malloc(sizeof(struct student); printf(please input more records:n); scanf(%ld %d %f,&q-num,&q-age,&q-score); if(q-num!=0) if(fp=fopen(/home/li
4、nux/shiyan/listfile.c,ab+)=NULL) printf(can not open the file.n); return; if(fwrite(q,sizeof(struct student),1,fp)!=1) printf(can not write file!n); printf(ok!n); fclose(fp); else printf(over write records into file.n); p-next=NULL; return head;void print() struct student *l; printf(the numbers of l
5、ist are:%dn,n); l=head; if(head!=NULL) do printf(num:%ld age:%d score:%.1fn,l-num,l-age,l-score); l=l-next; while(l!=NULL); else printf(the list is null.n); struct student *insert() struct student *e,*t,*x,*into; t=head; printf(please input the insert record:n); into=(struct student *)malloc(sizeof(
6、struct student); scanf(%ld %d %f,&into-num,&into-age,&into-score); if(into-num!=0) if(fp=fopen(/home/linux/shiyan/listfile.c,ab+)=NULL) printf(can not open the file.n); return; if(fwrite(into,sizeof(struct student),1,fp)!=1) printf(can not write file!n); printf(ok!n); fclose(fp); else printf(over wr
7、ite records into file.n); while(into-num!=0) e=into; if(head=NULL) head=e; e-next=NULL; else while(e-numt-num)&t-next!=NULL) x=t; t=t-next; if(e-numnum) if(t=head) head=e; e-next=t; else x-next=e; e-next=t; else t-next=e; e-next=NULL; n+=1; printf(please input the insert record:n); into=(struct stud
8、ent *)malloc(sizeof(struct student); scanf(%ld %d %f,&into-num,&into-age,&into-score); if(into-num!=0) if(fp=fopen(/home/linux/shiyan/listfile.c,ab+)=NULL) printf(can not open the file.n); return; if(fwrite(into,sizeof(struct student),1,fp)!=1) printf(can not write file!n); printf(ok!n); fclose(fp);
9、 else printf(over write records into file.n); return head;struct student *delete() struct student *s,*c; long num; printf(please input the delete record:n); scanf(%ld,&num); while(num!=0) if(head=NULL) printf(the list is null.n); return head; s=head; while(num!=s-num)&(s-next!=NULL) c=s; s=s-next; i
10、f(num=s-num) if(s=head) head=s-next; free(s); else c-next=s-next; s=s-next; free(s); printf(delete %ld is success.n,num); n-=1; else printf(can not found %ldn,num); printf(please input the delete record:n); scanf(%ld,&num); return head;struct student *refer() struct student *r,*m; long rnum; printf(
11、please input to refer num:n); scanf(%ld,&rnum); r=head; if(rnum!=0) while(m-next!=NULL&r-num!=rnum) m=r; r=m-next; if(m-next=NULL) printf(the record is not exist.n); return head; if(rnum=r-num) printf(the record find:n); printf(num:%ld age:%d score:%.1fn,r-num,r-age,r-score); else printf(the inputer
12、 is error.n); return head;/*struct student *save() FILE *fp; struct student *l1; if(fp=fopen(/home/linux/shiyan/listx,ab+)=NULL) printf(can not open the file.n); return; l1=head; for(id=0;idnum,f1-age,f1-score);/sp=(struct student *)malloc(sizeof(struct student); / f1=f1-next; n+=1; fclose(fp); retu
13、rn f1;/*void load() fd=open(/home/linux/shiyan/list.txt,O_RDONLY); for(id=0;idnum,f-age,f-score); f=f-next; buffer=f; close(fd);*/*void save() fd=open(/home/linux/shiyan/list.txt,O_WRONLY|O_CREAT); for(id=0;idnext; close(fd);*/int choose() int choice; scanf(%d,&choice); return choice;void selecte(in
14、t number) / head=create(); / struct student *head; switch(number) case 1: insert(); break; case 2: delete(); break; case 3: print(); break; case 4: load(); break; case 5: refer(); break; / case 6: / save(); / break; case 6: exit(0); default: printf(the choice is not lawful,please input againn); brea
15、k; void menu() printf(=THE MAIN MENU=n); printf(1:insert a record in list.n); printf(2:delete a record from list.n); printf(3:printf present records on screen.n); printf(4:read the records from listfile.c.n); printf(5:select the record from list.n); printf(6:get away the main menu.n); printf(=n); pr
16、intf(please input a choice:1-2-3-4-5-6n); selecte(choose();/*void save() FILE *fp; if(fp=fopen(list.txt,w)=NULL) printf(can not open file!n); return; for(id=0;idnext; fclose(fp);*/*void load() FILE *fp; if(fp=fopen(list.txt,r=NULL) printf(can not open file!n); return; for(id=0;idnum,head-age,head-sc
17、ore); head=head-next; fclose(fp);*/int main() / printf(please input the records:n); / h=create(); / print(h); /* printf(please input the insert record:n); into=(struct student *)malloc(sizeof(struct student); scanf(%ld %d %f,&into-num,&into-age,&into-score); while(into-num!=0) h=insert(h,into); prin
18、t(h); printf(please input the insert record:n); into=(struct student *)malloc(sizeof(struct student);scanf(%ld %d %f,&into-num,&into-age,&into-score); printf(please input the delete record:n); scanf(%ld,&dnum); while(dnum!=0) h=delete(h,dnum); print(h); printf(please input the delete record:n); scanf(%ld,&dnum); */ head=create(); while(1) menu(); return EXIT_SUCCESS;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1