结构体与链表编程题及解答

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1结构体与共用体【程序1】题目:编写input()和output()函数输入,输出5个学生的数据记录。(用结构体设计,学生记录中包括学号、姓名、四门课程成绩)程序源代码:#includestdio.h#defineN5structstudent{charnum[6];charname[8];intscore[4];}stu[N];voidinput(structstudentstu[]);voidprint(structstudentstu[]);voidmain(){input(stu);print(stu);}voidinput(structstudentstu[]){inti,j;for(i=0;iN;i++){printf(\npleaseinput%dof%d\n,i+1,N);printf(num:);scanf(%s,stu[i].num);printf(name:);scanf(%s,stu[i].name);for(j=0;j3;j++){printf(score%d.,j+1);scanf(%d,&stu[i].score[j]);}printf(\n);}}2voidprint(structstudentstu[]){inti,j;printf(\nNo.NameScore1Score2Score3\n);for(i=0;iN;i++){printf(%-5s%-9s,stu[i].num,stu[i].name);for(j=0;j3;j++)printf(%-8d,stu[i].score[j]);printf(\n);}}【程序2】建立100名学生的信息表,每个学生的数据包括学号、姓名、及一门课的成绩,要求从键盘输入这100名学生的信息,并按照每一行显示一名学生信息的格式将他们的信息显示出来。#includestdio.h#defineN3structstudent{intnum;charname[20];intscore;};voidmain(){inti;structstudents[N];/*定义结构体类型数组,长度100*/for(i=0;iN;i++){printf(inputnumber:);scanf(%d,&s[i].num);printf(inputname:);scanf(%s,s[i].name);printf(inputscore:);scanf(%d,&s[i].score);}printf(Number:Name:score:\n);for(i=0;iN;i++)printf(%-8d%-10s%d\n,s[i].num,s[i].name,s[i].score);}【程序3】题目:创建一个链表。程序源代码:/*creatalist*/#includestdlib.h#includestdio.h3#defineN3structlist{intdata;structlist*next;};typedefstructlistnode;typedefnode*link;voidmain(){linkptr,head;intnum,i;intj;printf(pleaseinputnumbers==);scanf(%d,&j);ptr=(link)malloc(sizeof(node));head=ptr;printf(pleaseenternumbers:);for(i=0;i=j-1;i++){scanf(%d,&num);ptr-data=num;ptr-next=(link)malloc(sizeof(node));if(i==j-1)ptr-next=NULL;elseptr=ptr-next;}ptr=head;while(ptr!=NULL){printf(Thevalueis==%d\n,ptr-data);ptr=ptr-next;}}==============================================================【程序4】题目:反向输出一个链表。1.程序分析:2.程序源代码:/*reverseoutputalist*/#includestdlib.h#includestdio.hstructlist4{intdata;structlist*next;};typedefstructlistnode;typedefnode*link;voidmain(){linkptr,head,tail;intnum,i;intj;printf(pleaseinputnumbers==);scanf(%d,&j);tail=(link)malloc(sizeof(node));tail-next=NULL;ptr=tail;printf(\npleaseinputdata==);for(i=0;i=j-1;i++){scanf(%d,&num);ptr-data=num;head=(link)malloc(sizeof(node));head-next=ptr;ptr=head;}ptr=ptr-next;while(ptr!=NULL){printf(Thevalueis==%d\n,ptr-data);ptr=ptr-next;}}【程序5】已有a,b两个链表,每个链表中的的结点包括学号、成绩。要求把两个链表合并,按学号升序排列#includestdio.h#includestdlib.htypedefstructstudent{intnum;floatscore;structstudent*next;}STU;STU*create()5{STU*p,*head=NULL,*tail=head;while(1){p=(STU*)malloc(sizeof(STU));scanf(%d%f,&p-num,&p-score);p-next=NULL;if(p-num0){free(p);break;}if(head==NULL)head=p;elsetail-next=p;tail=p;}returnhead;}voidoutput(STU*p){while(p!=NULL){printf(%d\t%.2f\n,p-num,p-score);p=p-next;}}STU*link(STU*p1,STU*p2){STU*p,*head;if(p1-num=p2-num){head=p=p1;p1=p1-next;}else{head=p=p2;p2=p2-next;}while(p1!=NULL&&p2!=NULL){if(p1-num=p2-num)6{p-next=p1;p=p1;p1=p1-next;}else{p-next=p2;p=p2;p2=p2-next;}}if(p1!=NULL)p-next=p1;elsep-next=p2;returnhead;}intmain(intargc,char*argv[]){STU*a,*b,*c;printf(\n请输入链表a的信息,学号小于零时结束输入:格式(学号成绩)\n);a=create();printf(\n请输入链表b的信息,学号小于零时结束输入:格式(学号成绩)\n);b=create();printf(\n链表a的信息为:\n);output(a);printf(\n链表b的信息为:\n);output(b);c=link(a,b);printf(\n合并后的链表信息为:\n);output(c);return0;}【程序6】13个人转成一圈,从第一个人开始报数,报到3的退出,问最后退出的人原来的序号。(用链表实现)#includestdio.h#includemalloc.h#defineLENsizeof(structstudent)structstudent7{intnum;structstudent*next;};intmain(){inti;structstudent*head,*p1,*p2;//p1指前,p2指后head=p1=p2=(structstudent*)malloc(LEN);p2-num=1;for(i=2;i13;i++){p1=(structstudent*)malloc(LEN);p1-num=i;p2-next=p1;p2=p1;}p1=(structstudent*)malloc(LEN);p1-num=13;p1-next=head;p2-next=p1;//建立链表完成p2=p1;p1=head;while(p1-next!=p1){for(i=0;i2;i++){p1=p1-next;p2=p2-next;}printf(No.%2dhasbeendeleted!\n,p1-num);p2-next=p1-next;p1=p1-next;}printf(\nThelastoneisNo.%d.\n,p1-num);return1;}【程序7】有两个链表a和b,设结点中包含学号、姓名。从a链表中删去与b链表中有相同学号的那些结点。#includestdio.h#includestdlib.htypedefstructstudent{intnum;8floatscore;structstudent*next;}STU;STU*create(){inti;STU*p,*head=NULL,*tail=head;while(1){p=(STU*)malloc(sizeof(STU));scanf(%d%f,&p-num,&p-score);p-next=NULL;if(p-num0){free(p);break;}if(head==NULL)head=p;elsetail-next=p;tail=p;}returnhead;}voidoutput(STU*p){while(p!=NULL){printf(%d\t%.2f\n,p-num,p-score);p=p-next;}}STU*del(STU*a,STU*b){STU*head,*p1,*p2;p1=p2=head=a;//让p1、p2、head结点指向链表a的头部while(b!=NULL){p1=p2=head;//每次循环前让p1、p2始终指向删除后链表的头部while(p1!=NULL){if(b-num==p1-num)//学号相同,删除结点信息9if(p1==head)//如果删除的是头结点,则头结点位置要后移{head=p1-next;free(p1);p1=p2=head;}else//如果删除的是中间结点{p2-next=p1-next;free(p1);p1=p2-next;}else//学号不同,则p1,p2指针依次后移{p2=p1;p1=p1-next;}}b=b-next;}returnhead;}intmain(intargc,char*argv[]){STU*a,*b,*c;printf(\n请输入链表a的信息,学号小于零时结束输入:格式(学号成绩)\n);a=create();printf(\n请输入链表b的信息,学号小于零时结束输入:格式(学号成绩)\n);b=create();system(cls);printf(\n链表a的信息为:\n);output(a);printf(\n链表b的信息为:\n);output(b);c=del(a,b);printf(\n删除后的链表信息为:\n);output(c);return0;}【程序8】建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。#includestdio.h#includestdlib.h

1 / 11
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功