开放实验答案结构体与链表.docx
- 文档编号:24687575
- 上传时间:2023-05-31
- 格式:DOCX
- 页数:16
- 大小:50.94KB
开放实验答案结构体与链表.docx
《开放实验答案结构体与链表.docx》由会员分享,可在线阅读,更多相关《开放实验答案结构体与链表.docx(16页珍藏版)》请在冰豆网上搜索。
开放实验答案结构体与链表
结构体与链表
实验1结构体变量与结构体数组编程
参考程序:
2、
(1)charname[]中的数组name没有指定大小,此处可以定义为name[20]
(2)p=stu也有错误,应该将结构体的首地址赋给指针p,从而使p指向该结构体。
正确的写法是:
p=&stu;
(3)(*p).name="Mary"也有错误,此处不能这么赋值。
正确的写法是:
strcpy(p->name,"Mary")
(4)scanf("%f",p->score)也有错误,此处p->score前面应加上取地址符&,正确写法是:
scanf("%f",&p->score)
(5)printf(“%6d%10s%1.2f\n”,p.num,p->name,p->score);也有错误。
完整的程序是:
#include
#include
intmain()
{
structstudent
{intnum;
charname[20];
floatscore;
}stu,*p;
p=&stu;
stu.num=1001;
strcpy((*p).name,"Mary");
scanf("%f",&p->score);
printf("%6d%10s%6.2f\n",(*p).num,p->name,p->score);
return0;
}
/**********************************************************************
3.有10本图书,每本图书的信息包括书号、书名、作者、价格,编写函数完成以下功能:
(1)从键盘输入数据,将其存放在结构体数组中;
(2)输入书名,在数组中查找是否存在此书,有此书则输出此书的信息,无此书则输出提示信息;
(3)输入一个价格,将高于此价格的图书信息输出。
**********************************************************************/
#include
#include
#include
#defineFORMAT"%d%s%s%4.2f\n"
#defineN2
structbook{
intnum;//书号
charname[20];//书名
charauthor[20];//作者
floatprice;//书的价格
};
//定义一个有10个元素的结构体数组,并将其初始化为0
structbookbook[N]={0};
//此函数的作用是输入书名在数组中查找是否存在此书
voidbook_name(char*name)
{
inti;
for(i=0;i { while(strcmp(book[i].name,name)==0) { printf(FORMAT,book[i].num,book[i].name,book[i].author,book[i].price); break; } } } //此函数的作用是: 输入一个价格,将高于此价格的图书信息输出 voidbook_price(floatprice) { inti; for(i=0;i { while(book[i].price>price) { printf(FORMAT,book[i].num,book[i].name,book[i].author,book[i].price); break; } } } //在主函数中实现从键盘输入数据,将其存放在结构体数组Á中 intmain(void) { inti; printf("书号书名作者价格\n"); for(i=0;i scanf("%d%s%s%f",&book[i].num,&book[i].name,&book[i].author,&book[i].price); floatb_price; charname[20]; printf("请输入你查找的书名: "); scanf("%s",name); printf("----------------------------\n"); book_name(name); printf("----------------------------\n"); printf("请输入你指定的书的价格: "); scanf("%f",&b_price); printf("----------------------------\n"); book_price(b_price); printf("----------------------------\n"); return0; } 实验2链表基本操作编程 参考程序: /********************************************************************** 2.编写函数用表首添加法建立链表,在main函数中调用该函数并输出链表,结点信息包括学号、姓名、年龄。 **********************************************************************/ #include"stdio.h" #include"stdlib.h" structs { floatnum; charname[12]; intage; structs*next; }; #defineLENsizeof(structs) structs*creat(void) { structs*p1,*p2; p1=p2=(structs*)malloc(LEN); scanf("%f%s%d",&p1->num,&p1->name,&p1->age); if(p1->num! =0) p2->next=NULL; else p2=NULL; while(p1->num! =0) { p2=p1; p1=(structs*)malloc(LEN); scanf("%f%s%d",&p1->num,p1->name,&p1->age); p1->next=p2; } free(p1); return(p2); } voidlist(structs*head) { structs*q; if(head==NULL) printf("链表信息为空: \n"); else { q=head; printf("新建的链表是: \n"); while(q! =NULL) { printf("%.f%s%d\n",q->num,q->name,q->age); q=q->next; } } } voidmain() { structs*a; printf("创建链表: \n"); a=creat(); list(a); } /********************************************************************** 3.编程实现两个链表的链接,即令链表a的最后一个结点指向链表b的第1个结点,如图2.1所示: **********************************************************************/ #include"stdio.h" #include"stdlib.h" structs { floatnum; charname[12]; intage; structs*next; }; #defineLENsizeof(structs) structs*creat(void) { structs*p1,*p2; p1=p2=(structs*)malloc(LEN); scanf("%f%s%d",&p1->num,&p1->name,&p1->age); if(p1->num! =0) p2->next=NULL; else p2=NULL; while(p1->num! =0) { p2=p1; p1=(structs*)malloc(LEN); scanf("%f%s%d",&p1->num,p1->name,&p1->age); p1->next=p2; } free(p1); return(p2); } structs*zuhe(structs*m,structs*n) { structs*p=m,*head; head=p; while(p->next! =NULL) p=p->next; p->next=n; returnhead; } voidlist(structs*head) { structs*q; q=head; while(q! =NULL) { printf("%.f%s%d\n",q->num,q->name,q->age); q=q->next; } } voidmain() { structs*a,*b,*c; printf("创建链表a: \n"); a=creat(); printf("创建链表b: \n"); b=creat(); printf("连接后的链表是: \n"); c=zuhe(a,b); list(c); } 实验3链表复杂应用编程 一.实验目的与要求 1.熟练使用结点指针变量。 2.能够建立一些其它形式的链表。 二.实验题目 /********************************************************************** 1.编程实现建立一个带有头结点的单链表,头结点的数据成员存放了该链表中的结点个数,如图2.2所示: 图2.2带头结点的单链表 **********************************************************************/ #include #include #include typedefstructNode { intdata; structNode*pNext; }NODE,*PNODE; PNODEcreate_list(void);//创建一个链表 voidtraverse_list(PNODEpHead);//遍历链表 intmain(void) { PNODEpHead=NULL; pHead=create_list(); printf("遍历链表: \n"); traverse_list(pHead); printf("链表的长度是pHead->data=%d\n",pHead->data); return0; } PNODEcreate_list(void) { intlen;//链表中节点数目 inti; intval; printf("请输入要创建的链表中节点的个数len="); scanf("%d",&len); PNODEpHead=(PNODE)malloc(sizeof(NODE)); pHead->data=len; if(NULL==pHead) { printf("分配内存失败! 程序终止! "); exit(-1); } PNODEpTail=pHead; pTail->pNext=NULL; for(i=0;i { printf("请输入第%d个节点的有效数据: ",i+1); scanf("%d",&val); PNODEpNew=(PNODE)malloc(sizeof(NODE)); if(NULL==pNew) { printf("分配内存失败! 程序终止! "); exit(-1); } pNew->data=val; pTail->pNext=pNew; pNew->pNext=NULL; pTail=pNew; } returnpHead; } voidtraverse_list(PNODEpHead) { PNODEp=pHead->pNext; while(NULL! =p) { printf("%10d",p->data); p=p->pNext; } printf("\n"); return; } /********************************************************************** 2.编程实现链表的逆序,要求在原有结点空间上实现,如图2.3所示: **********************************************************************/ #include #include #include typedefstructNode { intdata; structNode*pNext; }NODE,*PNODE; PNODEcreate_list(void);//创建一个链表 voidtraverse_list(PNODEpHead);//遍历链表 voidreverse_link(PNODEpHead);//逆序 intmain(void) { PNODEpHead=NULL; pHead=create_list(); printf("遍历链表: \n"); traverse_list(pHead); printf("链表的长度是pHead->data=%d\n",pHead->data); printf("对链表中数据进行逆序排列: \n"); reverse_link(pHead); printf("逆序排列后输出\n"); traverse_list(pHead); return0; } PNODEcreate_list(void) { intlen;//链表中节点数目 inti; intval; printf("请输入要创建的链表中节点的个数len="); scanf("%d",&len); PNODEpHead=(PNODE)malloc(sizeof(NODE)); pHead->data=len; if(NULL==pHead) { printf("分配内存失败! 程序终止! "); exit(-1); } PNODEpTail=pHead; pTail->pNext=NULL; for(i=0;i { printf("请输入第%d个节点的有效数据: ",i+1); scanf("%d",&val); PNODEpNew=(PNODE)malloc(sizeof(NODE)); if(NULL==pNew) { printf("分配内存失败! 程序终止! "); exit(-1); } pNew->data=val; pTail->pNext=pNew; pNew->pNext=NULL; pTail=pNew; } returnpHead; } voidtraverse_list(PNODEpHead) { PNODEp=pHead->pNext; while(NULL! =p) { printf("%10d",p->data); p=p->pNext; } printf("\n"); return; } voidreverse_link(PNODEpHead) { PNODEp,q,ptmp; p=pHead->pNext; q=pHead->pNext->pNext; ptmp=NULL; while(q! =NULL) { ptmp=q->pNext; q->pNext=p; p=q; q=ptmp; } pHead->pNext->pNext=NULL; pHead->pNext=p; }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 开放 实验 答案 结构