数据结构实验报告答案.docx
- 文档编号:10452948
- 上传时间:2023-02-11
- 格式:DOCX
- 页数:43
- 大小:43.18KB
数据结构实验报告答案.docx
《数据结构实验报告答案.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告答案.docx(43页珍藏版)》请在冰豆网上搜索。
数据结构实验报告答案
数据结构(C语言版) 实验报告
专业 班级 学号 姓名
实验1
实验题目:
单链表得插入与删除
实验目得:
了解与掌握线性表得逻辑结构与链式存储结构,掌握单链表得基本算法及相关得时间性能分析。
实验要求:
建立一个数据域定义为字符串得单链表,在链表中不允许有重复得字符串;根据输入得字符串,先找到相应得结点,后删除之。
实验主要步骤:
1、分析、理解给出得示例程序。
2、调试程序,并设计输入数据(如:
bat,cat,eat,fat,hat,jat,lat,mat,#),测试程序得如下功能:
不允许重复字符串得插入;根据输入得字符串,找到相应得结点并删除。
3、修改程序:
(1)增加插入结点得功能。
(2)将建立链表得方法改为头插入法.
程序代码:
#include"stdio、h”
#include"string、h"
#include"stdlib、h"
#include"ctype、h”
typedef struct node //定义结点
{
ﻩchar data[10]; //结点得数据域为字符串
ﻩstruct node*next;//结点得指针域
}ListNode;
typedefListNode*LinkList; //自定义LinkList单链表类型
LinkListCreatListR1(); //函数,用尾插入法建立带头结点得单链表
LinkList CreatList(void); //函数,用头插入法建立带头结点得单链表
ListNode*LocateNode(); //函数,按值查找结点
voidDeleteList(); //函数,删除指定值得结点
void printlist();//函数,打印链表中得所有值
voidDeleteAll(); //函数,删除所有结点,释放内存
ListNode*AddNode();ﻩ//修改程序:
增加节点.用头插法,返回头指针
//==========主函数==============
voidmain()
{
ﻩcharch[10],num[5];
LinkListhead;
head=CreatList(); //用头插入法建立单链表,返回头指针
printlist(head); //遍历链表输出其值
printf(” Delete node(y/n):
”); //输入”y”或”n”去选择就是否删除结点
ﻩscanf("%s",num);
ﻩif(strcmp(num,"y”)==0|| strcmp(num,"Y")==0){
ﻩprintf(”Please inputDelete_data:
”);
ﻩscanf(”%s”,ch); //输入要删除得字符串
ﻩDeleteList(head,ch);
ﻩprintlist(head);
ﻩ}
printf("Addnode?
(y/n):
”); //输入”y"或”n"去选择就是否增加结点
scanf(”%s",num);
if(strcmp(num,"y")==0||strcmp(num,"Y”)==0)
ﻩ{
ﻩﻩhead=AddNode(head);
ﻩ}
printlist(head);
DeleteAll(head); //删除所有结点,释放内存
}
//==========用尾插入法建立带头结点得单链表===========
LinkListCreatListR1(void)
{
char ch[10];
LinkListhead=(LinkList)malloc(sizeof(ListNode));//生成头结点
ListNode*s,*r,*pp;
r=head;
r->next=NULL;
printf("Input # to end”); //输入”#"代表输入结束
printf("\nPleaseinputNode_data:
");
scanf("%s",ch); //输入各结点得字符串
while(strcmp(ch,"#")!
=0) {
ﻩpp=LocateNode(head,ch); //按值查找结点,返回结点指针
ﻩif(pp==NULL){ //没有重复得字符串,插入到链表中
ﻩﻩs=(ListNode*)malloc(sizeof(ListNode));
ﻩﻩstrcpy(s->data,ch);
ﻩﻩr—〉next=s;
ﻩﻩr=s;
ﻩr-〉next=NULL;
ﻩ}
ﻩprintf(”Input#toend ");
ﻩﻩprintf("PleaseinputNode_data:
”);
ﻩﻩscanf(”%s",ch);
}
returnhead; //返回头指针
}
//==========用头插入法建立带头结点得单链表===========
LinkListCreatList(void)
{
ﻩcharch[100];
ﻩLinkListhead,p;
ﻩhead=(LinkList)malloc(sizeof(ListNode));
ﻩhead—>next=NULL;
ﻩwhile
(1)
ﻩ{
ﻩprintf(”Input#toend");
ﻩprintf(”Please inputNode_data:
");
ﻩscanf("%s”,ch);
ﻩif(strcmp(ch,”#"))
{
ﻩﻩif(LocateNode(head,ch)==NULL)
ﻩﻩ{
ﻩﻩﻩstrcpy(head—〉data,ch);
ﻩp=(LinkList)malloc(sizeof(ListNode));
ﻩﻩﻩp—>next=head;
ﻩﻩﻩhead=p;
ﻩﻩ}
}
ﻩelse
ﻩﻩbreak;
}
return head;
}
//==========按值查找结点,找到则返回该结点得位置,否则返回NULL==========
ListNode *LocateNode(LinkListhead,char*key)
{
ListNode *p=head-〉next;//从开始结点比较
while(p!
=NULL&&strcmp(p->data,key)!
=0)//直到p为NULL或p-〉data为key止
ﻩp=p-〉next; //扫描下一个结点
return p; //若p=NULL则查找失败,否则p指向找到得值为key得结点
}
//==========修改程序:
增加节点=======
ListNode*AddNode(LinkListhead)
{
charch[10];
ListNode *s,*pp;
printf("\nPleaseinputaNewNode_data:
");
scanf("%s",ch); //输入各结点得字符串
ﻩpp=LocateNode(head,ch); //按值查找结点,返回结点指针
printf("ok2\n”);
ﻩif(pp==NULL) { //没有重复得字符串,插入到链表中
ﻩs=(ListNode*)malloc(sizeof(ListNode));
strcpy(s—>data,ch);
printf(”ok3\n");
s—〉next=head—>next;
ﻩhead-〉next=s;
ﻩ}
ﻩreturnhead;
}
//==========删除带头结点得单链表中得指定结点=======
voidDeleteList(LinkListhead,char *key)
{
ListNode *p,*r,*q=head;
p=LocateNode(head,key); //按key值查找结点得
if(p==NULL){ //若没有找到结点,退出
ﻩprintf(”positionerror”);
ﻩexit(0);
}
while(q->next!
=p) //p为要删除得结点,q为p得前结点
ﻩﻩq=q—>next;
r=q-〉next;
q—>next=r->next;
free(r); //释放结点
}
//===========打印链表=======
voidprintlist(LinkList head)
{
ListNode*p=head->next; //从开始结点打印
while(p){
printf(”%s,”,p—〉data);
p=p-〉next;
}
printf("\n”);
}
//==========删除所有结点,释放空间===========
voidDeleteAll(LinkListhead)
{
ListNode*p=head,*r;
while(p->next){
r=p—>next;
ﻩfree(p);
p=r;
ﻩ}
free(p);
}
实验结果:
Input# toendPlease inputNode_data:
bat
Input#to end PleaseinputNode_data:
cat
Input#toend Please inputNode_data:
eat
Input #toendPleaseinputNode_data:
fat
Input# toend Please input Node_data:
hat
Input#toendPleaseinputNode_data:
jat
Input#toend PleaseinputNode_data:
lat
Input#toend PleaseinputNode_data:
mat
Input#toendPleaseinputNode_data:
#
mat,lat,jat,hat,fat, eat,cat, bat,
Deletenode(y/n):
y
PleaseinputDelete_data:
hat
mat,lat,jat, fat,eat, cat,bat,
Insertnode(y/n):
y
Pleaseinput Insert_data:
put
position :
5
mat, lat, jat,fat, eat, put, cat,bat,
请按任意键继续、 、、
示意图:
lat
jat
hat
fat
eat
cat
bat
mat
NULL
head
lat
jat
hat
fat
eat
cat
bat
mat
head
lat
jat
fat
eat
put
cat
bat
mat
head
NULL
NULL
心得体会:
本次实验使我们对链表得实质了解更加明确了,对链表得一些基本操作也更加熟练了。
另外实验指导书上给出得代码就是有一些问题得,这使我们认识到实验过程中不能想当然得直接编译执行,应当在阅读并完全理解代码得基础上再执行,这才就是实验得意义所在。
实验2
实验题目:
二叉树操作设计与实现
实验目得:
掌握二叉树得定义、性质及存储方式,各种遍历算法。
实验要求:
采用二叉树链表作为存储结构,完成二叉树得建立,先序、中序与后序以及按层次遍历得操作,求所有叶子及结点总数得操作。
实验主要步骤:
1、分析、理解程序。
2、调试程序,设计一棵二叉树,输入完全二叉树得先序序列,用#代表虚结点(空指针),如ABD###CE##F##,建立二叉树,求出先序、中序与后序以及按层次遍历序列,求所有叶子及结点总数。
实验代码
#include”stdio、h"
#include"stdlib、h"
#include"string、h"
#define Max 20 //结点得最大个数
typedefstructnode{
char data;
structnode*lchild,*rchild;
}BinTNode; //自定义二叉树得结点类型
typedefBinTNode*BinTree;//定义二叉树得指针
int NodeNum,leaf; //NodeNum为结点数,leaf为叶子数
//==========基于先序遍历算法创建二叉树==============
//=====要求输入先序序列,其中加入虚结点"#”以示空指针得位置==========
BinTree CreatBinTree(void)
{
BinTreeT;
charch;
if((ch=getchar())=='#’)
return(NULL); //读入#,返回空指针
else{
T=(BinTNode*)malloc(sizeof(BinTNode));//生成结点
T->data=ch;
ﻩT—〉lchild=CreatBinTree(); //构造左子树
ﻩT—〉rchild=CreatBinTree();//构造右子树
ﻩreturn(T);
}
}
//========NLR先序遍历=============
voidPreorder(BinTreeT)
{
if(T){
ﻩprintf(”%c”,T-〉data); //访问结点
ﻩﻩPreorder(T—〉lchild); //先序遍历左子树
ﻩPreorder(T->rchild); //先序遍历右子树
}
}
//========LNR中序遍历===============
void Inorder(BinTreeT)
{
if(T) {
ﻩInorder(T—>lchild); //中序遍历左子树
ﻩprintf(”%c”,T->data);//访问结点
Inorder(T-〉rchild); //中序遍历右子树
}
}
//==========LRN 后序遍历============
voidPostorder(BinTreeT)
{
if(T){
ﻩPostorder(T->lchild); //后序遍历左子树
Postorder(T->rchild);//后序遍历右子树
printf("%c",T—>data); //访问结点
}
}
//=====采用后序遍历求二叉树得深度、结点数及叶子数得递归算法========
intTreeDepth(BinTreeT)
{
int hl,hr,max;
if(T){
ﻩhl=TreeDepth(T-〉lchild);//求左深度
hr=TreeDepth(T—〉rchild);//求右深度
max=hl>hr?
hl:
hr; //取左右深度得最大值
ﻩﻩNodeNum=NodeNum+1; //求结点数
ﻩif(hl==0&&hr==0)leaf=leaf+1;//若左右深度为0,即为叶子.
return(max+1);
}
elsereturn(0);
}
//====利用"先进先出"(FIFO)队列,按层次遍历二叉树==========
voidLevelorder(BinTreeT)
{
intfront=0,rear=1;
BinTNode*cq[Max],*p;//定义结点得指针数组cq
cq[1]=T; //根入队
while(front!
=rear)
{
front=(front+1)%NodeNum;
ﻩﻩp=cq[front]; //出队
ﻩﻩprintf(”%c",p—〉data);//出队,输出结点得值
ﻩﻩif(p->lchild!
=NULL){
ﻩrear=(rear+1)%NodeNum;
ﻩcq[rear]=p—〉lchild; //左子树入队
ﻩ}
if(p—〉rchild!
=NULL){
rear=(rear+1)%NodeNum;
ﻩﻩ cq[rear]=p->rchild; //右子树入队
}
}
}
//====数叶子节点个数==========
int countleaf(BinTreeT)
{
int hl,hr;
if(T){
hl=countleaf(T-〉lchild);
ﻩhr=countleaf(T->rchild);
if(hl==0&&hr==0)//若左右深度为0,即为叶子。
ﻩreturn
(1);
ﻩelse returnhl+hr;
}
ﻩelse return0;
}
//==========主函数=================
void main()
{
BinTreeroot;
ﻩchari;
int depth;
printf("\n”);
printf("CreatBin_Tree;Input preorder:
");//输入完全二叉树得先序序列,
//用#代表虚结点,如ABD###CE##F##
root=CreatBinTree();//创建二叉树,返回根结点
do{ //从菜单中选择遍历方式,输入序号。
ﻩprintf("\t********** select ************\n”);
printf("\t1:
Preorder Traversal\n");
ﻩﻩprintf("\t2:
IorderTraversal\n");
printf(”\t3:
Postordertraversal\n");
ﻩprintf(”\t4:
PostTreeDepth,Nodenumber,Leafnumber\n");
ﻩprintf("\t5:
LevelDepth\n”);//按层次遍历之前,先选择4,求出该树得结点数。
printf(”\t0:
Exit\n");
ﻩﻩprintf("\t*******************************\n”);
ﻩfflush(stdin);
ﻩscanf("%c”,&i);//输入菜单序号(0-5)
switch (i-'0'){
ﻩcase1:
printf(”PrintBin_treePreorder:
");
ﻩPreorder(root); //先序遍历
ﻩﻩbreak;
ﻩcase 2:
printf("PrintBin_TreeInorder:
”);
ﻩInorder(root); //中序遍历
ﻩbreak;
ﻩcase3:
printf(”PrintBin_TreePostorder:
");
ﻩﻩﻩPostorder(root);//后序遍历
ﻩﻩbreak;
case 4:
ﻩﻩdepth=TreeDepth(root); //求树得深度及叶子数
ﻩprintf(”BinTree Depth=%dBinTreeNodenumber=%d",depth,NodeNum);
ﻩprintf("BinTreeLeafnumber=%d",countleaf(root));
ﻩﻩbreak;
case 5:
printf("LevePrintBin_Tree:
");
ﻩLevelorder(root); //按层次遍历
ﻩﻩbreak;
default:
exit(1);
}
ﻩprintf(”\n");
} while(i!
=0);
}
实验结果:
CreatBin_Tree; Inputpreorder:
ABD###CE##F##
**********select ************
1:
PreorderTraversal
2:
Iorder Traversal
3:
Postordertraversal
4:
PostTreeDepth,Node number,Leaf number
5:
LevelDepth
0:
Exit
*******************************
1
Print Bin_treePreorder:
ABDCEF
2
PrintBin_Tree Inorder:
DBAECF
3
PrintBin_TreePostorder:
DBEFCA
4
BinTreeDepth=3BinTreeNodenumber=6 BinTreeLeafnumber=3
5
LevePrint Bin_Tree:
ABCDEF
0
Pressanykey tocontinue
二叉树示意图
A
B
F
E
D
C
心得体会:
这次实验加深了我对二叉树得印象,尤其就是对二叉树得各种遍历操作有了一定得了解.同时认识到,在设计程序时辅以图形化得描述就是非常有用处得。
ﻬ实验3
实验题目:
图得遍历操作
实验目得:
掌握有向图与无向图得概念;掌握邻接矩阵与邻接链表建立图得存储结构;掌握DFS及BFS对图得遍历操作;了解图结构在人工智能、工程等领域得广泛应用。
实验要求:
采用邻接矩阵与邻接链表作为图得存储结构,完成有向图与无向图得DFS与BFS操作。
实验主要步骤:
设计一个有向图与一个无向图,任选一种存储结构,完成有向图与无向图得DFS(深度优先遍历)与BFS(广度优先遍历)得操作.
1.邻接矩阵作为存储结构
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验 报告 答案
![提示](https://static.bdocx.com/images/bang_tan.gif)