邻接表的深搜与广搜遍历

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>


#define N 10
#define INFINITY  32768
#define True 1
#define False 0
#define Error -1
#define Ok 1


typedef enum{DG,DN,UDG,UDN}GraphKind;
typedef char VertexType;


//边结点
typedef struct ArcNode{
int adjVex;
struct ArcNode *nextArc;
}ArcNode;


//表结点
typedef struct VexNode{
VertexType data;
ArcNode *firstArc;
}VexNode;


//邻接表
typedef struct{
VexNode vexs[N];
int vexnum,arcnum;
GraphKind kind;
}AdjList;


//队列结点
typedef struct Node{
int data;
struct Node *next;
}Node;


//队列
typedef struct{
Node *front;
Node *rear;
}LinkQueue;



int Dvisited[N]={0};
int Bvisited[N]={0};


//队列的初始化
int InitLinkQueue(LinkQueue &Q){
Q.front=(Node *)malloc(sizeof(Node));
if(Q.front!=NULL){//队列不为空
Q.rear=Q.front;
Q.front->next=NULL;

return True;
}else{
return False;
}
}


//进队操作
int EnterQueue(LinkQueue &Q,int x){
Node *newNode=(Node *)malloc(sizeof(Node));
newNode->data=x;
newNode->next=NULL;

Q.rear->next=newNode;
Q.rear=newNode;

return True;
}


//出队操作
int ExitQueue(LinkQueue &Q,int *x){
if(Q.front==Q.rear){
return False;
}else{
Node *p=Q.front->next;
Q.front->next=p->next;

if(Q.rear==p){
Q.rear=Q.front;
}

*x=p->data;
free(p);

return True;
}
}


//判断队列是否为空
int IsEmpty(LinkQueue Q){
if(Q.front==Q.rear){
return True;
}else{
return False;
}
}


//查找顶点所在的位置
int LocateVex(AdjList G,VertexType v){
int i,j=Error;

for(i=0;i<G.vexnum;i++){
if(G.vexs[i].data==v){
return i;
}
}

return j;
}


//创建邻接表
void CreateAdjList(AdjList &G){
int i,j,l,m;
VertexType data;

VertexType v1,v2;
printf("请输入有向图的顶点数:");
scanf("%d",&G.vexnum);

printf("\n");

printf("请输入有向图的弧数:");
scanf("%d",&G.arcnum);

printf("\n");

printf("请依次输入有向图的各个顶点:\n");

for(i=0;i<G.vexnum;i++){//初始化顶点(表结点)
printf("第%d个顶点是:",i+1);
data=getche();

printf("\n");

G.vexs[i].data=data;
G.vexs[i].firstArc=NULL;
}

for(j=0;j<G.arcnum;j++){
printf("\n请输入有向图第%d条弧的弧尾和弧头:\n",j+1);
printf("弧尾:");
v1=getche();

printf("\t");

printf("弧头:");
v2=getche();

l=LocateVex(G,v1);
m=LocateVex(G,v2);

//开辟一个新的边结点的空间
ArcNode *p=(ArcNode *)malloc(sizeof(ArcNode));
//初始化新创建的边结点
p->adjVex=m;
p->nextArc=G.vexs[l].firstArc;
G.vexs[l].firstArc=p;
}

printf("\n");
printf("有向图的邻接表创建成功。\n");
}


//显示图的信息
void Display(AdjList G){
int i;
ArcNode *p;

printf("有向图有%d个顶点,%d条弧。\n",G.vexnum,G.arcnum);

for(i=0;i<G.vexnum;i++){
p=G.vexs[i].firstArc;

while(p){
printf("<%c->%c>\t",G.vexs[i].data,G.vexs[p->adjVex].data);
p=p->nextArc;
}

printf("\n");

}
}


//深度遍历(邻接表)
void Depth(AdjList G,int adj){

ArcNode *p;

printf("%c",G.vexs[adj].data);

Dvisited[adj]=1;

p=G.vexs[adj].firstArc;

while(p!=NULL){

if(!Dvisited[p->adjVex]){


Depth(G,p->adjVex);
}

p=p->nextArc;
}
}


//广度遍历(邻接表)
void Breadth(AdjList G,int adj){
int x;

LinkQueue Q;
InitLinkQueue(Q);


Bvisited[adj]=1;
EnterQueue(Q,adj);

while(!IsEmpty(Q)){
ExitQueue(Q,&x);


printf("%c",G.vexs[x].data);

ArcNode *p=G.vexs[x].firstArc;

while(p){
if(!Bvisited[p->adjVex]){
Bvisited[p->adjVex]=1;
EnterQueue(Q,p->adjVex);
}
p=p->nextArc;
}
}
}


