[2017年整理]结构体与链表编程题及解答

上传人:豆浆 文档编号:914646 上传时间:2017-05-21 格式:DOC 页数:11 大小:86KB
返回 下载 相关 举报
[2017年整理]结构体与链表编程题及解答_第1页
第1页 / 共11页
[2017年整理]结构体与链表编程题及解答_第2页
第2页 / 共11页
[2017年整理]结构体与链表编程题及解答_第3页
第3页 / 共11页
[2017年整理]结构体与链表编程题及解答_第4页
第4页 / 共11页
[2017年整理]结构体与链表编程题及解答_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《[2017年整理]结构体与链表编程题及解答》由会员分享,可在线阅读,更多相关《[2017年整理]结构体与链表编程题及解答(11页珍藏版)》请在金锄头文库上搜索。

1、1结构体与共用体【程序 1】题目:编写 input()和 output()函数输入,输出 5 个学生的数据记录。 (用结构体设计,学生记录中包括学号、姓名、四门课程成绩)程序源代码:#include #define N 5struct student char num6;char name8;int score4; stuN;void input(struct student stu);void print(struct student stu);void main()input(stu);print(stu);void input(struct student stu) int i,j;fo

2、r(i=0;i#define N 3struct student int num;char name20;int score;void main()int i;struct student sN; /*定义结构体类型数组,长度 100*/for(i=0;i3#define N 3struct list int data;struct list *next;typedef struct list node;typedef node *link;void main() link ptr,head;int num,i;int j;printf(please input numbers=);scanf

3、(%d,&j);ptr=(link)malloc(sizeof(node);head=ptr;printf(please enter numbers:);for(i=0;idata=num;ptr-next=(link) malloc(sizeof(node);if(i=j-1) ptr-next=NULL;elseptr=ptr-next;ptr=head;while(ptr!=NULL) printf(The value is =%dn,ptr-data);ptr=ptr-next;=【程序 4】题目:反向输出一个链表。1.程序分析:2.程序源代码:/*reverse output a l

4、ist*/#include stdlib.h#include struct list4 int data;struct list *next;typedef struct list node;typedef node *link;void main()link ptr,head,tail;int num,i;int j;printf(please input numbers=);scanf(%d,&j);tail=(link)malloc(sizeof(node);tail-next=NULL;ptr=tail;printf(nplease input data=);for(i=0;idata

5、=num;head=(link)malloc(sizeof(node);head-next=ptr;ptr=head;ptr=ptr-next;while(ptr!=NULL) printf(The value is =%dn,ptr-data);ptr=ptr-next;【程序 5】已有 a,b 两个链表,每个链表中的的结点包括学号、成绩。要求把两个链表合并,按学号升序排列#include #include typedef struct studentint num;float score;struct student *next;STU;STU *create()5 STU *p,*hea

6、d=NULL,*tail=head;while (1)p=(STU *)malloc(sizeof(STU);scanf(%d%f,&p-num,&p-score);p-next=NULL;if (p-numnext=p;tail=p;return head;void output(STU *p)while (p!=NULL)printf(%dt%.2fn,p-num,p-score);p=p-next;STU *link(STU *p1,STU *p2)STU *p,*head;if (p1-numnum)head=p=p1;p1=p1-next;elsehead=p=p2;p2=p2-ne

7、xt;while (p1!=NULL&p2!=NULL)if (p1-numnum)6p-next=p1;p=p1;p1=p1-next;elsep-next=p2;p=p2;p2=p2-next;if(p1!=NULL)p-next=p1;elsep-next=p2;return head;int main(int argc, char *argv) STU *a,*b,*c;printf(n 请输入链表 a 的信息,学号小于零时结束输入:格式(学号 成绩)n);a=create();printf(n 请输入链表 b 的信息,学号小于零时结束输入:格式(学号 成绩)n);b=create()

8、;printf(n 链表 a 的信息为:n );output(a);printf(n 链表 b 的信息为:n );output(b);c=link(a,b);printf(n 合并后的链表信息为:n );output(c);return 0;【程序 6】13 个人转成一圈,从第一个人开始报数,报到 3 的退出,问最后退出的人原来的序号。 (用链表实现)#include #include #define LEN sizeof(struct student)struct student7int num;struct student *next;int main()int i;struct stud

9、ent *head,*p1,*p2;/p1 指前,p2 指后head=p1=p2=(struct student *)malloc(LEN);p2-num=1;for(i=2;inum=i;p2-next=p1;p2=p1;p1=(struct student *)malloc(LEN);p1-num=13;p1-next=head;p2-next=p1;/建立链表完成p2=p1;p1=head;while(p1-next!=p1)for(i=0;inext;p2=p2-next;printf(No.%2d has been deleted!n,p1-num);p2-next=p1-next;

10、p1=p1-next;printf(nThe last one is No.%d.n,p1-num);return 1;【程序 7】有两个链表 a 和 b,设结点中包含学号、姓名。从 a 链表中删去与 b 链表中有相同学号的那些结点。#include #include typedef struct studentint num;8float score;struct student *next;STU;STU *create()int i;STU *p,*head=NULL,*tail=head;while (1)p=(STU *)malloc(sizeof(STU);scanf(%d%f,&

11、p-num,&p-score);p-next=NULL;if (p-numnext=p;tail=p;return head;void output(STU *p)while (p!=NULL)printf(%dt%.2fn,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

12、-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;return head;int main(int argc, char *argv) STU *a,*b,*c;printf(n 请输入链表 a 的信息,学号小于零时结束输入:格式(学号 成绩)n);a=create();pr

13、intf(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);return 0;【程序 8】建立一个链表,每个结点包括:学号、姓名、性别、年龄。输入一个年龄,如果链表中的结点所包含的年龄等于此年龄,则将此结点删去。#include #include10#define LEN sizeof(struct stud

14、ent) struct student char num6; char name8; char sex6; int age; struct student *next; stu10; void main() struct student *p,*pt,*head; int i,length,iage,flag=1; int find=0; /* 若找到待删除元素,find=1 ,否则 find=0 */while(flag=1) printf(input length of list(next=p; pt=p; printf(NO:); scanf(%s,p-num); printf(name:); scanf(%s,p-name); printf(sex:); scanf(%s,p-sex); printf(age:); scanf(%d,&p-age); p-next=NULL; p=head; printf(n NO. name sex agen); while(p!=NULL) printf(%4s%8s%6s%6dn,p-num, p-name, p-sex, p-age); p=p-next; 11 printf(input age:); scanf(

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号