数据结构实验报告.docx
- 文档编号:27016708
- 上传时间:2023-06-25
- 格式:DOCX
- 页数:31
- 大小:96.04KB
数据结构实验报告.docx
《数据结构实验报告.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告.docx(31页珍藏版)》请在冰豆网上搜索。
数据结构实验报告
院系:
计算机科学学院
专业:
自动化
年级:
课程名称:
计算机软件基础
学号:
姓名:
指导教师:
年月日
年级
班号
学号
专业
自动化
姓名
实验名称
顺序表的基本操作
实验
类型
设计型
综合型
创新型
√
实
验
目
的
或
要
求
实验目的:
1.理解线性表顺序存储结构的存储方式及其特点。
2.掌握对顺序表存储结构的描述、基本操作的算法实现。
3.掌握通过简单实验对C语言基础知识进行回顾;为后序的实验做准备。
实验要求:
1.程序采用模块化设计。
(独立的功能要用独立的函数来实现。
)
2.建立相应的功能菜单。
(针对本程序的5个功能建立相应的五个功能菜单
选择相应的功能选项后进入相应的功能操作界面)
3.每次实验完成后我都会随机挑选20%左右的同学的程序进行检查,程序
必须能够实现所有要求的功能,并对指定的任一模块进行详细的讲解。
若
未通过检查视为本次实验不合格。
4.实验报告根据模板要求认真填写。
实
验
原
理
(
算
法
流
程
)
实
验
原
理
(
算
法
流
程
)
实
验
原
理
(
算
法
流
程
)
实
验
原
理
(
算
法
流
程
)
实
验
原
理
(
算
法
流
程
)
实
验
原
理
(
算
法
流
程
)
#include
#include
#include
#defineN100
#defineM10
intf;
typedefstruct
{
charstr[20];
}st;
typedefstruct
{
intlen;
st*data;
intlinesize;
}linklist;
voidinit(linklist&l)//初始化函数
{
l.len=0;
l.data=(st*)malloc(N*sizeof(st));
l.linesize=N;
}
voidinput(linklist&l,inti)//输入函数
{
printf("请输入第%d个数据:
\t",i+1);
fflush(stdin);
gets(l.data[i].str);
}
voidmenu()
{
charch;
printf("\t|*************************主菜单*************************|\n");
printf("\t||\n");
printf("\t|1、录入数据2、查询数据|\n");
printf("\t||\n");
printf("\t|3、删除数据4、插入数据|\n");
printf("\t||\n");
printf("\t|5、修改数据6、退出系统|\n");
printf("\t||\n");
printf("\t|********************************************************|\n");
printf("\t请输入你想进行的操作(1~6):
\t");
fflush(stdin);
scanf("%d",&f);
system("cls");
if(f>6||f<1)
{
printf("输入错误!
\n按任意键返回主菜单重新输入!
\n");
fflush(stdin);
scanf("%c",&ch);
system("cls");
menu();
}
}
voiddisplay(linklist&l)//输出函数
{
inti;charch;
if(l.len==0)
{
printf("顺序表为空!
\n按任意键返回主菜单!
\n");
fflush(stdin);
scanf("%c",&ch);
system("cls");
menu();
}
for(i=0;i { printf("第%d个数据是: \t",i+1); puts(l.data[i].str); } } voidcreatlist(linklist&l)//创建顺序表 { inti; i=l.len; charch,ch1; if(l.len==0) { printf("|*************************开始创建顺序表*************************|\n"); } input(l,i); i++; l.len++; printf("是否继续输入? \t"); fflush(stdin); scanf("%c",&ch); while(ch=='y'||ch=='Y') { if(l.len>=l.linesize) { st*newbase; newbase=(st*)realloc(l.data,(N+M)*sizeof(st)); l.data=newbase; l.linesize+=M; } input(l,i); i++; l.len++; fflush(stdin); //ch=getchar(); printf("是否继续输入? \t"); scanf("%c",&ch); } system("cls"); printf("输入完成后的数据为: \n"); display(l); printf("按任意键返回主菜单! \n"); fflush(stdin); scanf("%c",&ch1); system("cls"); menu(); } intdatasearch(linklist&l)//查询函数 { stshuju; inti,j; j=-1; if(l.len==0) { returnj; } printf("请输入你要查询的数据: \t"); fflush(stdin); gets(shuju.str); i=0; while(i { if(strcmp(l.data[i].str,shuju.str)==0) { j=i+1; printf("该数据的位置是: %d\n",j); returnj; } i++; } printf("未找到该数据! \n"); returnj; } voiddatadelete(linklist&l)//删除函数 { charch; inti,j; printf("请输入你需要删除的数据的位置: \n"); fflush(stdin); scanf("%d",&i); for(j=i-1;j { l.data[j]=l.data[j+1]; } l.len--; printf("删除成功! \n"); printf("删除后的数据为: \n"); display(l); printf("按任意键返回主菜单! \n"); fflush(stdin); scanf("%c",&ch); system("cls"); menu(); } voiddatainsert(linklist&l)//插入函数 { inti,j; charch; charshuju[20]; if(l.len>=l.linesize) { st*newbase; newbase=(st*)realloc(l.data,(N+M)*sizeof(st)); l.data=newbase; l.linesize+=M; } printf("请输入你要插入数据的位置: \n"); fflush(stdin); scanf("%d",&i); printf("请输入你要插入的数据: \n"); fflush(stdin); gets(shuju); for(j=l.len-1;j>=i-1;j--) { l.data[j+1]=l.data[j]; } strcpy(l.data[i-1].str,shuju); l.len++; printf("插入成功! \n"); printf("插入完成后的数据为: \n"); display(l); printf("按任意键返回主菜单! \n"); fflush(stdin); scanf("%c",&ch); system("cls"); menu(); } voiddatamodify(linklist&l) { inti; charshuju[20]; charch,ch1; printf("请输入你要修改的数据的位置: \t"); fflush(stdin); scanf("%d",&i); printf("第%d个数据是: \t%s\n",i,l.data[i-1].str); printf("确认修改? fflush(stdin); scanf("%c",&ch); if(ch=='y'||ch=='Y') { printf("请输入修改后的数据: \t"); fflush(stdin); gets(shuju); strcpy(l.data[i-1].str,shuju); printf("修改成功! \n"); printf("修改完成后的数据为: \n"); display(l); } printf("按任意键返回主菜单! \n"); fflush(stdin); scanf("%c",&ch1); system("cls"); menu(); } voidmain() { inti; charch; linklistl; init(l); system("colora"); menu(); while(f! =0) { switch(f) { case1: creatlist(l);break; case2: i=datasearch(l); printf("按任意键返回主菜单! \n"); fflush(stdin); scanf("%c",&ch); system("cls"); menu();break; case3: datadelete(l);break; case4: datainsert(l);break; case5: datamodify(l);break; case6: exit(0); } } } 实 验 结 果 分 析 及 心 得 体 会 成 绩 评 定 教师签名: 2013年月日 年级 班号 学号 专业 自动化 姓名 实验名称 单循环链表的基本操作 实验 类型 设计型 综合型 创新型 √ 实 验 目 的 或 要 求 实验要求: 1.理解线性表链式存储结构的存储方式及其特点。 2.掌握对链表存储结构的描述、基本操作的算法实现。 3.通过实验掌握对C语言指针的基本应用。 实 验 原 理 ( 算 法 流 程 ) 实 验 原 理 ( 算 法 流 程 ) 实 验 原 理 ( 算 法 流 程 ) 实 验 原 理 ( 算 法 流 程 ) 实 验 原 理 ( 算 法 流 程 ) #include #include structlist { charc; structlist*next; }; intflag=1; structlist*_creat_() { structlist*p1,*p2,*head; head=p1=p2=(list*)malloc(sizeof(structlist)); p1->c=getchar(); while(p1->c! ='#') { p2=p1; p1=(list*)malloc(sizeof(structlist)); p1->c=getchar(); p2->next=p1; } p2->next=NULL; returnhead; } void_search_(structlist*p,charc) { for(;p;p=p->next) if(p->c==c) { printf("找到%c\n",c); return; } printf("未找到%c\n",c); } structlist*_insert_(structlist*p,intpos,charc) { structlist*t,*pre,*head=p; t=(list*)malloc(sizeof(structlist)); t->c=c; if(pos<=1) { t->next=p; returnt; } while(--pos&&p->next) { pre=p; p=p->next; } if(p->next==NULL) { p->next=t; t->next=NULL; returnhead; } t->next=p; pre->next=t; returnhead; } structlist*_delete_(structlist*p,charc) { structlist*head=p,*pre; while(p) { if(p->c==c) break; pre=p; p=p->next; } if(p==NULL) returnhead; elseif(p==head) { head=p->next; free(p); returnhead; } else { pre->next=p->next; free(p); returnhead; } } void_print_(structlist*p) { for(;p;p=p->next) putchar(p->c); putchar('\n'); } voidmenu() { charch; printf("\t\t1、创建单循环链表\n\n"); printf("\t\t2、查找字符\n\n"); printf("\t\t3、插入字符\n\n"); printf("\t\t4、删除字符\n\n"); printf("\t\t5、退出操作\n\n"); printf("\t****************************************\n"); printf("\t\t输入操作序号: "); fflush(stdin); scanf("%d",&flag); if(flag<1||flag>5) { printf("操作序号输入错误! \n"); printf("按回车键返回重新输入! \n"); fflush(stdin); scanf("%c",&ch); system("cls"); menu(); } } voidmain() { structlist*p; charc,ch; intn; while(flag! =0) { menu(); switch(flag) { case1: printf("输入一串字母'#'结束: \n"); p=_creat_(); _print_(p);break; case2: printf("输入要查找的字母: \n"); scanf("\n%c",&c); _search_(p,c);break; case3: printf("输入要插入的字母和位置: \n"); scanf("\n%c",&c); scanf("%d",&n); p=_insert_(p,n,c); _print_(p);break; case4: printf("输入要删除的字母: \n"); scanf("\n%c",&c); p=_delete_(p,c); _print_(p);break; case5: exit(0); } printf("按回车键返回! \n"); fflush(stdin); scanf("%c",&ch); system("cls"); } } 实 验 结 果 分 析 及 心 得 体 会 成 绩 评 定 教师签名: 2013年月日 年级 2012 班号 学号 专业 姓名 实验名称 二叉树的基本操作 实验 类型 设计型 综合型 创新型 √ 实 验 目 的 或 要 求 实验要求: 1)从键盘输入二叉树的各结点值,按先序递归方式创建二叉树 2)分别实现先序、中序、后序递归遍历二叉树 3)输出二叉树的高度 4)输出二叉树的按层次遍历序列* 5)以菜单方式运行 实 验 原 理 ( 算 法 流 程 ) 实 验 原 理 ( 算 法 流 程 ) 实 验 原 理 ( 算 法 流 程 ) 实 验 原 理 ( 算 法 流 程 ) #include #include #include typedefcharelemtype; typedefstructnode { elemtypedata; structnode*lchild,*rchild; }bitree; intflag; bitree*creat_tree()//创建二叉树 { charc;bitree*root; printf("输入二叉树的节点: \t"); fflush(stdin); scanf("%c",&c); if(c=='@')root=NULL; else { root=(bitree*)malloc(sizeof(bitree)); root->data=c; root->lchild=creat_tree(); root->rchild=creat_tree(); } return(root); } voidDLR(bitree*r)//先序遍历 { if(r! =NULL) { printf("%c",r->data); DLR(r->lchild); DLR(r->rchild); } } voidLDR(bitree*r)//中序遍历 { if(r! =NULL) { LDR(r->lchild); printf("%c",r->data); LDR(r->rchild); } } voidLRD(bitree*r)//后续遍历 { if(r! =NULL) { LRD(r->lchild); LRD(r->rchild); printf("%c",r->data); } } intbitreeDepth(bitree*r)//求二叉树的深度 { inth1,h2; if(r==NULL) return0; else { h1=bitreeDepth(r->lchild); h2=bitreeDepth(r->rchild); if(h1>h2) returnh1+1; else returnh2+1; } } voidmenu() { charch; system("cls"); printf("\t******************二叉树的基本操作******************\n\n"); printf("1、创建二叉树\n\n"); printf("\t2、二叉树的先序遍历\n\n"); printf("\t3、二叉树的中序遍历\n\n"); printf("\t4、二叉树的后序遍历\n\n"); printf("\t5、二叉树的深度\n\n"); printf("\t6、退出操作\n\n"); printf("\t****************************************************\n"); printf("\t请输入操作序号: \t"); fflush(stdin); scanf("%d",&flag); if(flag<1||flag>6) { system("cls"); printf("输入操作序号错误! \n"); printf("按回车键返回重输入! \n"); fflush(stdin); scanf("%c",&ch); menu(); } } vo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验 报告