int main(){
AdjList G;

CreateAdjList(G);
Display(G);

printf("有向图深度遍历:");
Depth(G,0);
printf("\n");

printf("有向图广度遍历:");
Breadth(G,0);
printf("\n");
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好资源#include <iostream> #include <string> #include <queue> using namespace std; //表结点 typedef struct ArcNode{ int adjvex;//该弧所指向的顶点的位置 ArcNode *nextarc;//指向下一条弧的指针 }ArcNode; //头结点 typedef struct VNode{ string data;//顶点信息 ArcNode* firstarc;//第一个表结点的地址,指向第一条依附该顶点的弧的指针 }VNode, AdjList[10]; typedef struct{ AdjList vertices; int vexnum, arcnum;//图的顶点数和弧数 }ALGraph; int LocateVex(ALGraph G, string u)//返回顶点u在图中的位置 { for(int i=0; i<G.vexnum; i++) if(G.vertices[i].data==u) return i; return -1; } void CreateUDG(ALGraph &G)//构造无向图 { string v1, v2; int i, j, k; cout<<"请输入顶点数和边数:"; cin>>G.vexnum>>G.arcnum; cout<<"请输入顶点:"; for(i=0; i<G.vexnum; i++) { cin>>G.vertices[i].data; G.vertices[i].firstarc=NULL; } cout<<"请输入边:"; cout<<endl; for(k=0; k<G.arcnum; k++) { cin>>v1>>v2; i=LocateVex(G, v1); j=LocateVex(G, v2); //插入v1的邻接表,为了提高效率,总在表头插入结点。 ArcNode *arc=new ArcNode; arc->adjvex=j; arc->nextarc=G.vertices[i].firstarc; G.vertices[i].firstarc=arc; //插入v2的邻接表,为了提高效率,总在表头插入结点。 arc=new ArcNode; arc->adjvex=i; arc->nextarc=G.vertices[j].firstarc; G.vertices[j].firstarc=arc; } } void Print(ALGraph G)//打印邻接表 { cout<<"打印邻接表如下:"; cout<<endl; for(int i=0; i<G.vexnum; i++)//遍历每个顶点的邻接表 { cout<<G.vertices[i].data; ArcNode *p=G.vertices[i].firstarc; while(p) { cout<<"->"<<G.vertices[p->adjvex].data; p=p->nextarc; } cout<<endl; } } int FirstAdjVex(ALGraph G, int v)//返回顶点v的第一个邻接点序号 { ArcNode *p=G.vertices[v].firstarc; if(p) return p->adjvex; else return -1; } int NextAdjVex(ALGraph G, int v, int w)//返回顶点v的相对于w的下一个邻接点序号 { ArcNode* p=G.vertices[v].firstarc; while(p) { if(p->adjvex==w) break; p=p->nextarc; } if(p->adjvex!=w || !p->nextarc)//如果没找到w或者w是最后一个邻接点 return -1; return p->nextarc->adjvex; } bool visited[10]; void DFS(ALGraph G, int v) { visited[v]=true; cout<<G.vertices[v].data<<" "; for(int w=FirstAdjVex(G, v); w>=0; w=NextAdjVex(G, v, w) ) if(!visited[w]) DFS(G, w); } void DFSTraverse(ALGraph G)//深搜 { for(int i=0; i<G.vexnum; i++) visited[i]=false; for(i=0; i<G.vexnum; i++) if(!visited[i]) DFS(G, i); } void BFSTraverse(ALGraph G)//广 { queue<int> q; for(int i=0; i<G.vexnum; i++) visited[i]=false; for(i=0; i<G.vexnum; i++) { if(!visited[i]) { q.push(i); visited[i]=true; while(!q.empty()) { int v=q.front(); q.pop(); cout<<G.vertices[v].data<<" "; for(int w=FirstAdjVex(G, v); w>=0; w=NextAdjVex(G, v, w)) { if (!visited[w]) { q.push(w); visited[w]=true; } } } } } } void main() { ALGraph G; CreateUDG(G); Print(G); cout<<"深搜:"; DFSTraverse(G); cout<<endl; cout<<"广:"; BFSTraverse(G); cout<<endl; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值