收藏 分销(赏)

链表的综合操作源程序.doc

上传人:仙人****88 文档编号:9201298 上传时间:2025-03-16 格式:DOC 页数:4 大小:31.50KB 下载积分:10 金币
下载 相关 举报
链表的综合操作源程序.doc_第1页
第1页 / 共4页
链表的综合操作源程序.doc_第2页
第2页 / 共4页


点击查看更多>>
资源描述
#include<stdio.h> #include<stdlib.h> #include<malloc.h> #define LEN sizeof(struct student) struct student { int num; float score; struct student *next; }; void main() //主函数 { struct student *creat(void);//建立一个动态链表 struct student *del(struct student *head,int del_num);//删除链表 struct student *insert(struct student *head,struct student *stu);//插入链表 void print(struct student *head);//输出链表 struct student *head; struct student *stu; //要插入的节点 int del_num; head=creat(); //创建动态链表 print(head); //输出原链表 printf("\n"); //空一格 printf("Input the delete del_num:"); scanf("%d",&del_num); while(del_num!=0) //多次删除 { head=del(head,del_num); printf("\n"); printf("The deleted list is:\n"); print(head); printf("\n"); printf("Input the delete del_num:"); scanf("%d",&del_num); } printf("\n"); printf("Input the insert node:"); stu=(struct student*)malloc(LEN); scanf("%d,%f",&stu->num,&stu->score); while(stu->num!=0) //多次插入 { head=insert(head,stu); print(head); printf("\n"); printf("Input the insert node:"); stu=(struct student*)malloc(LEN); scanf("%d,%f",&stu->num,&stu->score); } } /***************************************************/ /***************建立动态链表************************/ struct student *creat(void) //建立一个动态链表 { int n; struct student *head; struct student *p1,*p2; n=0; p1=p2=(struct student*)malloc(LEN); scanf("%d,%f",&p1->num,&p1->score); head=NULL; while(p1->num!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct student*)malloc(LEN); scanf("%d,%f",&p1->num,&p1->score); } p2->next=NULL; return(head); } /***************************************************/ /***************删除链表***************************/ struct student *del(struct student *head,int del_num) { struct student *p1,*p2; if(head==NULL) printf("This is a null list!\n"); p1=head; while(del_num!=p1->num&&p1->num!=NULL) { p2=p1; p1=p1->next; } if(del_num==p1->num) { if(p1==head) head=p1->next; else p2->next=p1->next; printf("delete:%d\n",del_num); } else printf("%d not been found!\n",del_num); return(head); } /***************************************************/ /***************插入链表节点************************/ struct student *insert(struct student *head,struct student *stu) { struct student *p0,*p1,*p2; p1=head; p0=stu; if(head==NULL) { head=p0; p0->next=NULL;} else { while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1; p1=p1->next;} if(p0->num<=p1->num) { if(head==p1) head=p0; else p2->next=p0; p0->next=p1;} else {p1->next=p0;p0->next=NULL;} } return(head); } /***************************************************/ /***************输出链表****************************/ void print(struct student *head) { struct student *p; p=head; do { printf("%d%5.0f\n",p->num,p->score); p=p->next; }while(p!=NULL); } 注意:运行时,每输入些组数据后,回车输入“0”,然后回车。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服