代码展示:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#define MaxSize 50
#define MAXVERTEX 100 //最大顶点数
typedef struct ArcNode //边表节点
{
int adjvex; //邻接点域,存储该顶点对应的下标
int wigth; //用于存储权值
struct ArcNode *next; //链域,指向下一个邻接点
}ArcNode;
typedef struct VertexNode //顶点表节点
{
char data; //存储顶点数据的信息
ArcNode *firstarc; //边表头指针
}VertexNode, AdjList[MAXVERTEX];
typedef struct ALGraph
{
AdjList adjlist; //定义邻接表
int numvertex; //当前邻接表的顶点数
int numarc; //当前邻接表的边数
}ALGraph;
void CreateALGraph(ALGraph &G)
{
ArcNode *e;
printf("请输入顶点个数:");
scanf("%d",&G.numvertex);
getchar();
printf("请输入边的条数:");
scanf("%d",&G.numarc);
getchar();
for(int i=0; i<G.numvertex; i++)
{
printf("请输入顶点:");
scanf("%c",&G.adjlist[i].data);
getchar();
G.adjlist[i].firstarc = NULL;
}
for(int i=0; i<G.numarc; i++)
{
int m,n, w;
printf("请输入两边的顶点和边上的权重:");
scanf("%d %d %d",&m,&n,&w);
getchar();
e = new ArcNode;
e->adjvex = m;
e->wigth = w;
e->next = G.adjlist[n].firstarc;
G.adjlist[n].firstarc = e;
// e = new ArcNode;
// e->adjvex = n;
// e->wigth = w;
// e->next = G.adjlist[m].firstarc;
// G.adjlist[m].firstarc = e;
}
}
void PrintALGraph(ALGraph G)
{
for(int i=0; i<G.numvertex; i++)
{
ArcNode *p = G.adjlist[i].firstarc;
printf("%c",G.adjlist[i].data);
while(p!=NULL)
{
printf("--权值为%d-->%c\t",p->wigth,G.adjlist[p->adjvex].data);
p = p->next;
}
printf("\n");
}
}
//-------------------------------------------------------------------
typedef struct
{
int data[MaxSize];
int front;
int rear;
}SqQueue;
void InitQueue(SqQueue &Q)
{
Q.front = Q.rear = 0;
}
int QueueEmpty(SqQueue Q)
{
if(Q.rear == Q.front)
{
// printf("循环队列为空!\n");
return 1;
}
else
{
// printf("循环队列未空!\n");
return 0;
}
}
int QueueLength(SqQueue Q)
{
if(Q.front == Q.rear)
{
return 0;
}
else
{
int t = (Q.rear-Q.front+MaxSize)%MaxSize;
return t;
}
}
void GetHead(SqQueue Q,int &x)
{
if(Q.front == Q.rear)
{
printf("循环队列为空,没有对头元素!");
}
else
{
x = Q.data[Q.front];
}
}
void EnQueue(SqQueue &Q,int x)
{
if((Q.rear+1)%MaxSize == Q.front)
{
printf("循环队列已满,无法入队!");
}
else
{
Q.data[Q.rear] = x;
Q.rear = (Q.rear+1)%MaxSize;
}
}
void DeQueue(SqQueue &Q,int &x)
{
if(Q.front == Q.rear)
{
printf("循环队列为空,无法出队!");
}
else
{
x = Q.data[Q.front];
Q.front = (Q.front+1)%MaxSize;
}
}
void Print(SqQueue Q)
{
while(Q.front != Q.rear)
{
printf("%d ",Q.data[Q.front]);
Q.front = (Q.front+1)%MaxSize;
}
}
//---------------------------------------------------------------
int visited[MAXVERTEX] = {0};
void visit(ALGraph G,int i)
{
if(visited[i]==0)
{
printf("访问顶点%c\n",G.adjlist[i].data);
}
}
void BFS(ALGraph G,SqQueue &Q,int v)//广度优先遍历,借助队列
{
visit(G,v);
visited[v] = 1;
EnQueue(Q,v);
while(QueueEmpty(Q)==0)
{
int e;
DeQueue(Q,e);
for(ArcNode *p = G.adjlist[v].firstarc; p!=NULL; p=p->next)
{
if(visited[p->adjvex]!=0)
{
visit(G,p->adjvex);
visited[p->adjvex] = 1;
EnQueue(Q,p->adjvex);
}
}
}
}
void BFSTraverse(ALGraph G,SqQueue Q)
{
InitQueue(Q);
for(int i=0; i<G.numvertex; i++)
{
if(!visited[i])
{
BFS(G,Q,i);
}
}
}
void DFS(ALGraph G,int v)
{
visit(G,v);
visited[v] = 1;
for(ArcNode *p = G.adjlist[v].firstarc; p!=NULL; p = p->next)
{
if(visited[p->adjvex]==0)
{
DFS(G,p->adjvex);
}
}
}
void DFSTraverse(ALGraph G)
{
for(int i=0; i<G.numvertex; i++)
{
if(visited[i]==0)
{
DFS(G,i);
}
}
}
int main() {
ALGraph G;
CreateALGraph(G);
// PrintALGraph(G);
SqQueue Q;
// BFSTraverse(G,Q);
DFSTraverse(G);
return 0;
}