数据结构实验报告Word文件下载.docx
- 文档编号:18635137
- 上传时间:2022-12-30
- 格式:DOCX
- 页数:15
- 大小:145.38KB
数据结构实验报告Word文件下载.docx
《数据结构实验报告Word文件下载.docx》由会员分享,可在线阅读,更多相关《数据结构实验报告Word文件下载.docx(15页珍藏版)》请在冰豆网上搜索。
printf("
输入顶点数目:
"
);
scanf("
%d"
&
G.vexnum);
输入弧的数目:
G.arcnum);
for(i=0;
i<
G.vexnum;
++i)//初始化邻接矩阵
{
G.vertices[i].data=i;
G.vertices[i].firstarc=NULL;
//没有相关信息
}
输入弧的始点i和终点j\n"
for(k=0;
k<
G.arcnum;
++k){
输入第%d条弧的始点i:
k+1);
v1);
输入第%d条弧的终点j:
v2);
i=v1;
j=v2;
while(i<
1||i>
G.vexnum||j<
1||j>
G.vexnum){
}
i--;
j--;
%d%d\n"
v1,v2);
ArcNode*p;
p=newArcNode;
if(!
p){
printf("
空间不足!
\n"
return(0);
p->
adjvex=j;
nextarc=G.vertices[i].firstarc;
G.vertices[i].firstarc=p;
有向图建立成功!
\n\n"
returnOK;
}
voidDFS(ALGraphG,intv,int*visited)
{
intw;
visited[v]=1;
%d->
v+1);
for(w=G.vertices[v].data;
G.vertices[v].firstarc!
=NULL;
w=G.vertices[v].firstarc->
adjvex,G.vertices[v].firstarc=G.vertices[v].firstarc->
nextarc)
if(visited[w]==0)
DFS(G,w,visited);
voidDFSGraph(ALGraphG)
intv;
intvisited[MAX_VERTEX_NUM];
for(v=0;
v<
++v)
visited[v]=0;
if(visited[v]==0)
DFS(G,v,visited);
typedefstructSqQueue
{
QElemType*base;
intfront;
intrear;
}SqQueue;
intInitQueue(SqQueue&
Q)
Q.base=newQElemType;
Q.base)
error"
return0;
Q.front=Q.rear=0;
intEnQueue(SqQueue&
Q,QElemTypee)
if((Q.rear+1)%MAXQSIZE==Q.front)
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
}
intQueueEmpty(SqQueueQ)
if(Q.front==Q.rear)
{returnOK;
else
return0;
intDeQueue(SqQueue&
Q,QElemType&
e)
{if(Q.front==Q.rear)
{
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXQSIZE;
returne;
voidBFSGrahp(ALGraphG)
{intv,w,u;
SqQueueQ;
InitQueue(Q);
EnQueue(Q,v);
while(!
QueueEmpty(Q))
DeQueue(Q,u);
for(w=G.vertices[u].data;
G.vertices[u].firstarc!
w=G.vertices[u].firstarc->
adjvex,G.vertices[u].firstarc=G.vertices[u].firstarc->
visited[w]=1;
w+1);
EnQueue(Q,w);
}
if(visited[w]==0)
{
visited[w]=1;
printf("
EnQueue(Q,w);
}
voidmain()
ALGraphG;
CreateGraph(G);
\n深度优先遍历的结果:
DFSGraph(G);
\n广度优先遍历的结果:
BFSGrahp(G);
实验六
#include<
stdlib.h>
io.h>
process.h>
typedefintInfoType;
#defineEQ(a,b)((a)==(b))
#defineLT(a,b)((a)<
(b))
#defineLQ(a,b)((a)<
=(b))
#defineMAXSIZE20
typedefintKeyType;
structRedType
KeyTypekey;
InfoTypeotherinfo;
};
structSqList
RedTyper[MAXSIZE+1];
intlength;
voidInsertSort(SqList&
L)
inti,j;
for(i=2;
=L.length;
++i)
ifLT(L.r[i].key,L.r[i-1].key)
L.r[0]=L.r[i];
for(j=i-1;
LT(L.r[0].key,L.r[j].key);
--j)
L.r[j+1]=L.r[j];
L.r[j+1]=L.r[0];
intPartition(SqList&
L,intlow,inthigh)
RedTypet;
KeyTypepivotkey;
pivotkey=L.r[low].key;
while(low<
high)
high&
&
L.r[high].key>
=pivotkey)
--high;
t=L.r[low];
L.r[low]=L.r[high];
L.r[high]=t;
L.r[low].key<
++low;
returnlow;
voidQSort(SqList&
intpivotloc;
if(low<
pivotloc=Partition(L,low,high);
QSort(L,low,pivotloc-1);
QSort(L,pivotloc+1,high);
voidQuickSort(SqList&
QSort(L,1,L.length);
voidprint(SqListL)
inti;
for(i=1;
i++)
(%d,%d)"
L.r[i].key,L.r[i].otherinfo);
typedefSqListHeapType;
voidHeapAdjust(HeapType&
H,ints,intm)
RedTyperc;
intj;
rc=H.r[s];
for(j=2*s;
j<
=m;
j*=2)
if(j<
m&
LT(H.r[j].key,H.r[j+1].key))
++j;
LT(rc.key,H.r[j].key))
break;
H.r[s]=H.r[j];
s=j;
H.r[s]=rc;
voidHeapSort(HeapType&
H)
for(i=H.length/2;
i>
0;
--i)
HeapAdjust(H,i,H.length);
for(i=H.length;
1;
--i)
t=H.r[1];
H.r[1]=H.r[i];
H.r[i]=t;
HeapAdjust(H,1,i-1);
#defineN8
voidmain()
RedTyped[N]={{49,1},{38,2},{65,3},{97,4},{76,5},{13,6},{27,7},{49,8}};
HeapTypeh;
N;
h.r[i+1]=d[i];
h.length=N;
排序前:
print(h);
HeapSort(h);
排序后:
实验四
#include<
malloc.h>
#defineOK1
#defineERROR0
#defineOVERFLOW-2
typedefintstatus;
typedefcharTElemType;
typedefstructBiTNode
{//注意采用的是二叉链表作为二叉树的存储结构
TElemTypedata;
structBiTNode*lchild,*rchild;
}BiTNode,*BiTree;
statusCreateBiTree_PreOrder(BiTree&
T)
{//先序次序构造二叉树
TElemTypech;
%c"
ch);
if(ch=='
'
)T=NULL;
else{
(T=(BiTNode*)malloc(sizeof(BiTNode))))exit(OVERFLOW);
T->
data=ch;
CreateBiTree_PreOrder(T->
lchild);
rchild);
statusCountNode(BiTreeT)
{//初始条件:
树T存在。
操作结果:
结点总数
if(T)
return1+CountNode(T->
lchild)+CountNode(T->
intTreeDepth(BiTreeT)
返回T的深度
intl,r;
if(T){
l=TreeDepth(T->
lchild)+1;
r=TreeDepth(T->
rchild)+1;
returnl>
r?
l:
r;
statusPreOrderTraverse(BiTreeT,status(*Visit)(TElemTypee))
{//先序遍历
if(T){
if(Visit(T->
data))
if(PreOrderTraverse(T->
lchild,Visit))
rchild,Visit))
returnERROR;
statusInOrderTraverse(BiTreeT,status(*Visit)(TElemTypee))
{//中序遍历
if(T!
=NULL){
if(InOrderTraverse(T->
elsereturnOK;
statusPostOrderTraverse(BiTreeT,status(*Visit)(TElemTypee))
{//后序遍历
if(PostOrderTraverse(T->
statusVisit(TElemTypee)
{//输出各结点的数据值
%3c"
e);
BiTreeT;
构造一棵二叉树(abc##d#e##f):
CreateBiTree_PreOrder(T);
先序遍历输出:
PreOrderTraverse(T,Visit);
printf("
中序遍历输出:
InOrderTraverse(T,Visit);
后序遍历输出:
PostOrderTraverse(T,Visit);
结点总数:
%d\n"
CountNode(T));
树的深度:
TreeDepth(T));
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 实验 报告
![提示](https://static.bdocx.com/images/bang_tan.gif)