资源描述:
《c语言链表及其相关操作代码.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、链表动态创建#include#include#includestructStudent{charnum[10];//字符串学号charname[10];//字符串姓名doublescore;//双精度实型成绩structStudent*next;//用与构建链表指向下一结点};structStudent*creatlinklist()//当成绩项目输入0时,创建链表结束{structStudent*head;structStudent*p1,*p2;intn;n=0;head=NULL;p1=(structStudent*)mal
2、loc(sizeof(structStudent));printf("请输入学号姓名和成绩");scanf("%s%s%lf",p1->num,p1->name,&p1->score);while(p1->score!=0){if(n==0){head=p1;p2=p1;n++;}else{p2->next=p1;p2=p1;n++;}p1=(structStudent*)malloc(sizeof(structStudent));printf("请输入学号姓名和成绩");scanf("%s%s%lf",p1->num,p1->name,&p1->score);}if(head!
3、=NULL)p2->next=NULL;returnhead;}voidprint_linklist(structStudent*head){structStudent*pi;if(head==NULL){printf("空链表!");}else{printf("学号t姓名t成绩");for(pi=head;pi!=NULL;pi=pi->next){printf("%st%st%lf",pi->num,pi->name,pi->score);}}}intmain(){structStudent*head;head=creatlinklist();print_link
4、list(head);return0;}/////////////////////////////////////////////////////////////////链表查找#include#include#includestructStudent{charnum[10];//字符串学号charname[10];//字符串姓名doublescore;//双精度实型成绩structStudent*next;//用与构建链表指向下一结点};structStudent*creatlinklist(){structStudent*he
5、ad;structStudent*p1,*p2;intn;n=0;head=NULL;p1=(structStudent*)malloc(sizeof(structStudent));printf("请输入学号姓名和成绩");scanf("%s%s%lf",p1->num,p1->name,&p1->score);while(p1->score!=0){if(n==0){head=p1;p2=p1;n++;}else{p2->next=p1;p2=p1;n++;}p1=(structStudent*)malloc(sizeof(structStudent));printf("请输入学
6、号姓名和成绩");scanf("%s%s%lf",p1->num,p1->name,&p1->score);}if(head!=NULL)p2->next=NULL;returnhead;}voidprintlinklist(structStudent*head){structStudent*pi;if(head==NULL){printf("空链表!");}else{printf("学号t姓名t成绩");for(pi=head;pi!=NULL;pi=pi->next){printf("%st%st%lf",pi->num,pi->name,pi->score)
7、;}}}structStudent*find_linklist_by_name(structStudent*head,charname[]){structStudent*pi;structStudent*pr;pr=NULL;for(pi=head;pi!=NULL;pi=pi->next){if(strcmp(name,pi->name)==0){pr=pi;break;}}returnpr;}voidprint_one_node