资源描述:
《c程序设计2-第五讲-链表.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、假定一台电脑由显示器、CPU、内存、硬盘和机箱5个部件组成,每个部件包括牌子、型号和价格3个属性,从一个文本文件中读取一台电脑的5个部件的信息,然后算出该电脑的总价格,并把5个部件的属性和总价格输出另一个文件中。1structBuJian{charPinPai;charXingHao;intJiaGe;};2main(){structBuJianxsq,yp,cpu,nc,jx;intZongJiaGe;FILE*fp;fp=fopen("e:\a.txt","r");fscanf(fp,"%s%s%d"
2、,xsq.PinPai,xsq.XingHao,&xsq.JiaGe);fscanf(fp,"%s%s%d",yp.PinPai,yp.XingHao,&yp.JiaGe);fscanf(fp,"%s%s%d",cpu.PinPai,cpu.XingHao,&cpu.JiaGe);fscanf(fp,"%s%s%d",nc.PinPai,nc.XingHao,&nc.JiaGe);fscanf(fp,"%s%s%d",jx.PinPai,jx.XingHao,&jx.JiaGe);fclose(fp);3Z
3、ongJiaGe=xsq.JiaGe+yp.JiaGe+cpu.JiaGe+nc.JiaGe+jx.JiaGe;fp=fopen("e:\b.txt","w");fprint(fp,"%s%s%d",xsq.PinPai,xsq.XingHao,xsq.JiaGe);fprint(fp,"%s%s%d",yp.PinPai,yp.XingHao,yp.JiaGe);fprint(fp,"%s%s%d",cpu.PinPai,cpu.XingHao,cpu.JiaGe);fprint(fp,"%
4、s%s%d",nc.PinPai,nc.XingHao,nc.JiaGe);fprint(fp,"%s%s%d",jx.PinPai,jx.XingHao,jx.JiaGe);fprint(fp,"%d",ZongJiaGe);fclose(fp);}4第二题5structBuJian{charPinPai;charXingHao;intJiaGe;};6structDianNao{BuJianxsq;BuJianyp;BuJiancpu;BuJiannc;BuJianjx;};7main(){
5、structDianNaodn[3];inti;FILE*fp;fp=fopen("e:\a.txt","r");for(i=0;i<3;i++){fscanf(fp,"%s%s%d",dn[i].xsq.PinPai,dn[i].xsq.XingHao,&dn[i].xsq.JiaGe);fscanf(fp,"%s%s%d",dn[i].yp.PinPai,dn[i].yp.XingHao,&dn[i].yp.JiaGe);fscanf(fp,"%s%s%d",dn[i].cpu.PinPai,dn[i
6、].cpu.XingHao,&dn[i].cpu.JiaGe);fscanf(fp,"%s%s%d",dn[i].nc.PinPai,dn[i].nc.XingHao,&dn[i].nc.JiaGe);fscanf(fp,"%s%s%d",dn[i].jx.PinPai,dn[i].jx.XingHao,&dn[i].jx.JiaGe);}fclose(fp);8第五讲链表97QianSunLiZhouWuWangnullHead7131432537链表结构(1)头指针变量head──指向链表的首结点。(2
7、)每个结点由2个域组成:1)数据域──存储结点本身的信息。2)指针域──指向后继结点的指针。(3)尾结点的指针域置为“NULL(空)”,作为链表结束的标志10链表结构的定义structstudent{charname[10];structstudent*next;};next为student类型指针变量,指向下一个结点的指针域。结点的变量或指针变量的定义:structstudentnode,*head;node可以存放一个学生结点指针head可以存放学生结点的地址。11例:建立一个简单链表,它由3个学生数据
8、的节点组成。输出各节点中的数据。#defineNULL0structstudent{longnum;floatscore;structstudent*next;};main(){structstudenta,b,c,*head,*p;a.num=10101;a.score=89.5;b.num=10103;b.score=90;c.num=10107;c.score=85;head=&a;a.next=&b;b.