数据结构上机操作题Word格式文档下载.docx
- 文档编号:7239999
- 上传时间:2023-05-08
- 格式:DOCX
- 页数:16
- 大小:21.57KB
数据结构上机操作题Word格式文档下载.docx
《数据结构上机操作题Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《数据结构上机操作题Word格式文档下载.docx(16页珍藏版)》请在冰点文库上搜索。
voidsubs()
node*p,*p1,*p2,*q,*heada,*headb;
heada=creatlist();
headb=creatlist();
p=heada;
p1=p;
while(p!
=NULL)
q=headb;
while(q->
data!
=p->
data&
&
q!
=NULL)q=q->
if(q!
if(p==heada)
heada=heada->
p1=heada;
elseif(p->
next==NULL)p1->
elsep1->
next=p->
p2=p->
p->
free(p);
p=p2;
else
p=p->
if(p==NULL)
kong\n"
A-B\n"
%d\n"
p->
data);
main()
subs();
实验二二叉树
(掌握二叉树的链式存储结构、二叉树的遍历方法和带权二叉树的用途。
)
方案一:
二叉树的建立和遍历
具体内容:
先生成一棵二叉树,再用中序遍历方式打印每个结点值,并统计其叶子结点的个数。
操作建议:
①二叉树的每个结点请从键盘输入(数据类型自选),边输入边建树。
而且每个结点按“左小右大”的原则依次挂接在前一结点后。
例如,对于输入序列(12,7,17,11,16,2,13,9,21,4),应以12作为根结点,将7(比12小)链接在12的左边,把17链接在12的右边,11链接在7的左边,…见下图。
②二叉树的遍历请用中序遍历方式,边遍历边打印(输出的结点序列会很有趣!
),同时统计叶子结点个数,等全部结点数打印完毕后,还要换行打印出你所统计的叶子结点数。
实验二二叉树的参考程序
参考程序如下:
对序列12,7,17,11,16,2,13,9,21,4构成一棵二叉树(按左小右大原则),然后中序遍历输出数据元素序列。
解:
首先笔算:
画出树的形状如下:
12
717
2111621
4913
用中序遍历应得到排序结果2,4,7,9,11,12,13,16,17,21
然后编程:
说明部分为:
#include<
stdlib.h>
typedefstructliuyu{intdata;
structliuyu*lchild,*rchild;
}test;
liuyu*root;
intm=sizeof(test);
voidinsert_data(intx)/*先生成二叉排序树*/
{liuyu*p,*q,*s;
s=(test*)malloc(m);
s->
lchild=NULL;
rchild=NULL;
if(!
root){root=s;
return;
}
p=root;
while(p)/*如何接入二叉排序树的适当位置*/
{q=p;
if(p->
data==x){printf("
dataalreadyexist!
\n"
return;
elseif(x<
p->
data)p=p->
lchild;
elsep=p->
rchild;
if(x<
q->
data)q->
lchild=s;
elseq->
rchild=s;
voidLDR(liuyu*root)/*中序遍历递归函数*/
{if(!
root)return;
LDR(root->
lchild);
printf("
root->
LDR(root->
rchild);
voidmain()/*先生成二叉排序树,再调用中序遍历递归函数进行排序输出*/
{inti,x;
i=1;
root=NULL;
/*千万别忘了赋初值给root!
*/
do{printf("
pleaseinputdata%d:
"
i);
i++;
scanf("
/*从键盘采集数据,以-9999表示输入结束*/
if(x==-9999){printf("
\nNowoutputasortresult:
LDR(root);
insert_data(x);
}/*调用插入数据元素的函数*/
while(x!
=-9999);
实验三排序和查找
先从键盘输入26个字母生成无序数组,对数组排序后,再从键盘输入一个字符进行折半查找。
实验三方案1参考程序
#include<
#defineM100
intn,mid;
charstr[M],K;
voidbuild()
{
inti,j,w;
chartemp;
n=strlen(str);
for(i=0;
i<
n-1;
i++)
w=i;
for(j=i+1;
j<
n;
j++)
if(str[j]<
str[w])w=j;
if(w!
=i)
{temp=str[i];
str[i]=str[w];
str[w]=temp;
binsrch()
intlow,high;
low=0;
high=n-1;
while(low<
=high)
mid=(low+high)/2;
if(K==str[mid])return(mid);
elseif(K<
str[mid])high=mid-1;
elselow=mid+1;
Kisnotfound\n"
gets(str);
build();
%c"
K);
binsrch();
%cisthe%dthelement\n"
K,mid+1);
实验四:
为一个图(maxnode=20)建立一个邻接表、编写深度遍历和广度遍历算法并给出遍历结果。
答案
/*调试成功邻接矩阵的深度遍历和广度遍历无向图*/
constintn=6;
/*图中顶点数*/
constinte=7;
/*图中边数*/
structgraph{intV[6];
intarcs[6][6];
}*a;
typedefstructgraphGraph;
intvisited[7],visited1[7];
/*顶点的数据类型*/
voidcreatedj(Graph*g)
{inti,j,k;
pleaseinputnnodeinformation\n"
for(k=1;
k<
=6;
k++)
{printf("
inputv[i]"
k);
a->
V[k]);
/*输入顶点信息序号*/
visited[k]=0;
/*初始化访问标志数组*/
visited1[k]=0;
\n\n"
for(i=1;
for(j=1;
a->
arcs[i][j]=0;
/*邻接矩阵初始化*/
pleaseinputedgeinformation\n"
/*输入边的信息*/
for(k=1;
=7;
inputstartnode,endnodelikethis:
ij\n"
/*输入起始结点和终止结点序号*/
%d\t"
%d%d"
i,&
j);
arcs[i][j]=1;
arcs[j][i]=1;
/*邻接矩阵的深度遍历*/
voiddfs(Graph*g,inti)
intj;
g->
V[i]);
/*visit(i);
visited[i]=1;
for(j=1;
j<
j++)
if((g->
arcs[i][j]==1)&
(!
visited[j]))
dfs(g,j);
/*邻接矩阵的广度遍历*/
voidbfs_Matrix(Graph*g,inti)
{intQ[7];
intf,r,j;
f=r=0;
visited1[i]=1;
r++;
Q[r]=i;
/*i入队列*/
while(f<
r)
{f++;
i=Q[f];
/*被访问过的点出对*/
for(j=1;
=n;
arcs[i][j]==1)&
(!
visited1[j]))
{printf("
V[j]);
visited1[j]=1;
Q[r]=j;
inti;
intj;
/*=======Createadjacenttable=======*/
createdj(a);
===============================\n"
ThegraphinDFSis:
dfs(a,1);
ThegraphinBFSis:
bfs_Matrix(a,2);
return(0);
//创建无向图图的连接表,进行深度和广度遍历调试成功
#defineLINKstructlink
#defineHEADNODEstructheadnode
#defineNULL0
#definemaxnode20
LINK
intadjvex;
chardata;
LINK*next;
};
HEADNODE
intV;
}*g[maxnode];
intvisited[maxnode];
intvisited1[maxnode];
voidcreategraph()
{intn,e;
//顶点个数,边的数目
inti,j,k;
LINK*s;
Node(n),arc(e)"
//输入边数和顶点数
n,&
e);
for(i=1;
i++)//对n个单链表进行初始化
g[i]=(HEADNODE*)malloc(sizeof(HEADNODE));
g[i]->
V=i;
visited[i]=0;
//用于深度遍历
visited1[i]=0;
//用于广度遍历
i=j=0;
=e;
k++)//插入边数,在单链表上插入新的结点形成图。
printf("
\arc>
\tstartNodeendNode:
ij(ENTER)\n"
if(i>
n||j>
n)printf("
errorpleaseinputagain"
s=(LINK*)malloc(sizeof(LINK));
//无向图需要两个结点单链表的头插法
scanf("
%d%d"
s->
adjvex=j;
next=g[i]->
adjvex=i;
next=g[j]->
g[j]->
main()
voiddfs_link();
voidbfs_link();
creategraph();
dfs_link
(2);
bfs_link
(1);
//对图进行深度遍历
voiddfs_link(inti)
LINK*p;
//p=(LINK*)malloc(sizeof(LINK));
g[i]->
V);
p=g[i]->
while(p!
=NULL)
if(visited[p->
adjvex]==0)
dfs_link(p->
adjvex);
p=p->
//对图进行广度遍历
voidbfs_link(inti)
{intQ[maxnode+1];
intfront=0,rear=0;
//print访问的结点序号
rear++;
Q[rear]=i;
while(front<
rear)
{front++;
i=Q[front];
{if(!
visited1[p->
adjvex])
{printf("
g[p->
adjvex]->
visited1[p->
adjvex]=1;
rear++;
Q[rear]=p->
adjvex;
}
实验五冒泡算法的实现
冒泡算法的实现
#include"
stdio.h"
voidbubble(int*x,intn)
{inti,j,flag=1;
intswap;
(i<
n)&
(flag==1);
{flag=0;
for(j=0;
n-i-1;
if(x[j]>
x[j+1])
{flag=1;
swap=x[j];
x[j]=x[j+1];
x[j+1]=swap;
10;
%d"
x[i]);
i);
voidmain()
{inti;
intarray[10]={55,2,6,4,32,12,9,73,26,37};
clrscr();
bubble(array,10);
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 上机 操作
![提示](https://static.bingdoc.com/images/bang_tan.gif)