数据结构实验参考源代码 实验36.docx
《数据结构实验参考源代码 实验36.docx》由会员分享,可在线阅读,更多相关《数据结构实验参考源代码 实验36.docx(52页珍藏版)》请在冰豆网上搜索。
数据结构实验参考源代码实验36
实验三树形结构
以下为部分参考程序:
//递归实现二叉树的3种遍历
#include
typedefcharelemtype;
structbitree
{定义二叉树数据类型
elemtypedata;//结点信息
bitree*lchild,*rchild;//左右孩子
};
bitree*create()//建立二叉链表
{bitree*root,*s,*q[100];//q为队列,最大空间为100
intfront=l,rear=0;//队列头、尾指针
charch;
root=NULL;
cout<<”请输入结点值(‘,’为虚结点,‘#’结束):
”<cin>>ch;while(ch!=’#’){s==NULL;if(ch!=’,’){s=newbitree;s->data=ch;s->lchild=NULL;s->rchild=NULL;}rear++;q[rear]=s;//进队if(rear==1)root=s;else{if((s!=NULL)&&(q[front]!=NULL)){if(rear%2==0)q[front]->lchild=s;elseq[front]->rchid=s;}if(rear%2==1)front++;//出队}cin>>ch;}returnroot:}voidpreorder(bitree*root)//先序遍历{bitree*p;p=root;if(p!=NULL){cout<data<<””;preorder(p->lchild);preorder(p->rchild);}}voidinorderl(bitree*root)//中序遍历{bitree*p;p=root;if(p!=NULL){inorder(p->lchild);cout<data<<“”;inorder(p->rchild);}}voidpostorder(bitree*root)//后序遍历{bitree*p;p=root;if(p!=NULL){postorder(p->lchild);postorder(p->rchild);cout<data<<“”;}}voidmain(){bitree*root;root=create();//建立二叉链表cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}//用非递归实现二叉树的3种遍历,部分遍历程序如下:voidpreorderl(bitree*root)//先序遍历{bitree*p,*s[100];//s为堆栈inttop=0;//top为栈顶指针p=root;while((p!=NULL)||(top>0)){while(p!=NULL){cout<data<<””;s[++top]=p;//进栈p=p->lchild;}p=s[top--];//退栈p=p->rchild;}}voidinorderl(bitree*root)//中序遍历{bitree*p,*s[100];inttop=0;p=root;while((p!=NULL)Il(top>O)){while(p!=NULL){s[++top]=p;p=p->lchild;}{p=s[top--];cout<data<<“”;p=p->rchild;}}}voidpostorderl(bitree*root)//后序遍历(bitree*p*sl[100];ints2[100],top=0,b;p=root;do{while(p!=NULL){s1[top]=p;s2[top++]=0;p=p->lchild;)if(top>0)(b=s2[--top];p=s1[top];if(b==0){sl[top]=p;s2[top++]=l;p=p->rchild;}else{cout<data<<””;p=NULL;}}}while(top>0);}//建立二叉链表参考前述程序//按照层次遍历二叉链表#includetypedefcharelemtype;structbitree{elemtypedata;//结点信息bitree*lchild,*rchild;//左右孩子};//按层次遍历二叉树(建立二叉链表同前)voidlorder(bitree*t){bitreeq[100],*p;//q代表队列intf,r;//f,r类似于队列头、尾指针q[1]=t;f=r=l;cout<<”按层次遍历二叉树的结果为:”;whileif<==r){p=q[f];f++;//出队cout<data<<””;if(P->lchild!=NULL){r++;q[r]=p->lchild;}//入队ifp->rchild!=NULLl.{r++;q[r]=p->rchild;}//入队}cout<}voidmain(){bitree*t;t=create0;//建立二叉链表lorder(t);//:按层次遍历二叉树}//将二叉树的左右子树进行交换voidleftOright(bitree*r){bitree*root=r;if(root!=NULL){leftOright(root->lchild);leftOright(root->rchild);bitree*t=root->lchild;root->lchild=root->rchild;root->rchild=t;}}//主函数voidmain(){bitree*root;root=create();//建立二叉链表cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cin>>ch;
while(ch!
=’#’)
{s==NULL;
if(ch!
=’,’)
{s=newbitree;
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
q[rear]=s;//进队
if(rear==1)root=s;
else
{if((s!
=NULL)&&(q[front]!
=NULL))
{if(rear%2==0)q[front]->lchild=s;
elseq[front]->rchid=s;}
if(rear%2==1)front++;//出队
cin>>ch;}
returnroot:
voidpreorder(bitree*root)//先序遍历
{bitree*p;
p=root;
if(p!
=NULL)
{cout<data<<””;
preorder(p->lchild);
preorder(p->rchild);
}}
voidinorderl(bitree*root)//中序遍历
{
inorder(p->lchild);
cout<data<<“”;
inorder(p->rchild);
voidpostorder(bitree*root)//后序遍历
postorder(p->lchild);
postorder(p->rchild);
cout<data<<“”;
voidmain()
{bitree*root;
root=create();//建立二叉链表
cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}//用非递归实现二叉树的3种遍历,部分遍历程序如下:voidpreorderl(bitree*root)//先序遍历{bitree*p,*s[100];//s为堆栈inttop=0;//top为栈顶指针p=root;while((p!=NULL)||(top>0)){while(p!=NULL){cout<data<<””;s[++top]=p;//进栈p=p->lchild;}p=s[top--];//退栈p=p->rchild;}}voidinorderl(bitree*root)//中序遍历{bitree*p,*s[100];inttop=0;p=root;while((p!=NULL)Il(top>O)){while(p!=NULL){s[++top]=p;p=p->lchild;}{p=s[top--];cout<data<<“”;p=p->rchild;}}}voidpostorderl(bitree*root)//后序遍历(bitree*p*sl[100];ints2[100],top=0,b;p=root;do{while(p!=NULL){s1[top]=p;s2[top++]=0;p=p->lchild;)if(top>0)(b=s2[--top];p=s1[top];if(b==0){sl[top]=p;s2[top++]=l;p=p->rchild;}else{cout<data<<””;p=NULL;}}}while(top>0);}//建立二叉链表参考前述程序//按照层次遍历二叉链表#includetypedefcharelemtype;structbitree{elemtypedata;//结点信息bitree*lchild,*rchild;//左右孩子};//按层次遍历二叉树(建立二叉链表同前)voidlorder(bitree*t){bitreeq[100],*p;//q代表队列intf,r;//f,r类似于队列头、尾指针q[1]=t;f=r=l;cout<<”按层次遍历二叉树的结果为:”;whileif<==r){p=q[f];f++;//出队cout<data<<””;if(P->lchild!=NULL){r++;q[r]=p->lchild;}//入队ifp->rchild!=NULLl.{r++;q[r]=p->rchild;}//入队}cout<}voidmain(){bitree*t;t=create0;//建立二叉链表lorder(t);//:按层次遍历二叉树}//将二叉树的左右子树进行交换voidleftOright(bitree*r){bitree*root=r;if(root!=NULL){leftOright(root->lchild);leftOright(root->rchild);bitree*t=root->lchild;root->lchild=root->rchild;root->rchild=t;}}//主函数voidmain(){bitree*root;root=create();//建立二叉链表cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}//用非递归实现二叉树的3种遍历,部分遍历程序如下:voidpreorderl(bitree*root)//先序遍历{bitree*p,*s[100];//s为堆栈inttop=0;//top为栈顶指针p=root;while((p!=NULL)||(top>0)){while(p!=NULL){cout<data<<””;s[++top]=p;//进栈p=p->lchild;}p=s[top--];//退栈p=p->rchild;}}voidinorderl(bitree*root)//中序遍历{bitree*p,*s[100];inttop=0;p=root;while((p!=NULL)Il(top>O)){while(p!=NULL){s[++top]=p;p=p->lchild;}{p=s[top--];cout<data<<“”;p=p->rchild;}}}voidpostorderl(bitree*root)//后序遍历(bitree*p*sl[100];ints2[100],top=0,b;p=root;do{while(p!=NULL){s1[top]=p;s2[top++]=0;p=p->lchild;)if(top>0)(b=s2[--top];p=s1[top];if(b==0){sl[top]=p;s2[top++]=l;p=p->rchild;}else{cout<data<<””;p=NULL;}}}while(top>0);}//建立二叉链表参考前述程序//按照层次遍历二叉链表#includetypedefcharelemtype;structbitree{elemtypedata;//结点信息bitree*lchild,*rchild;//左右孩子};//按层次遍历二叉树(建立二叉链表同前)voidlorder(bitree*t){bitreeq[100],*p;//q代表队列intf,r;//f,r类似于队列头、尾指针q[1]=t;f=r=l;cout<<”按层次遍历二叉树的结果为:”;whileif<==r){p=q[f];f++;//出队cout<data<<””;if(P->lchild!=NULL){r++;q[r]=p->lchild;}//入队ifp->rchild!=NULLl.{r++;q[r]=p->rchild;}//入队}cout<}voidmain(){bitree*t;t=create0;//建立二叉链表lorder(t);//:按层次遍历二叉树}//将二叉树的左右子树进行交换voidleftOright(bitree*r){bitree*root=r;if(root!=NULL){leftOright(root->lchild);leftOright(root->rchild);bitree*t=root->lchild;root->lchild=root->rchild;root->rchild=t;}}//主函数voidmain(){bitree*root;root=create();//建立二叉链表cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}//用非递归实现二叉树的3种遍历,部分遍历程序如下:voidpreorderl(bitree*root)//先序遍历{bitree*p,*s[100];//s为堆栈inttop=0;//top为栈顶指针p=root;while((p!=NULL)||(top>0)){while(p!=NULL){cout<data<<””;s[++top]=p;//进栈p=p->lchild;}p=s[top--];//退栈p=p->rchild;}}voidinorderl(bitree*root)//中序遍历{bitree*p,*s[100];inttop=0;p=root;while((p!=NULL)Il(top>O)){while(p!=NULL){s[++top]=p;p=p->lchild;}{p=s[top--];cout<data<<“”;p=p->rchild;}}}voidpostorderl(bitree*root)//后序遍历(bitree*p*sl[100];ints2[100],top=0,b;p=root;do{while(p!=NULL){s1[top]=p;s2[top++]=0;p=p->lchild;)if(top>0)(b=s2[--top];p=s1[top];if(b==0){sl[top]=p;s2[top++]=l;p=p->rchild;}else{cout<data<<””;p=NULL;}}}while(top>0);}//建立二叉链表参考前述程序//按照层次遍历二叉链表#includetypedefcharelemtype;structbitree{elemtypedata;//结点信息bitree*lchild,*rchild;//左右孩子};//按层次遍历二叉树(建立二叉链表同前)voidlorder(bitree*t){bitreeq[100],*p;//q代表队列intf,r;//f,r类似于队列头、尾指针q[1]=t;f=r=l;cout<<”按层次遍历二叉树的结果为:”;whileif<==r){p=q[f];f++;//出队cout<data<<””;if(P->lchild!=NULL){r++;q[r]=p->lchild;}//入队ifp->rchild!=NULLl.{r++;q[r]=p->rchild;}//入队}cout<}voidmain(){bitree*t;t=create0;//建立二叉链表lorder(t);//:按层次遍历二叉树}//将二叉树的左右子树进行交换voidleftOright(bitree*r){bitree*root=r;if(root!=NULL){leftOright(root->lchild);leftOright(root->rchild);bitree*t=root->lchild;root->lchild=root->rchild;root->rchild=t;}}//主函数voidmain(){bitree*root;root=create();//建立二叉链表cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}//用非递归实现二叉树的3种遍历,部分遍历程序如下:voidpreorderl(bitree*root)//先序遍历{bitree*p,*s[100];//s为堆栈inttop=0;//top为栈顶指针p=root;while((p!=NULL)||(top>0)){while(p!=NULL){cout<data<<””;s[++top]=p;//进栈p=p->lchild;}p=s[top--];//退栈p=p->rchild;}}voidinorderl(bitree*root)//中序遍历{bitree*p,*s[100];inttop=0;p=root;while((p!=NULL)Il(top>O)){while(p!=NULL){s[++top]=p;p=p->lchild;}{p=s[top--];cout<data<<“”;p=p->rchild;}}}voidpostorderl(bitree*root)//后序遍历(bitree*p*sl[100];ints2[100],top=0,b;p=root;do{while(p!=NULL){s1[top]=p;s2[top++]=0;p=p->lchild;)if(top>0)(b=s2[--top];p=s1[top];if(b==0){sl[top]=p;s2[top++]=l;p=p->rchild;}else{cout<data<<””;p=NULL;}}}while(top>0);}//建立二叉链表参考前述程序//按照层次遍历二叉链表#includetypedefcharelemtype;structbitree{elemtypedata;//结点信息bitree*lchild,*rchild;//左右孩子};//按层次遍历二叉树(建立二叉链表同前)voidlorder(bitree*t){bitreeq[100],*p;//q代表队列intf,r;//f,r类似于队列头、尾指针q[1]=t;f=r=l;cout<<”按层次遍历二叉树的结果为:”;whileif<==r){p=q[f];f++;//出队cout<data<<””;if(P->lchild!=NULL){r++;q[r]=p->lchild;}//入队ifp->rchild!=NULLl.{r++;q[r]=p->rchild;}//入队}cout<}voidmain(){bitree*t;t=create0;//建立二叉链表lorder(t);//:按层次遍历二叉树}//将二叉树的左右子树进行交换voidleftOright(bitree*r){bitree*root=r;if(root!=NULL){leftOright(root->lchild);leftOright(root->rchild);bitree*t=root->lchild;root->lchild=root->rchild;root->rchild=t;}}//主函数voidmain(){bitree*root;root=create();//建立二叉链表cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cout<<”后序遍历的结果”<postorder(root);cout<}//用非递归实现二叉树的3种遍历,部分遍历程序如下:voidpreorderl(bitree*root)//先序遍历{bitree*p,*s[100];//s为堆栈inttop=0;//top为栈顶指针p=root;while((p!=NULL)||(top>0)){while(p!=NULL){cout<data<<””;s[++top]=p;//进栈p=p->lchild;}p=s[top--];//退栈p=p->rchild;}}voidinorderl(bitree*root)//中序遍历{bitree*p,*s[100];inttop=0;p=root;while((p!=NULL)Il(top>O)){while(p!=NULL){s[++top]=p;p=p->lchild;}{p=s[top--];cout<data<<“”;p=p->rchild;}}}voidpostorderl(bitree*root)//后序遍历(bitree*p*sl[100];ints2[100],top=0,b;p=root;do{while(p!=NULL){s1[top]=p;s2[top++]=0;p=p->lchild;)if(top>0)(b=s2[--top];p=s1[top];if(b==0){sl[top]=p;s2[top++]=l;p=p->rchild;}else{cout<data<<””;p=NULL;}}}while(top>0);}//建立二叉链表参考前述程序//按照层次遍历二叉链表#includetypedefcharelemtype;structbitree{elemtypedata;//结点信息bitree*lchild,*rchild;//左右孩子};//按层次遍历二叉树(建立二叉链表同前)voidlorder(bitree*t){bitreeq[100],*p;//q代表队列intf,r;//f,r类似于队列头、尾指针q[1]=t;f=r=l;cout<<”按层次遍历二叉树的结果为:”;whileif<==r){p=q[f];f++;//出队cout<data<<””;if(P->lchild!=NULL){r++;q[r]=p->lchild;}//入队ifp->rchild!=NULLl.{r++;q[r]=p->rchild;}//入队}cout<}voidmain(){bitree*t;t=create0;//建立二叉链表lorder(t);//:按层次遍历二叉树}//将二叉树的左右子树进行交换voidleftOright(bitree*r){bitree*root=r;if(root!=NULL){leftOright(root->lchild);leftOright(root->rchild);bitree*t=root->lchild;root->lchild=root->rchild;root->rchild=t;}}//主函数voidmain(){bitree*root;root=create();//建立二叉链表cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
postorder(root);cout<}//用非递归实现二叉树的3种遍历,部分遍历程序如下:voidpreorderl(bitree*root)//先序遍历{bitree*p,*s[100];//s为堆栈inttop=0;//top为栈顶指针p=root;while((p!=NULL)||(top>0)){while(p!=NULL){cout<data<<””;s[++top]=p;//进栈p=p->lchild;}p=s[top--];//退栈p=p->rchild;}}voidinorderl(bitree*root)//中序遍历{bitree*p,*s[100];inttop=0;p=root;while((p!=NULL)Il(top>O)){while(p!=NULL){s[++top]=p;p=p->lchild;}{p=s[top--];cout<data<<“”;p=p->rchild;}}}voidpostorderl(bitree*root)//后序遍历(bitree*p*sl[100];ints2[100],top=0,b;p=root;do{while(p!=NULL){s1[top]=p;s2[top++]=0;p=p->lchild;)if(top>0)(b=s2[--top];p=s1[top];if(b==0){sl[top]=p;s2[top++]=l;p=p->rchild;}else{cout<data<<””;p=NULL;}}}while(top>0);}//建立二叉链表参考前述程序//按照层次遍历二叉链表#includetypedefcharelemtype;structbitree{elemtypedata;//结点信息bitree*lchild,*rchild;//左右孩子};//按层次遍历二叉树(建立二叉链表同前)voidlorder(bitree*t){bitreeq[100],*p;//q代表队列intf,r;//f,r类似于队列头、尾指针q[1]=t;f=r=l;cout<<”按层次遍历二叉树的结果为:”;whileif<==r){p=q[f];f++;//出队cout<data<<””;if(P->lchild!=NULL){r++;q[r]=p->lchild;}//入队ifp->rchild!=NULLl.{r++;q[r]=p->rchild;}//入队}cout<}voidmain(){bitree*t;t=create0;//建立二叉链表lorder(t);//:按层次遍历二叉树}//将二叉树的左右子树进行交换voidleftOright(bitree*r){bitree*root=r;if(root!=NULL){leftOright(root->lchild);leftOright(root->rchild);bitree*t=root->lchild;root->lchild=root->rchild;root->rchild=t;}}//主函数voidmain(){bitree*root;root=create();//建立二叉链表cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
//用非递归实现二叉树的3种遍历,部分遍历程序如下:
voidpreorderl(bitree*root)//先序遍历
{bitree*p,*s[100];//s为堆栈
inttop=0;//top为栈顶指针
while((p!
=NULL)||(top>0))
{while(p!
s[++top]=p;//进栈
p=p->lchild;
p=s[top--];//退栈
p=p->rchild;
{bitree*p,*s[100];
inttop=0;
=NULL)Il(top>O))
{s[++top]=p;p=p->lchild;}
{p=s[top--];
p=p->rchild;
}
voidpostorderl(bitree*root)//后序遍历
(bitree*p*sl[100];
ints2[100],top=0,b;
do
{s1[top]=p;s2[top++]=0;
p=p->lchild;)
if(top>0)
(b=s2[--top];
p=s1[top];
if(b==0)
{sl[top]=p;s2[top++]=l;
p=p->rchild;}
{cout<data<<””;
p=NULL;
}while(top>0);
//建立二叉链表参考前述程序
//按照层次遍历二叉链表
//按层次遍历二叉树(建立二叉链表同前)
voidlorder(bitree*t)
{bitreeq[100],*p;//q代表队列
intf,r;//f,r类似于队列头、尾指针
q[1]=t;f=r=l;
cout<<”按层次遍历二叉树的结果为:
”;
whileif<==r)
{p=q[f];f++;//出队
cout<data<<””;
if(P->lchild!
{r++;q[r]=p->lchild;}//入队
ifp->rchild!
=NULLl.
{r++;q[r]=p->rchild;}//入队
cout<}voidmain(){bitree*t;t=create0;//建立二叉链表lorder(t);//:按层次遍历二叉树}//将二叉树的左右子树进行交换voidleftOright(bitree*r){bitree*root=r;if(root!=NULL){leftOright(root->lchild);leftOright(root->rchild);bitree*t=root->lchild;root->lchild=root->rchild;root->rchild=t;}}//主函数voidmain(){bitree*root;root=create();//建立二叉链表cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
{bitree*t;
t=create0;//建立二叉链表
lorder(t);//:
按层次遍历二叉树
//将二叉树的左右子树进行交换
voidleftOright(bitree*r)
{bitree*root=r;
if(root!
{leftOright(root->lchild);
leftOright(root->rchild);
bitree*t=root->lchild;
root->lchild=root->rchild;
root->rchild=t;
//主函数
cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cout<<”后序遍历的结果”<postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
postorder(root);cout<}leftTOright(root);cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
leftTOright(root);
cout<cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cout<<”先序遍历的结果”<preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
preorder(root);cout<cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cout<<”中序遍历的结果”<inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
inorder(root);cout<cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
cout<<”后序遍历的结果”<postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
postorder(root);cout<}实验四图形结构以下为部分参考程序://邻接矩阵的存储结构typedefstruct{intvertex;}node;typedefstruct{intadj;}arc;typedefstruct{nodenode[maxnode];arcarcs[maxnode][maxnode];}graph;//实现插入、删除边的操作voidins_arc(graph*g,intv,intw){g->arcs[v][w].adj=l;return;}voiddel_arc(graph*g,intv,intw){g->arc[v][w].adj=0;retum;}//内容1参考程序#definemaxnode40#definenull0#includetypedefstructst_arc{intadjvex;intweight;structst_arc*nextrac;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边{arcnode*rl,*r2;rl=g[v].frrstarc;r2=rl;while(r1!=null&&r1->adjvex!=w){r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)//当只有一个边结点时g[v].firstarc=r1->nextarc;elser2->nextarc=r1->nextarc;//有多个边结点时rl=g[w].firstarc;r2=rl;while(r1!=null&&r1->adjvex!=v)//在以v为头结点的链表中,删除相应的//边结点{r2=rl;rl=rl->nextarc;}if(rl==null){printf(”noedgev-w.”);return;}elseif(r1==r2)g[w].firstarc=rl->nextarc;elser2->nextarc=r1->nextarc;}voidprint(vernodeg[],intn)//打印图中各结点的结构{arcnode*q;inti;printf(”adjacencylistofthegraph:\n”);for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
实验四图形结构
//邻接矩阵的存储结构
typedefstruct
{intvertex;
}node;
{intadj;
}arc;
{nodenode[maxnode];
arcarcs[maxnode][maxnode];
}graph;
//实现插入、删除边的操作
voidins_arc(graph*g,intv,intw)
{g->arcs[v][w].adj=l;
return;
voiddel_arc(graph*g,intv,intw)
{g->arc[v][w].adj=0;
retum;
//内容1参考程序
#definemaxnode40
#definenull0
typedefstructst_arc
{intadjvex;
intweight;
structst_arc*nextrac;
}arcnode;
structst_arc*firstarc;
}vernode;
typedefvernodeadjlist[maxnode];
voiddel_arc(vernodeg[],intv,intw)//删除从顶点v到顶点w的边
{arcnode*rl,*r2;
rl=g[v].frrstarc;
r2=rl;
while(r1!
=null&&r1->adjvex!
=w)
{r2=rl;
rl=rl->nextarc;
if(rl==null)
{printf(”noedgev-w.”);
if(r1==r2)//当只有一个边结点时
g[v].firstarc=r1->nextarc;
r2->nextarc=r1->nextarc;//有多个边结点时
rl=g[w].firstarc;
=v)//在以v为头结点的链表中,删除相应的
//边结点
{r2=rl;
rl=rl->nextarc;
if(rl==null)
{printf(”noedgev-w.”);
if(r1==r2)
g[w].firstarc=rl->nextarc;
r2->nextarc=r1->nextarc;
voidprint(vernodeg[],intn)//打印图中各结点的结构
{arcnode*q;
inti;
printf(”adjacencylistofthegraph:
\n”);
for(i=0;i{printf(”\t%d\t”,i);printf(”%d\t”,g[i].vertex);q=g[i].firstarc;while(q!=null){printf(”%d\t”,q->adjvex);printf(”%d\t”,q->weight);q=q->nextarc;}printf(”\n”);}}main(){inti,j,n,k,w,v;arcnode*p,*q;adjlistg;printf(”Inputnode:”);//输入图中顶点个数scanf(”%d”,&n);for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
{printf(”\t%d\t”,i);
printf(”%d\t”,g[i].vertex);
q=g[i].firstarc;
while(q!
=null)
{printf(”%d\t”,q->adjvex);
printf(”%d\t”,q->weight);
q=q->nextarc;
printf(”\n”);
main()
{inti,j,n,k,w,v;
arcnode*p,*q;
adjlistg;
printf(”Inputnode:
”);//输入图中顶点个数
scanf(”%d”,&n);
for(k=0;k{printf(”node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;//对顺序存储部分初始化}for(;;)//输入各边,并将对应的边结点插入到链表中{printf(”Insertedgei-j,w:”)scanf(”%d”,&i);scanf(”%d”,&j);scanf(”%d”,&w);if(i==-1&&j==-1&&w=-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->weight=w;q->nextarc=g[i].firstarc;//头指针指向新的边结点g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->nextarc=g[i].firstarc;g[i].firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->weight=w;p->nextarc=g[j].firstarc;g[j].firstarc=p;}print(g,n);printf(”DeleteedgeV-w:”);scanf(”%d%d”,&v,&w);del_arc(g,v,w);print(g,n);}1内容2的知识要点:构造有向图链接表与无向图链接表的算法区别是:无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。//内容2的参考程序//有向图链表的存储结构为:typedefstructst_arc{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号intweight;//存放和该边有关的信息,如权值等structst_arc*nextarc;//依附于该顶点的下一个边结点的指针}arcnode;typedefstruct{intvertex;//存放与顶点有关的信息structst_arc*firstarc;}vernode;//存储顶点信息typedefvernodeadjlist[maxnode];//参考程序见内容12内容3的知识要点:深度优先搜索遍历图的算法:首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。图采用邻接表作存储结构,图的深度优先遍历次序为①→②→④→⑤→⑥→③参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。//内容3的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问voiddfs();for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
{printf(”node%d=”,k);
scanf(“%d”,&g[k].vertex);
g[k].firstarc=null;//对顺序存储部分初始化
for(;;)//输入各边,并将对应的边结点插入到链表中
{printf(”Insertedgei-j,w:
”)
scanf(”%d”,&i);
scanf(”%d”,&j);
scanf(”%d”,&w);
if(i==-1&&j==-1&&w=-1)
break;
q=(arcnode*)malloc(sizeof(arcnode));
q->adjvex=j;
q->weight=w;
q->nextarc=g[i].firstarc;//头指针指向新的边结点
g[i].firstarc=q;
p=(arcnode*)malloc(sizeof(arcnode));
p->nextarc=g[i].firstarc;
p->adjvex=i;
p->weight=w;
p->nextarc=g[j].firstarc;
g[j].firstarc=p;
print(g,n);
printf(”DeleteedgeV-w:
”);
scanf(”%d%d”,&v,&w);
del_arc(g,v,w);
1内容2的知识要点:
构造有向图链接表与无向图链接表的算法区别是:
无向图两结点无向对偶,因而邻接表有一定的对偶性;有向图两结点间有向无对偶关系,因而建立邻接表时应根据输入的顶点及边的有向关系建立(当箭头方向离开结点为有关,当箭头方向指向结点为无关)。
//内容2的参考程序
//有向图链表的存储结构为:
{intaajvex;//存放依附于该边的另一顶点在一维数组中的序号
intweight;//存放和该边有关的信息,如权值等
structst_arc*nextarc;//依附于该顶点的下一个边结点的指针
{intvertex;//存放与顶点有关的信息
}vernode;//存储顶点信息
//参考程序见内容1
2内容3的知识要点:
深度优先搜索遍历图的算法:
首先访问指定的起始顶点v0,从vo出发,访问vo的一个未被访问过的邻接顶点w1,再从w1出发,访问w1的一个未被访问的顶点w2,然后从w2出发,访问w2的一个未被访问的邻接顶点w3,依此类推,直到一个所有邻接点都被访问过为止。
图采用邻接表作存储结构,图的深度优先遍历次序为
①→②→④→⑤→⑥→③
参考程序运行过程中,深度优先遍历时指针p的移动方向示意如图下所示,图中p1、p2、p3、p4、p5和p6为深度优先遍历图的各结点时,指针p的移动次序。
//内容3的参考程序
typedefstructst_arc//定义结构体
{intadivex;
intweigh;
structst_arc*nextarc;
voidtrave(adjlistg,intn)//采用邻接表作存储结构的/深度优先遍历
{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问
voiddfs();
for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
visited[i]=0;//数组初始化
for(i=0;iif(visited[i]==0)dfs(g,i,visted);}voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。{arcnode*p;intw;visited[k]=1;printf(%d,g[k],vertex);p=g[k],firstarc;while(p!=null){w=p->adjvex;if(visited[w]==0)dfs(g,w,visited);p=p->nextarc;}}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
if(visited[i]==0)
dfs(g,i,visted);
voiddfs(adjlistg,intk,intvisited[])//从顶点k出发,深度优先遍历图g。
{arcnode*p;
intw;
visited[k]=1;
printf(%d,g[k],vertex);
p=g[k],firstarc;
while(p!
{w=p->adjvex;
if(visited[w]==0)
dfs(g,w,visited);
p=p->nextarc;
{inti,j,n,k,v;
arcnode*p,*q;
printf(“Inputnode:
scanf(“%d”,&n);
for(k=0;k{printf(“node%d=”,k);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“dfs:”);trave(g,n);//深度优先遍历图。printf(“\n”);}3内容4的知识要点:广度优先遍历图的算法:首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。图的广度优先遍历次序为:①→②→③→④→⑤→⑥参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。//内容4的参考程序#definemaxnode40#definenull0#includetypedefstructst_arc//定义结构体{intadivex;intweigh;structst_arc*nextarc;}arcnode;typedefstruct{intvertex;structst_arc*firstarc;}vernode;typedefvernodeadjlist[maxnode];typedefstruct{intqueue[maxnode];intfront;intrear;}qqtype;voidintiate(qqtype*q)//初始化队列{q->front=-1;q->rear=-1;}intenter(qqtype*q,intx)//将X入队{if(q->rear==maxnode-1)return(-1);else{q->rear++;q->queue[q->rear]=x;return(0);}}intdelete(qqtype*q)//出队{if(q->front==q->rear)return(null);else{q->front++;return(q->queue[q->front]);}}intemtype(qqtype*q){if(q->front==q->rear)return(-1);return(0);}voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历{arcnode*p;intw;intiate(g);visited[k]=1;//访问点从k开始,并把它入队。printf(“%d”,g[p->adjvex].vertex);enter(g,k);while(emptye(g)==0){w=delete(g);p=g[w].firstarc;while(p!=null){if(visited[p->adjves]==0){printf(“%d”,g[p->adjvex].verte);visited[p->adjvex]=1;enter(g.p->adjvex);}p=p->nextarc;}}}voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
{printf(“node%d=”,k);
g[k].firstarc=null;
for(;;)
{printf(“Insertedgei-j:
scanf(“%d”,&i);
scanfi(“%d”,&j);
if(i==-1&&j==-1)
q->nextarc=g[i].firstarc;
g[i]firstarc=q;
p->nextarc=g[i].firstarc;
g[i]_firstarc=p;}
printf(“dfs:
trave(g,n);//深度优先遍历图。
printf(“\n”);
3内容4的知识要点:
广度优先遍历图的算法:
首先访问指定的起始顶点v0,从v0出发,访问v0的所有未被访问的邻接顶点w1,w2,…,wk,然后再依次从w1,w2,…,wk出发,访问所有未被访问过的邻接顶点,依此类推,直到图中所有未被访问过的邻接顶点都被访问过为止。
根据广度优点遍历的规则,在其算法实现中,借助一个队列g-queue来存放已被访问过的顶点。
从指定顶点开始,每访问一个顶点,就将它入队并排在队尾,然后从队头取出一个顶点,访问该顶点的所有未被访问的邻接点,依此类推,直至队列为空且图中结点均被访问过为止。
图的广度优先遍历次序为:
①→②→③→④→⑤→⑥
参考程序运行过程中,广度优先搜索时指针p的移动示意如下图所示,图中片p1、p2、p3、p4、p5和p6为广度优先遍历图的各结点指针p的移动次序。
//内容4的参考程序
{intqueue[maxnode];
intfront;
intrear;
}qqtype;
voidintiate(qqtype*q)//初始化队列
{q->front=-1;
q->rear=-1;
intenter(qqtype*q,intx)//将X入队
{if(q->rear==maxnode-1)
return(-1);
{q->rear++;
q->queue[q->rear]=x;
return(0);
intdelete(qqtype*q)//出队
{if(q->front==q->rear)
return(null);
{q->front++;
return(q->queue[q->front]);
intemtype(qqtype*q)
voidbfs(adjlistg,intk,intvisited[])//从顶点k出发进行广度优先遍历
intiate(g);
visited[k]=1;//访问点从k开始,并把它入队。
printf(“%d”,g[p->adjvex].vertex);
enter(g,k);
while(emptye(g)==0)
{w=delete(g);
p=g[w].firstarc;
{if(visited[p->adjves]==0)
{printf(“%d”,g[p->adjvex].verte);
visited[p->adjvex]=1;
enter(g.p->adjvex);
p=p->nextarc;
voidtrave(adjlistg,intn)//图采用邻接表存储的广度优先遍历
{inti,visited[maxnode];//数组visited标志图中的定点是否已被访问。
for(i=0;ivisited[i]=0;//数组初始化for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
for(i=0;iif(visited[i]==0)bfs(g,i,visted);}main(){inti,j,n,k,v;arcnode*p,*q;adjlistg;printf(“Inputnode:”);scanf(“%d”,&n);for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
bfs(g,i,visted);
for(k=0;k{printf(“node%d=”,k);scanf(“%d”,&g[k].vertex);g[k].firstarc=null;}for(;;){printf(“Insertedgei-j:”);scanf(“%d”,&i);scanfi(“%d”,&j);if(i==-1&&j==-1)break;q=(arcnode*)malloc(sizeof(arcnode));q->adjvex=j;q->nextarc=g[i].firstarc;g[i]firstarc=q;p=(arcnode*)malloc(sizeof(arcnode));p->adjvex=i;p->nextarc=g[i].firstarc;g[i]_firstarc=p;}printf(“bfs:”);trave(g,n);//广度优先遍历图。printf(“\n”);} 实验五查找以下为部分参考程序://参考程序#include#defineKEYTYPE
scanf(“%d”,&g[k].vertex);
g[i]_firstarc=p;
printf(“bfs:
trave(g,n);//广度优先遍历图。
实验五查找
//参考程序
#defineKEYTYPE
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1