A. c语言图的遍历,邻接表存储,深度,广度优先遍历
(1) 图的建立,按采用邻接表作为存储结构。
(2) 从指定顶点出发进行深度优先搜索遍历。
(3) 从指定顶点出发进行广度优先搜索遍历。
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"
#define MAX_INT 1000
#define MAX_VERTEX_NUM 20
#define MAX_QUEUE_NUMBER 20
typedef struct ArcNode
{
int adjvex;
double adj;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VexNode
{
char szName[40];
ArcNode *firstarc;
}VexNode,AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vexs;
int vexnum,arcnum;
}Net;
//定义队列
typedef struct{
int *elem;
int front, rear;
}Queue;
void InitQueue(Queue &Q)
{
Q.elem = new int[MAX_QUEUE_NUMBER];
Q.front = Q.rear = 0;
}
int EmptyQueue(Queue Q)
{
if(Q.front==Q.rear)
return 0;
else
return 1;
}
void DestroyQueue(Queue &Q){
delete []Q.elem;
Q.front = Q.rear = 0;
}
void EnterQueue(Queue &Q, int e)
{
if((Q.rear + 1)%MAX_QUEUE_NUMBER != Q.front)
Q.elem[Q.rear ] = e;
else
printf("队列满!\n");
Q.rear = (Q.rear + 1)%MAX_QUEUE_NUMBER;
}
void LeaveQueue(Queue &Q, int &e)
{
if(Q.rear != Q.front)
e = Q.elem[Q.front];
else
printf("队列空!\n");
Q.front = (Q.front+1)%MAX_QUEUE_NUMBER;
}
int LocateVex(Net ga,char *name)
{
int i;
for(i=0;i<ga.vexnum;i++)
if(strcmp(name,ga.vexs[i].szName)==0)
return i;
return -1;
}
void crt_net(Net &ga)
{
ArcNode *p;
char name1[40],name2[40];
int i,j,k;
double w;
printf("请输入顶点数和弧数:");
scanf("%d%d",&ga.vexnum,&ga.arcnum);
printf("请依次输入顶点名:\n");
for(i=0;i<ga.vexnum;i++)
{
scanf("%s",ga.vexs[i].szName);
ga.vexs[i].firstarc=NULL;
}
for(k=0;k<ga.arcnum;k++)
{
printf("请输入相邻的两个定点和权值:");
scanf("%s%s%lf",name1,name2,&w);
i=LocateVex(ga,name1);
j=LocateVex(ga,name2);
p=new ArcNode;
p->adjvex=j;
p->adj=w;
p->nextarc=ga.vexs[i].firstarc;
ga.vexs[i].firstarc=p;
}
}
void DFS(Net ga,char *name,int *visited)
{
int v,w;
ArcNode *p;
v=LocateVex(ga,name);
visited[v]=1;
printf("%s ",ga.vexs[v].szName);
p=ga.vexs[v].firstarc;
while(p!=NULL)
{
w=p->adjvex;
if(visited[w]==0)
DFS(ga,ga.vexs[w].szName,visited);
p=p->nextarc;
}
}
void DFSTravel(Net ga,char *name)
{
int v,k=0;
int visited[20];
for(v=0;v<ga.vexnum;v++)
visited[v]=0;
for(v=LocateVex(ga,name);k!=2;v=(v+1)%(ga.vexnum-1))
{
if(v+1==LocateVex(ga,name))
k++;
if(visited[v]==0)
DFS(ga,ga.vexs[v].szName,visited);
}
}
void BFSTravel(Net ga,char *name)
{
ArcNode *p;
int v,w,u,k=0;
Queue Q;
int visited[20];
for(v=0;v<ga.vexnum;v++)
visited[v]=0;
InitQueue(Q);
for(v=LocateVex(ga,name);k!=2;v=(v+1)%(ga.vexnum-1))
{
if(v+1==LocateVex(ga,name))
k++;
if(visited[v]==0)
{
visited[v]=1;
printf("%s ",ga.vexs[v].szName);
EnterQueue(Q,v);
while(EmptyQueue(Q)!=0)
{
LeaveQueue(Q,u);
p=ga.vexs[u].firstarc;
while(p!=NULL)
{
w=p->adjvex;
if(visited[w]==0)
{
printf("%s ",ga.vexs[w].szName);
visited[w]=1;
EnterQueue(Q,w);
}
p=p->nextarc;
}
}
}
}
}
void main()
{
char name[40];
Net ga;
crt_net(ga);
printf("请输入深度优先遍历开始点的名:");
scanf("%s",name);
printf("深度优先遍历:");
DFSTravel(ga,name);
printf("\n");
printf("请输入广度优先遍历开始点的名:");
scanf("%s",name);
printf("广度优先遍历:");
BFSTravel(ga,name);
printf("\n");
}
B. c语言关于图的广度优先遍历
深度优先是沿着一条路走到底,走不通了或到头了,再回溯,再搜索。而广搜是先搜离得最近的,再慢慢搜索远的,队列就是按顺序存,所以开头存的近的,末尾存远的,说白了队列就是从近到远保存数据的,说的不好,希望对你会点帮助。
C. 求一个C语言编程,图的遍历,深度优先和广度优先搜索的程序。要浅显易懂的~~~~
给你一个作为参考吧
#include <iostream>
#define INFINITY 32767
#define MAX_VEX 20 //最大顶点个数
#define QUEUE_SIZE (MAX_VEX+1) //队列长度
using namespace std;
bool *visited; //访问标志数组
//图的邻接矩阵存储结构
typedef struct{
char *vexs; //顶点向量
int arcs[MAX_VEX][MAX_VEX]; //邻接矩阵
int vexnum,arcnum; //图的当前顶点数和弧数
}Graph;
//队列类
class Queue{
public:
void InitQueue(){
base=(int *)malloc(QUEUE_SIZE*sizeof(int));
front=rear=0;
}
void EnQueue(int e){
base[rear]=e;
rear=(rear+1)%QUEUE_SIZE;
}
void DeQueue(int &e){
e=base[front];
front=(front+1)%QUEUE_SIZE;
}
public:
int *base;
int front;
int rear;
};
//图G中查找元素c的位置
int Locate(Graph G,char c){
for(int i=0;i<G.vexnum;i++)
if(G.vexs[i]==c) return i;
return -1;
}
//创建无向网
void CreateUDN(Graph &G){
int i,j,w,s1,s2;
char a,b,temp;
printf("输入顶点数和弧数:");
scanf("%d%d",&G.vexnum,&G.arcnum);
temp=getchar(); //接收回车
G.vexs=(char *)malloc(G.vexnum*sizeof(char)); //分配顶点数目
printf("输入%d个顶点.\n",G.vexnum);
for(i=0;i<G.vexnum;i++){ //初始化顶点
printf("输入顶点%d:",i);
scanf("%c",&G.vexs[i]);
temp=getchar(); //接收回车
}
for(i=0;i<G.vexnum;i++) //初始化邻接矩阵
for(j=0;j<G.vexnum;j++)
G.arcs[i][j]=INFINITY;
printf("输入%d条弧.\n",G.arcnum);
for(i=0;i<G.arcnum;i++){ //初始化弧
printf("输入弧%d:",i);
scanf("%c %c %d",&a,&b,&w); //输入一条边依附的顶点和权值
temp=getchar(); //接收回车
s1=Locate(G,a);
s2=Locate(G,b);
G.arcs[s1][s2]=G.arcs[s2][s1]=w;
}
}
//图G中顶点k的第一个邻接顶点
int FirstVex(Graph G,int k){
if(k>=0 && k<G.vexnum){ //k合理
for(int i=0;i<G.vexnum;i++)
if(G.arcs[k][i]!=INFINITY) return i;
}
return -1;
}
//图G中顶点i的第j个邻接顶点的下一个邻接顶点
int NextVex(Graph G,int i,int j){
if(i>=0 && i<G.vexnum && j>=0 && j<G.vexnum){ //i,j合理
for(int k=j+1;k<G.vexnum;k++)
if(G.arcs[i][k]!=INFINITY) return k;
}
return -1;
}
//深度优先遍历
void DFS(Graph G,int k){
int i;
if(k==-1){ //第一次执行DFS时,k为-1
for(i=0;i<G.vexnum;i++)
if(!visited[i]) DFS(G,i); //对尚未访问的顶点调用DFS
}
else{
visited[k]=true;
printf("%c ",G.vexs[k]); //访问第k个顶点
for(i=FirstVex(G,k);i>=0;i=NextVex(G,k,i))
if(!visited[i]) DFS(G,i); //对k的尚未访问的邻接顶点i递归调用DFS
}
}
//广度优先遍历
void BFS(Graph G){
int k;
Queue Q; //辅助队列Q
Q.InitQueue();
for(int i=0;i<G.vexnum;i++)
if(!visited[i]){ //i尚未访问
visited[i]=true;
printf("%c ",G.vexs[i]);
Q.EnQueue(i); //i入列
while(Q.front!=Q.rear){
Q.DeQueue(k); //队头元素出列并置为k
for(int w=FirstVex(G,k);w>=0;w=NextVex(G,k,w))
if(!visited[w]){ //w为k的尚未访问的邻接顶点
visited[w]=true;
printf("%c ",G.vexs[w]);
Q.EnQueue(w);
}
}
}
}
//主函数
void main(){
int i;
Graph G;
CreateUDN(G);
visited=(bool *)malloc(G.vexnum*sizeof(bool));
printf("\n广度优先遍历: ");
for(i=0;i<G.vexnum;i++)
visited[i]=false;
DFS(G,-1);
printf("\n深度优先遍历: ");
for(i=0;i<G.vexnum;i++)
visited[i]=false;
BFS(G);
printf("\n程序结束.\n");
}
D. 如何写 图的深度优先和广度优先遍历的C程序。
深度优先遍历可以用递归写,访问跟节点,然后递归遍历根节点的各个子树,注意是遍历子树不是访问孩子节点
广度优先遍历可以用队列,访问根节点,然后把根节点的各个孩子节点放入队列,每次访问一个节点之后,访问队头节点,把那个节点的儿子节点继续放入队列
E. 图的广度优先遍历的C语言程序(有头文件的)
// bo7-2.cpp 图的邻接表存储(存储结构由c7-2.h定义)的基本操作(15个)
int LocateVex(ALGraph G,VertexType u)
{ // 初始条件: 图G存在,u和G中顶点有相同特征
// 操作结果: 若G中存在顶点u,则返回该顶点在图中位置;否则返回-1
int i;
for(i=0;i<G.vexnum;++i)
if(strcmp(u,G.vertices[i].data)==0)
return i;
return -1;
}
Status CreateGraph(ALGraph &G)
{ // 采用邻接表存储结构,构造没有相关信息的图G(用一个函数构造4种图)
int i,j,k;
int w; // 权值
VertexType va,vb;
ArcNode *p;
printf("请输入图的类型(有向图:0,有向网:1,无向图:2,无向网:3): ");
scanf("%d",&G.kind);
printf("请输入图的顶点数,边数: ");
scanf("%d,%d",&G.vexnum,&G.arcnum);
printf("请输入%d个顶点的值(<%d个字符):\n",G.vexnum,MAX_NAME);
for(i=0;i<G.vexnum;++i) // 构造顶点向量
{
scanf("%s",G.vertices[i].data);
G.vertices[i].firstarc=NULL;
}
if(G.kind==1||G.kind==3) // 网
printf("请顺序输入每条弧(边)的权值、弧尾和弧头(以空格作为间隔):\n");
else // 图
printf("请顺序输入每条弧(边)的弧尾和弧头(以空格作为间隔):\n");
for(k=0;k<G.arcnum;++k) // 构造表结点链表
{
if(G.kind==1||G.kind==3) // 网
scanf("%d%s%s",&w,va,vb);
else // 图
scanf("%s%s",va,vb);
i=LocateVex(G,va); // 弧尾
j=LocateVex(G,vb); // 弧头
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
if(G.kind==1||G.kind==3) // 网
{
p->info=(int *)malloc(sizeof(int));
*(p->info)=w;
}
else
p->info=NULL; // 图
p->nextarc=G.vertices[i].firstarc; // 插在表头
G.vertices[i].firstarc=p;
if(G.kind>=2) // 无向图或网,产生第二个表结点
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=i;
if(G.kind==3) // 无向网
{
p->info=(int*)malloc(sizeof(int));
*(p->info)=w;
}
else
p->info=NULL; // 无向图
p->nextarc=G.vertices[j].firstarc; // 插在表头
G.vertices[j].firstarc=p;
}
}
return OK;
}
void DestroyGraph(ALGraph &G)
{ // 初始条件: 图G存在。操作结果: 销毁图G
int i;
ArcNode *p,*q;
G.vexnum=0;
G.arcnum=0;
for(i=0;i<G.vexnum;++i)
{
p=G.vertices[i].firstarc;
while(p)
{
q=p->nextarc;
if(G.kind%2) // 网
free(p->info);
free(p);
p=q;
}
}
}
VertexType& GetVex(ALGraph G,int v)
{ // 初始条件: 图G存在,v是G中某个顶点的序号。操作结果: 返回v的值
if(v>=G.vexnum||v<0)
exit(ERROR);
return G.vertices[v].data;
}
Status PutVex(ALGraph &G,VertexType v,VertexType value)
{ // 初始条件: 图G存在,v是G中某个顶点
// 操作结果: 对v赋新值value
int i;
i=LocateVex(G,v);
if(i>-1) // v是G的顶点
{
strcpy(G.vertices[i].data,value);
return OK;
}
return ERROR;
}
int FirstAdjVex(ALGraph G,VertexType v)
{ // 初始条件: 图G存在,v是G中某个顶点
// 操作结果: 返回v的第一个邻接顶点的序号。若顶点在G中没有邻接顶点,则返回-1
ArcNode *p;
int v1;
v1=LocateVex(G,v); // v1为顶点v在图G中的序号
p=G.vertices[v1].firstarc;
if(p)
return p->adjvex;
else
return -1;
}
int NextAdjVex(ALGraph G,VertexType v,VertexType w)
{ // 初始条件: 图G存在,v是G中某个顶点,w是v的邻接顶点
// 操作结果: 返回v的(相对于w的)下一个邻接顶点的序号。
// 若w是v的最后一个邻接点,则返回-1
ArcNode *p;
int v1,w1;
v1=LocateVex(G,v); // v1为顶点v在图G中的序号
w1=LocateVex(G,w); // w1为顶点w在图G中的序号
p=G.vertices[v1].firstarc;
while(p&&p->adjvex!=w1) // 指针p不空且所指表结点不是w
p=p->nextarc;
if(!p||!p->nextarc) // 没找到w或w是最后一个邻接点
return -1;
else // p->adjvex==w
return p->nextarc->adjvex; // 返回v的(相对于w的)下一个邻接顶点的序号
}
void InsertVex(ALGraph &G,VertexType v)
{ // 初始条件: 图G存在,v和图中顶点有相同特征
// 操作结果: 在图G中增添新顶点v(不增添与顶点相关的弧,留待InsertArc()去做)
strcpy(G.vertices[G.vexnum].data,v); // 构造新顶点向量
G.vertices[G.vexnum].firstarc=NULL;
G.vexnum++; // 图G的顶点数加1
}
Status DeleteVex(ALGraph &G,VertexType v)
{ // 初始条件: 图G存在,v是G中某个顶点
// 操作结果: 删除G中顶点v及其相关的弧
int i,j;
ArcNode *p,*q;
j=LocateVex(G,v); // j是顶点v的序号
if(j<0) // v不是图G的顶点
return ERROR;
p=G.vertices[j].firstarc; // 删除以v为出度的弧或边
while(p)
{
q=p;
p=p->nextarc;
if(G.kind%2) // 网
free(q->info);
free(q);
G.arcnum--; // 弧或边数减1
}
G.vexnum--; // 顶点数减1
for(i=j;i<G.vexnum;i++) // 顶点v后面的顶点前移
G.vertices[i]=G.vertices[i+1];
for(i=0;i<G.vexnum;i++) // 删除以v为入度的弧或边且必要时修改表结点的顶点位置值
{
p=G.vertices[i].firstarc; // 指向第1条弧或边
while(p) // 有弧
{
if(p->adjvex==j)
{
if(p==G.vertices[i].firstarc) // 待删结点是第1个结点
{
G.vertices[i].firstarc=p->nextarc;
if(G.kind%2) // 网
free(p->info);
free(p);
p=G.vertices[i].firstarc;
if(G.kind<2) // 有向
G.arcnum--; // 弧或边数减1
}
else
{
q->nextarc=p->nextarc;
if(G.kind%2) // 网
free(p->info);
free(p);
p=q->nextarc;
if(G.kind<2) // 有向
G.arcnum--; // 弧或边数减1
}
}
else
{
if(p->adjvex>j)
p->adjvex--; // 修改表结点的顶点位置值(序号)
q=p;
p=p->nextarc;
}
}
}
return OK;
}
Status InsertArc(ALGraph &G,VertexType v,VertexType w)
{ // 初始条件: 图G存在,v和w是G中两个顶点
// 操作结果: 在G中增添弧<v,w>,若G是无向的,则还增添对称弧<w,v>
ArcNode *p;
int w1,i,j;
i=LocateVex(G,v); // 弧尾或边的序号
j=LocateVex(G,w); // 弧头或边的序号
if(i<0||j<0)
return ERROR;
G.arcnum++; // 图G的弧或边的数目加1
if(G.kind%2) // 网
{
printf("请输入弧(边)%s→%s的权值: ",v,w);
scanf("%d",&w1);
}
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
if(G.kind%2) // 网
{
p->info=(int*)malloc(sizeof(int));
*(p->info)=w1;
}
else
p->info=NULL;
p->nextarc=G.vertices[i].firstarc; // 插在表头
G.vertices[i].firstarc=p;
if(G.kind>=2) // 无向,生成另一个表结点
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=i;
if(G.kind==3) // 无向网
{
p->info=(int*)malloc(sizeof(int));
*(p->info)=w1;
}
else
p->info=NULL;
p->nextarc=G.vertices[j].firstarc; // 插在表头
G.vertices[j].firstarc=p;
}
return OK;
}
Status DeleteArc(ALGraph &G,VertexType v,VertexType w)
{ // 初始条件: 图G存在,v和w是G中两个顶点
// 操作结果: 在G中删除弧<v,w>,若G是无向的,则还删除对称弧<w,v>
ArcNode *p,*q;
int i,j;
i=LocateVex(G,v); // i是顶点v(弧尾)的序号
j=LocateVex(G,w); // j是顶点w(弧头)的序号
if(i<0||j<0||i==j)
return ERROR;
p=G.vertices[i].firstarc; // p指向顶点v的第一条出弧
while(p&&p->adjvex!=j) // p不空且所指之弧不是待删除弧<v,w>
{ // p指向下一条弧
q=p;
p=p->nextarc;
}
if(p&&p->adjvex==j) // 找到弧<v,w>
{
if(p==G.vertices[i].firstarc) // p所指是第1条弧
G.vertices[i].firstarc=p->nextarc; // 指向下一条弧
else
q->nextarc=p->nextarc; // 指向下一条弧
if(G.kind%2) // 网
free(p->info);
free(p); // 释放此结点
G.arcnum--; // 弧或边数减1
}
if(G.kind>=2) // 无向,删除对称弧<w,v>
{
p=G.vertices[j].firstarc; // p指向顶点w的第一条出弧
while(p&&p->adjvex!=i) // p不空且所指之弧不是待删除弧<w,v>
{ // p指向下一条弧
q=p;
p=p->nextarc;
}
if(p&&p->adjvex==i) // 找到弧<w,v>
{
if(p==G.vertices[j].firstarc) // p所指是第1条弧
G.vertices[j].firstarc=p->nextarc; // 指向下一条弧
else
q->nextarc=p->nextarc; // 指向下一条弧
if(G.kind==3) // 无向网
free(p->info);
free(p); // 释放此结点
}
}
return OK;
}
Boolean visited[MAX_VERTEX_NUM]; // 访问标志数组(全局量)
void(*VisitFunc)(char* v); // 函数变量(全局量)
void DFS(ALGraph G,int v)
{ // 从第v个顶点出发递归地深度优先遍历图G。算法7.5
int w;
VertexType v1,w1;
strcpy(v1,GetVex(G,v));
visited[v]=TRUE; // 设置访问标志为TRUE(已访问)
VisitFunc(G.vertices[v].data); // 访问第v个顶点
for(w=FirstAdjVex(G,v1);w>=0;w=NextAdjVex(G,v1,strcpy(w1,GetVex(G,w))))
if(!visited[w])
DFS(G,w); // 对v的尚未访问的邻接点w递归调用DFS
}
void DFSTraverse(ALGraph G,void(*Visit)(char*))
{ // 对图G作深度优先遍历。算法7.4
int v;
VisitFunc=Visit; // 使用全局变量VisitFunc,使DFS不必设函数指针参数
for(v=0;v<G.vexnum;v++)
visited[v]=FALSE; // 访问标志数组初始化
for(v=0;v<G.vexnum;v++)
if(!visited[v])
DFS(G,v); // 对尚未访问的顶点调用DFS
printf("\n");
}
typedef int QElemType; // 队列类型
#include"c3-2.h"
#include"bo3-2.cpp"
void BFSTraverse(ALGraph G,void(*Visit)(char*))
{//按广度优先非递归遍历图G。使用辅助队列Q和访问标志数组visited。算法7.6
int v,u,w;
VertexType u1,w1;
LinkQueue Q;
for(v=0;v<G.vexnum;++v)
visited[v]=FALSE; // 置初值
InitQueue(Q); // 置空的辅助队列Q
for(v=0;v<G.vexnum;v++) // 如果是连通图,只v=0就遍历全图
if(!visited[v]) // v尚未访问
{
visited[v]=TRUE;
Visit(G.vertices[v].data);
EnQueue(Q,v); // v入队列
while(!QueueEmpty(Q)) // 队列不空
{
DeQueue(Q,u); // 队头元素出队并置为u
strcpy(u1,GetVex(G,u));
for(w=FirstAdjVex(G,u1);w>=0;w=NextAdjVex(G,u1,strcpy(w1,GetVex(G,w))))
if(!visited[w]) // w为u的尚未访问的邻接顶点
{
visited[w]=TRUE;
Visit(G.vertices[w].data);
EnQueue(Q,w); // w入队
}
}
}
printf("\n");
}
void Display(ALGraph G)
{ // 输出图的邻接矩阵G
int i;
ArcNode *p;
switch(G.kind)
{
case DG: printf("有向图\n");
break;
case DN: printf("有向网\n");
break;
case AG: printf("无向图\n");
break;
case AN: printf("无向网\n");
}
printf("%d个顶点:\n",G.vexnum);
for(i=0;i<G.vexnum;++i)
printf("%s ",G.vertices[i].data);
printf("\n%d条弧(边):\n",G.arcnum);
for(i=0;i<G.vexnum;i++)
{
p=G.vertices[i].firstarc;
while(p)
{
if(G.kind<=1) // 有向
{
printf("%s→%s ",G.vertices[i].data,G.vertices[p->adjvex].data);
if(G.kind==DN) // 网
printf(":%d ",*(p->info));
}
else // 无向(避免输出两次)
{
if(i<p->adjvex)
{
printf("%s-%s ",G.vertices[i].data,G.vertices[p->adjvex].data);
if(G.kind==AN) // 网
printf(":%d ",*(p->info));
}
}
p=p->nextarc;
}
printf("\n");
}
}
// c7-2.h 图的邻接表存储表示
#define MAX_VERTEX_NUM 20
enum GraphKind{DG,DN,AG,AN}; // {有向图,有向网,无向图,无向网}
struct ArcNode
{
int adjvex; // 该弧所指向的顶点的位置
ArcNode *nextarc; // 指向下一条弧的指针
InfoType *info; // 网的权值指针
}; // 表结点
typedef struct
{
VertexType data; // 顶点信息
ArcNode *firstarc; // 第一个表结点的地址,指向第一条依附该顶点的弧的指针
}VNode,AdjList[MAX_VERTEX_NUM]; // 头结点
struct ALGraph
{
AdjList vertices;
int vexnum,arcnum; // 图的当前顶点数和弧数
int kind; // 图的种类标志
};
// c3-2.h 单链队列--队列的链式存储结构
typedef struct QNode
{
QElemType data;
QNode *next;
}*QueuePtr;
struct LinkQueue
{
QueuePtr front,rear; // 队头、队尾指针
};
// bo3-2.cpp 链队列(存储结构由c3-2.h定义)的基本操作(9个)
Status InitQueue(LinkQueue &Q)
{ // 构造一个空队列Q
if(!(Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode))))
exit(OVERFLOW);
Q.front->next=NULL;
return OK;
}
Status DestroyQueue(LinkQueue &Q)
{ // 销毁队列Q(无论空否均可)
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
Status ClearQueue(LinkQueue &Q)
{ // 将Q清为空队列
QueuePtr p,q;
Q.rear=Q.front;
p=Q.front->next;
Q.front->next=NULL;
while(p)
{
q=p;
p=p->next;
free(q);
}
return OK;
}
Status QueueEmpty(LinkQueue Q)
{ // 若Q为空队列,则返回TRUE,否则返回FALSE
if(Q.front==Q.rear)
return TRUE;
else
return FALSE;
}
int QueueLength(LinkQueue Q)
{ // 求队列的长度
int i=0;
QueuePtr p;
p=Q.front;
while(Q.rear!=p)
{
i++;
p=p->next;
}
return i;
}
Status GetHead(LinkQueue Q,QElemType &e)
{ // 若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR
QueuePtr p;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;
e=p->data;
return OK;
}
Status EnQueue(LinkQueue &Q,QElemType e)
{ // 插入元素e为Q的新的队尾元素
QueuePtr p;
if(!(p=(QueuePtr)malloc(sizeof(QNode)))) // 存储分配失败
exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e)
{ // 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR
QueuePtr p;
if(Q.front==Q.rear)
return ERROR;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return OK;
}
Status QueueTraverse(LinkQueue Q,void(*vi)(QElemType))
{ // 从队头到队尾依次对队列Q中每个元素调用函数vi()。一旦vi失败,则操作失败
QueuePtr p;
p=Q.front->next;
while(p)
{
vi(p->data);
p=p->next;
}
printf("\n");
return OK;
}
F. 图的深度/广度优先遍历C语言程序
#define INFINITY 10000 //无穷大
#define MAX_VERTEX_NUM 40
#define MAX 40
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
typedef struct ArCell{
int adj;
}ArCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
char name[20];
}infotype;
typedef struct
{
infotype vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum,arcnum;
}MGraph;
int LocateVex(MGraph *G,char* v)
{ int c=-1,i;
for(i=0;i<G->vexnum;i++)
if(strcmp(v,G->vexs[i].name)==0)
{c=i;break;}
return c;
}
MGraph * CreatUDN(MGraph *G)//初始化图,接受用户输入
{
int i,j,k,w;
char v1[20],v2[20];
printf("请输入图的顶点数和弧数:");
scanf("%d,%d",&G->vexnum,&G->arcnum);
printf("请依次输入图的顶点\n");
for(i=0;i<G->vexnum;i++){
printf("G.vexs[%d] : ",i);
scanf("%s",G->vexs[i].name);
getchar();
} // 构造顶点向量
for(i=0;i<G->vexnum;i++) //初始化邻接矩阵
for(j=0;j<G->vexnum;j++)
G->arcs[i][j].adj=INFINITY;
printf("请输入一条边依附的两个顶点和权值:\n");
for(k=0;k<G->arcnum;k++) //构造邻接矩阵
{
printf("第%d条边:\n",k+1);
printf("起始结点:");
scanf("%s",v1);
printf("结束结点:");
scanf("%s",v2);
printf("边的权值:");
scanf("%d",&w);
i=LocateVex(G,v1);
j=LocateVex(G,v2); // 确定v1和v2在G中位置
if(i>=0&&j>=0){
G->arcs[i][j].adj=w;
G->arcs[j][i]=G->arcs[i][j]; //置<v1,v2>的对称弧<v2,v1>
}
}
return G;
}
int FirstAdjVex(MGraph *G,int v)
{
int i;
if(v>=0 &&v<G->vexnum)
{ //v合理
for(i=0;i<G->vexnum;i++)
if(G->arcs[v][i].adj!=INFINITY)
{return i;break;}
}
return -1;
}
void VisitFunc(MGraph *G,int v)
{
printf("%s ",G->vexs[v].name);
}
int NextAdjVex(MGraph *G,int v,int w)
{
int k;
if(v>=0 && v<G->vexnum && w>=0 && w<G->vexnum) //v,w合理
{
for( k=w+1;k<G->vexnum;k++)
if(G->arcs[v][k].adj!=INFINITY)
{return k;break;}
}
return -1;
}
int visited[MAX];
void DFS(MGraph *G,int v)//从第v个顶点出发递归地深度优先遍历图G
{
int w;
visited[v]=1;
VisitFunc(G,v);//访问第v个结点
for(w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w))
if(!visited[w]){
DFS(G,w);
}
}
void DFSTraverse(MGraph *G,char *s)//深度优先遍历
{int v,k;
for(v=0;v<G->vexnum;v++)
visited[v]=0;
k=LocateVex(G,s);
if(k>=0&&k<G->vexnum){
for(v=k;v>=0;v--){
if(!visited[v])
DFS(G,v);}
for(v=k+1;v<G->vexnum;v++)
if(!visited[v])
DFS(G,v);
}
}
typedef struct QNode {
int data;
struct QNode *next;
}QNode,*Queueptr;
typedef struct {
Queueptr front;
Queueptr rear;
}LinkQueue;
int InitQueue (LinkQueue &Q){
Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
if(!Q.front)exit(-1);
Q.front->next=NULL;
return 1;
}
int EnQueue(LinkQueue &Q,int e){
Queueptr p;
p=(Queueptr)malloc(sizeof(QNode));
if(!p)exit(-1);
p->data=e; p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return 1;
}
int DeQueue (LinkQueue &Q,int &e){
Queueptr p;
if(Q.front==Q.rear)return(-1);
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
return 1;
}
int QueueEmpty(LinkQueue Q)
{
if(Q.rear==Q.front)
return 1;
return 0;
}
int Visited[MAX];
void BFSTraverse(MGraph *G,char *str) { //广度优先遍历
int w,u,v,k;
LinkQueue Q;
for(v=0;v<G->vexnum;v++) Visited[v]=0;
InitQueue(Q);
k=LocateVex(G,str);
for(v=k;v>=0;v--)
//for(v=0;v<G->vexnum;++v)
{
if(!Visited[v])
{
Visited[v]=1;
VisitFunc(G,v);
EnQueue(Q,v); //v入队
while(!QueueEmpty(Q))
{
DeQueue(Q,u); //出队
for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
if(!Visited[w])
{
Visited[w]=1;
VisitFunc(G,w);
EnQueue(Q,w);
}
} //while
}
}
for(v=k+1;v<G->vexnum;v++)
if(!Visited[v])
{
Visited[v]=1;
VisitFunc(G,v);
EnQueue(Q,v);//v入队
while(!QueueEmpty(Q))
{
DeQueue(Q,u);//出队
for(w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w))
if(!Visited[w])
{
Visited[w]=1;
VisitFunc(G,w);
EnQueue(Q,w);
}
}
}
}
void main()
{
MGraph *G,b;
char v[10];
G=CreatUDN(&b);
printf("请输入开始遍历的起始结点名称:");
scanf("%s",v);
printf("\n深度优先遍历(输出结点序列):\n");
DFSTraverse(G,v);
printf("\n广度优先遍历(输出结点序列):\n");
BFSTraverse(G,v);
}
这个程序我刚写好,可以运行出结果的,你先运行一下试试.
G. 用C语言编程实现图的遍历算法
图的遍历是指按某条搜索路径访问图中每个结点,使得每个结点均被访问一次,而且仅被访问一次。图的遍历有深度遍历算法和广度遍历算法,最近阿杰做了关于图的遍历的算法,下面是图的遍历深度优先的算法(C语言程序):
#include<stdio.h>
#include<malloc.h>
#define MaxVertexNum 5
#define m 5
#define TRUE 1
#define NULL 0
typedef struct node
{
int adjvex;
struct node *next;
}JD;
typedef struct EdgeNode
{
int vexdata;
JD *firstarc;
}TD;
typedef struct
{
TD ag[m];
int n;
}ALGRAPH;
void DFS(ALGRAPH *G,int i)
{
JD *p;
int visited[80];
printf("visit vertex:%d->",G->ag[i].vexdata);
visited[i]=1;
p=G->ag[i].firstarc;
while(p)
{
if (!visited[p->adjvex])
DFS(G,p->adjvex);
p=p->next;
}
}
void creat(ALGRAPH *G)
{
int i,m1,j;
JD *p,*p1;
printf("please input the number of graph\n");
scanf("%d",&G->n);
for(i=0;i<G->n;i++)
{
printf("please input the info of node %d",i);
scanf("%d",&G->ag[i].vexdata);
printf("please input the number of arcs which adj to %d",i);
scanf("%d",&m1);
printf("please input the adjvex position of the first arc\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",&p->adjvex);
p->next=NULL;
G->ag[i].firstarc=p;
p1=p;
for(j=2 ;j<=m1;j++)
{
printf("please input the position of the next arc vexdata\n");
p=(JD *)malloc(sizeof(JD));
scanf("%d",&p->adjvex);
p->next=NULL;
p1->next=p;
p1=p;
}
}
}
int visited[MaxVertexNum];
void DFSTraverse(ALGRAPH *G)
{
int i;
for(i=0;i<G->n;i++)
visited[i]=0;
for(i=0;i<G->n;i++)
if(!visited[i])
DFS(G,i);
}
int main()
{
ALGRAPH *G;
printf("下面以临接表存储一个图;\n");
creat(G);
printf("下面以深度优先遍历该图 \n");
DFSTraverse(G);
getchar();
}
H. C语言编写程序实现图的遍历操作
楼主你好,下面是源程序!
/*/////////////////////////////////////////////////////////////*/
/* 图的深度优先遍历 */
/*/////////////////////////////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
struct node /* 图顶点结构定义 */
{
int vertex; /* 顶点数据信息 */
struct node *nextnode; /* 指下一顶点的指标 */
};
typedef struct node *graph; /* 图形的结构新型态 */
struct node head[9]; /* 图形顶点数组 */
int visited[9]; /* 遍历标记数组 */
/********************根据已有的信息建立邻接表********************/
void creategraph(int node[20][2],int num)/*num指的是图的边数*/
{
graph newnode; /*指向新节点的指针定义*/
graph ptr;
int from; /* 边的起点 */
int to; /* 边的终点 */
int i;
for ( i = 0; i < num; i++ ) /* 读取边线信息,插入邻接表*/
{
from = node[i][0]; /* 边线的起点 */
to = node[i][1]; /* 边线的终点 */
/* 建立新顶点 */
newnode = ( graph ) malloc(sizeof(struct node));
newnode->vertex = to; /* 建立顶点内容 */
newnode->nextnode = NULL; /* 设定指标初值 */
ptr = &(head[from]); /* 顶点位置 */
while ( ptr->nextnode != NULL ) /* 遍历至链表尾 */
ptr = ptr->nextnode; /* 下一个顶点 */
ptr->nextnode = newnode; /* 插入节点 */
}
}
/********************** 图的深度优先搜寻法********************/
void dfs(int current)
{
graph ptr;
visited[current] = 1; /* 记录已遍历过 */
printf("vertex[%d]\n",current); /* 输出遍历顶点值 */
ptr = head[current].nextnode; /* 顶点位置 */
while ( ptr != NULL ) /* 遍历至链表尾 */
{
if ( visited[ptr->vertex] == 0 ) /* 如过没遍历过 */
dfs(ptr->vertex); /* 递回遍历呼叫 */
ptr = ptr->nextnode; /* 下一个顶点 */
}
}
/****************************** 主程序******************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 边线数组 */
{1, 3}, {3, 1},
{1, 4}, {4, 1},
{2, 5}, {5, 2},
{2, 6}, {6, 2},
{3, 7}, {7, 3},
{4, 7}, {4, 4},
{5, 8}, {8, 5},
{6, 7}, {7, 6},
{7, 8}, {8, 7} };
int i;
clrscr();
for ( i = 1; i <= 8; i++ ) /* 顶点数组初始化 */
{
head[i].vertex = i; /* 设定顶点值 */
head[i].nextnode = NULL; /* 指针为空 */
visited[i] = 0; /* 设定遍历初始标志 */
}
creategraph(node,20); /* 建立邻接表 */
printf("Content of the gragh's ADlist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf("vertex%d ->",head[i].vertex); /* 顶点值 */
ptr = head[i].nextnode; /* 顶点位置 */
while ( ptr != NULL ) /* 遍历至链表尾 */
{
printf(" %d ",ptr->vertex); /* 印出顶点内容 */
ptr = ptr->nextnode; /* 下一个顶点 */
}
printf("\n"); /* 换行 */
}
printf("\nThe end of the dfs are:\n");
dfs(1); /* 打印输出遍历过程 */
printf("\n"); /* 换行 */
puts(" Press any key to quit...");
getch();
}
/*//////////////////////////////////////////*/
/* 图形的广度优先搜寻法 */
/* ///////////////////////////////////////*/
#include <stdlib.h>
#include <stdio.h>
#define MAXQUEUE 10 /* 队列的最大容量 */
struct node /* 图的顶点结构定义 */
{
int vertex;
struct node *nextnode;
};
typedef struct node *graph; /* 图的结构指针 */
struct node head[9]; /* 图的顶点数组 */
int visited[9]; /* 遍历标记数组 */
int queue[MAXQUEUE]; /* 定义序列数组 */
int front = -1; /* 序列前端 */
int rear = -1; /* 序列后端 */
/***********************二维数组向邻接表的转化****************************/
void creategraph(int node[20][2],int num)
{
graph newnode; /* 顶点指针 */
graph ptr;
int from; /* 边起点 */
int to; /* 边终点 */
int i;
for ( i = 0; i < num; i++ ) /* 第i条边的信息处理 */
{
from = node[i][0]; /* 边的起点 */
to = node[i][1]; /* 边的终点 */
/* 建立新顶点 */
newnode = ( graph ) malloc(sizeof(struct node));
newnode->vertex = to; /* 顶点内容 */
newnode->nextnode = NULL; /* 设定指针初值 */
ptr = &(head[from]); /* 顶点位置 */
while ( ptr->nextnode != NULL ) /* 遍历至链表尾 */
ptr = ptr->nextnode; /* 下一个顶点 */
ptr->nextnode = newnode; /* 插入第i个节点的链表尾部 */
}
}
/************************ 数值入队列************************************/
int enqueue(int value)
{
if ( rear >= MAXQUEUE ) /* 检查伫列是否全满 */
return -1; /* 无法存入 */
rear++; /* 后端指标往前移 */
queue[rear] = value; /* 存入伫列 */
}
/************************* 数值出队列*********************************/
int dequeue()
{
if ( front == rear ) /* 队列是否为空 */
return -1; /* 为空,无法取出 */
front++; /* 前端指标往前移 */
return queue[front]; /* 从队列中取出信息 */
}
/*********************** 图形的广度优先遍历************************/
void bfs(int current)
{
graph ptr;
/* 处理第一个顶点 */
enqueue(current); /* 将顶点存入队列 */
visited[current] = 1; /* 已遍历过记录标志置疑1*/
printf(" Vertex[%d]\n",current); /* 打印输出遍历顶点值 */
while ( front != rear ) /* 队列是否为空 */
{
current = dequeue(); /* 将顶点从队列列取出 */
ptr = head[current].nextnode; /* 顶点位置 */
while ( ptr != NULL ) /* 遍历至链表尾 */
{
if ( visited[ptr->vertex] == 0 ) /*顶点没有遍历过*/
{
enqueue(ptr->vertex); /* 奖定点放入队列 */
visited[ptr->vertex] = 1; /* 置遍历标记为1 */
printf(" Vertex[%d]\n",ptr->vertex);/* 印出遍历顶点值 */
}
ptr = ptr->nextnode; /* 下一个顶点 */
}
}
}
/*********************** 主程序 ************************************/
/*********************************************************************/
void main()
{
graph ptr;
int node[20][2] = { {1, 2}, {2, 1}, /* 边信息数组 */
{6, 3}, {3, 6},
{2, 4}, {4, 2},
{1, 5}, {5, 1},
{3, 7}, {7, 3},
{1, 7}, {7, 1},
{4, 8}, {8, 4},
{5, 8}, {8, 5},
{2, 8}, {8, 2},
{7, 8}, {8, 7} };
int i;
clrscr();
puts("This is an example of Width Preferred Traverse of Gragh.\n");
for ( i = 1; i <= 8; i++ ) /*顶点结构数组初始化*/
{
head[i].vertex = i;
head[i].nextnode = NULL;
visited[i] = 0;
}
creategraph(node,20); /* 图信息转换,邻接表的建立 */
printf("The content of the graph's allist is:\n");
for ( i = 1; i <= 8; i++ )
{
printf(" vertex%d =>",head[i].vertex); /* 顶点值 */
ptr = head[i].nextnode; /* 顶点位置 */
while ( ptr != NULL ) /* 遍历至链表尾 */
{
printf(" %d ",ptr->vertex); /* 打印输出顶点内容 */
ptr = ptr->nextnode; /* 下一个顶点 */
}
printf("\n"); /* 换行 */
}
printf("The contents of BFS are:\n");
bfs(1); /* 打印输出遍历过程 */
printf("\n"); /* 换行 */
puts(" Press any key to quit...");
getch();
}
I. C语言实现图的广度优先搜索遍历算法
先写个大题思路,楼主先自己想想,想不出来的话,2天后给代码。
queue<node> q;
q.push(start);
bool canVisit[][];
node cur;
while(!q.empty()){
cur = q.top();
q.pop();
foreach(node is connected by cur){
if(canVisit[node.x][node.y])
{
printf("访问结点(%d,%d)",node.x,node.y);
canVisit[node.x][node.y]=false;
q.push(node);
}
}
}
J. 求图的广度遍历(C/C++ 程序)
#include<stdio.h>
#include<stdlib.h>
#define Max 10
#define FALSE 0
#define TRUE 1
#define Error printf
#define QueueSize 30
typedef struct
{
char vexs[Max];
int edges[Max][Max];
int n,e;
}MGraph;
int visited[Max];
typedef struct
{
int front;
int rear;
int count;
int data[QueueSize];
}CirQueue;
//初始化队列
void InitQueue(CirQueue *Q)
{
Q->front=Q->rear=0;
Q->count=0;
}
//队列空
int QueueEmpty(CirQueue *Q)
{
return Q->count=QueueSize;
}
//队列满
int QueueFull(CirQueue *Q)
{
return Q->count==QueueSize;
}
//进队
void EnQueue(CirQueue *Q,int x)
{
if(QueueFull(Q))
{
Error("Queue overflow");
}
else
{
Q->count++;
Q->data[Q->rear]=x;
Q->rear=(Q->rear+1)%QueueSize;
}
}
//出队
int DeQueue(CirQueue *Q)
{
int temp;
if(QueueEmpty(Q))
{
Error("Queue underflow");
}
else
{
temp=Q->data[Q->front];
Q->count--;
Q->front=(Q->front+1)%QueueSize;
return temp;
}
}
//建立图矩阵
void CreateMGraph(MGraph *G)
{
int i,j,k;
char ch1,ch2;
printf("\n\t\t请输入顶点数,边数并按回车键(格式:3,4):");
scanf("%d,%d",&(G->n),&(G->e));
for(i=0;i<G->n;i++)
{
getchar();
printf("\n\t\t请输入第%d个顶点序号并按回车键",i+1);
scanf("%c",&(G->vexs[i]));
}
for(i=0;i<G->n;i++)
{
for(j=0;j<G->n;j++)
{
G->edges[i][j]=0;
}
}
for(k=0;k<G->e;k++)
{
getchar();
printf("\n\t\t请输入第%d条边的顶点序号并按回车键(格式:i,j):",k+1);
scanf("%c,%c",&ch1,&ch2);
for(i=0;ch1!=G->vexs[i];i++);
for(j=0;ch2!=G->vexs[j];j++);
G->edges[i][j]=1;
}
}
//深度优先遍历递归
void DFSM(MGraph *G,int i)
{
int j;
printf("\n\t\t深度遍历序列:%c\n",G->vexs[i]);
visited[i]=TRUE;
for(j=0;j<G->n;j++)
{
if(G->edges[i][j]==1&&!visited[j])
{
DFSM(G,j);
}
}
}
//广度优先遍历递归
void BFSM(MGraph *G,int k)
{
int i,j;
CirQueue Q;
InitQueue(&Q);
printf("\n\t\t广度优先遍历序列:%c\n",G->vexs[k]);
visited[k]=TRUE;
EnQueue(&Q,k);
while(!QueueEmpty(&Q))
{
i=DeQueue(&Q);
for(j=0;j<G->n;j++)
{
if(G->edges[i][j]==1 &&! visited[j])
{
visited[j]=TRUE;
EnQueue(&Q,j);
}
}
}
}
//深度优先遍历
void DFSTraverseM(MGraph *G)
{
int i;
for(i=0;i<G->n;i++)
{
visited[i]=FALSE;
}
for(i=0;i<G->n;i++)
{
if(!visited[i])
{
DFSM(G,i);
}
}
}
//广度优先遍历
void BFSTraverseM(MGraph *G)
{
int i;
for(i=0;i<G->n;i++)
{
visited[i]=FALSE;
}
for(i=0;i<G->n;i++)
{
if(!visited[i])
{
BFSM(G,i);
}
}
}
main()
{
MGraph *G,a;
char ch1;
int i,j,ch2;
G=&a;
printf("\n\t\t建立一个图矩阵\n");
CreateMGraph(G);
printf("\n\t\t已建立图的矩阵\n");
for(i=0;i<G->n;i++)
{
printf("\n\t\t ");
for(j=0;j<G->n;j++)
{
printf("%5d",G->edges[i][j]);
}
}
getchar();
ch1='y';
while(ch1=='y'||ch1=='Y')
{
printf("\n");
printf("\n\t\t 图子系统 \n");
printf("\n\t\t***************************************\n");
printf("\n\t\t* 1--------更新矩阵 *\n");
printf("\n\t\t* 2--------深度优先遍历 *\n");
printf("\n\t\t* 3--------广度优先遍历 *\n");
printf("\n\t\t* 0--------退出 *\n");
printf("\n\t\t***************************************\n");
printf("\n\t\t请选择菜单号(0~3):");
scanf("%d",&ch2);
getchar();
switch(ch2)
{
case 1:
CreateMGraph(G);
printf("\n\t\t图建立完毕!\n");
break;
case 2:
DFSTraverseM(G);
break;
case 3:
BFSTraverseM(G);
break;
case 0:
ch1='n';
break;
default:
system("cls");
printf("\n\t\t输入有误!\n");
break;
}
if(ch2==1||ch2==2||ch2==3)
{
printf("\n\n\t\t ");
system("pause");
system("cls");
}
}
}