头文件:‘1.h’
#include <stdio.h>
#include <iostream>
#include <malloc.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
#define ERROR 0
typedef int Status;
using namespace std;
头文件:‘2.h’
#include "1.h"
#define MAX_VERTEX 20
typedef char VertexType;
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
}ArcNode;
typedef struct VNode
{
VertexType data;
ArcNode *firstarc;
}VNode,AdjList[MAX_VERTEX];
typedef struct
{
AdjList vertices;
int vexnum, arcnum;
int kind;
}ALGraph;
//队列
typedef int QElemtype;
#define MAXQSIZE 100
typedef struct
{
QElemtype *base;
int front;
int rear;
}SqQueue;
头文件:‘3.h’(函数的实现)
#pragma once
#include "2.h"
Status LovateVex(ALGraph &G, VertexType v)
{
int i;
for (i = 0; i < G.vexnum; i++)
{
if (G.vertices[i].data == v)
return i;
}
}
Status CreatDG(ALGraph &G)//创建有向图
{
VertexType v1, v2;
int j, k;
ArcNode *p;
cout << "输入定点个数和边数:";
cin >> G.vexnum >> G.arcnum;
int i;
cout << "输入顶点个数和边数:";
for (i = 0; i < G.vexnum; i++)
{
cin >> G.vertices[i].data;
G.vertices[i].firstarc =NULL;
}
cout << "建立图,输入两个顶点:";
for (i = 0; i < G.arcnum; i++)
{
cin >> v1 >> v2;
j=LovateVex(G, v1);
k = LovateVex(G, v2);
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = k;
p->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = p;
}
return OK;
}
Status CreatUDG(ALGraph &G)//创建无向图
{
VertexType v1, v2;
int j, k;
ArcNode *p,*q;
cout << "输入顶点个数和边数:";
cin >> G.vexnum >> G.arcnum;
int i;
cout << "输入顶点:";
for (i = 0; i < G.vexnum; i++)
{
cin >> G.vertices[i].data;
G.vertices[i].firstarc = NULL;
}
cout << "建立图,输入两个顶点:";
for (i = 0; i < G.arcnum; i++)
{
cin >> v1 >> v2;
j = LovateVex(G, v1);
k = LovateVex(G, v2);
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = k;
p->nextarc = G.vertices[j].firstarc;
G.vertices[j].firstarc = p;
q= (ArcNode*)malloc(sizeof(ArcNode));
q->adjvex = j;
q->nextarc = G.vertices[k].firstarc;
G.vertices[k].firstarc = q;
}
return OK;
}
Status GreatGraph(ALGraph &G)//选择自己创建的图的形式
{
cout << "输入图类型:" << "0-DG,1-UDG" << endl;
cin >> G.kind;
switch (G.kind)
{
case 0:return CreatDG(G); break;
case 1:return CreatUDG(G); break;
}
}
void PrintGraph(ALGraph G)//打印图的相关信息
{
ArcNode *p;
cout << endl << "图的顶点和边数:" << G.vexnum << " " << G.arcnum;
cout << endl << "顶点的信息:";
int i;
for (i = 0; i < G.vexnum; i++)
{
cout << " " << G.vertices[i].data;
}
cout << endl << "图的领接表:";
int j;//记录每个点的标号
for (i = 0; i < G.vexnum; i++)
{
cout << endl<< G.vertices[i].data << "的邻接点:";
p = G.vertices[i].firstarc;
while (p)
{
j = p->adjvex;
cout << " " << G.vertices[j].data;
p = p->nextarc;
}
cout << endl;
}
}
void FindOUTDegree(ALGraph G)//计算出度
{
int i;
ArcNode *p;
int count;
cout << "各顶点的出度如下:" << endl;
for (i = 0; i < G.vexnum; i++)
{
count = 0;
p = G.vertices[i].firstarc;
while (p)
{
count++;
p = p->nextarc;
}
cout << G.vertices[i].data << ":" << count;
cout <<endl;
}
}
void FindINDegree(ALGraph G)//计算入度
{
int indegree[MAX_VERTEX];
ArcNode *p;
int i;
cout << "各点入度如下:" << endl;
for (i = 0; i < G.vexnum; i++)
indegree[i] = 0;
for (i = 0; i < G.vexnum; i++)
{
p = G.vertices[i].firstarc;
while (p)
{
++indegree[p->adjvex];
p = p->nextarc;
}
}
int j;
for (j = 0; j < G.vexnum; j++)
{
cout << G.vertices[j].data << " :" << indegree[j] << endl;
}
}
bool visited[MAX_VERTEX];
void DFS(ALGraph G, int i)
{
visited[i] =true;
cout << G.vertices[i].data << endl;
ArcNode *p;
int w;
p = G.vertices[i].firstarc;
while (p)
{
w = p->adjvex;
if (!visited[w])
DFS(G, w);
p = p->nextarc;
}
}
void DFSTraverse(ALGraph G)//深度遍历
{
int i;
for (i = 0; i < G.vexnum; i++)
visited[i] = false;
for (i = 0; i < G.vexnum; i++)
{
if (!visited[i])
DFS(G, i);
}
}
//队列函数
#pragma once
Status InitQueue(SqQueue &Q)
{
Q.base = (QElemtype*)malloc(MAXQSIZE * sizeof(QElemtype));
if (!Q.base) return OVERFLOW;
Q.front = Q.rear = 0;
return OK;
}
Status EnQueue(SqQueue &Q, QElemtype e)
{
if ((Q.rear + 1) % MAXQSIZE == Q.front) return ERROR;
Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1) % MAXQSIZE;
return OK;
}
Status DeQueue(SqQueue &Q, QElemtype &e)
{
if (Q.front == Q.rear) return ERROR;
e = Q.base[Q.front];
Q.front = (Q.front + 1) % MAXQSIZE;
return OK;
}
Status QueueEmpty(SqQueue Q)
{
return Q.front == Q.rear;
}
void BFSTraverse(ALGraph G)//广度遍历
{
bool visited[MAX_VERTEX];
int i;
int w;
QElemtype e;
ArcNode *p;
for (i = 0; i < G.vexnum; i++)
visited[i] = false;
SqQueue Q;
InitQueue(Q);
for (i = 0; i < G.vexnum; i++)
{
if (!visited[i])
{
visited[i] = true;
cout << G.vertices[i].data << endl;
EnQueue(Q, i);
while (!QueueEmpty(Q))
{
DeQueue(Q, e);
for (p = G.vertices[i].firstarc; p; p = p->nextarc)
{
w = p->adjvex;
if (!visited[w])
{
visited[w] = true;
cout << G.vertices[w].data << endl;
EnQueue(Q,w);
}
}
}
}
}
}
主函数:
#include "3.h"
int main()
{
ALGraph G;
GreatGraph(G);
PrintGraph(G);
FindOUTDegree(G);
FindINDegree(G);
cout << "深度搜索结果:" << endl;
DFSTraverse(G);
cout << "广度搜索结果如下:" << endl;
BFSTraverse(G);
system("pause");
}