#include <iostream>
#include <string>
#include <queue>
using namespace std;

#define MAXVEX 10
#define INFINITY 0XFFFFFFFF
#define SUCCESS 1
#define UNSUCCESS 0
typedef int Status;
bool visited[MAXVEX]; //全局数组,记录结点是否已补访问

typedef int EdgeWeight;
typedef struct EdgeNode
{
int adjvex; //邻接点
EdgeWeight weight; //权值
struct EdgeNode* next; //指向下一条边

}EdgeNode;

typedef string VertexType; //顶点类型
typedef struct
{
VertexType data;
EdgeNode* pFirstEdge; //指示第一条边
}VertexNode;

typedef VertexNode AdjList[MAXVEX];//邻接表

typedef struct
{
AdjList adjList; //邻接表
int iVexNum; //顶点个数
int iEdgeNum; //边数
}AdjListGraph;


//由顶点值得到顶点索引
int GetIndexByVertexVal( const AdjListGraph& G, VertexType val )
{
for ( int i = 0; i < G.iVexNum; ++i )
{
if ( val == G.adjList[i].data )
return i;
}
return -1;
}

//创建有向图
Status CreateAdjListGraph( AdjListGraph& G )
{
cout << "输入顶点个数以及边数:";
cin >> G.iVexNum >> G.iEdgeNum;
cout << "请输入" << G.iVexNum << "个顶点:";
for ( int i = 0; i < G.iVexNum; ++i )
{
cin >> G.adjList[i].data;
G.adjList[i].pFirstEdge = NULL;
}

cout << "请输入由两点构成的边(" << G.iEdgeNum << "条):";
for ( int i = 0; i < G.iEdgeNum; ++i )
{
VertexType first;
VertexType second;
cin >> first >> second;
int m = GetIndexByVertexVal( G, first );
int n = GetIndexByVertexVal( G, second );
if ( m == -1 || n == -1 )
return UNSUCCESS;

EdgeNode* pEdgeNode = new EdgeNode;
pEdgeNode->adjvex = n;
pEdgeNode->weight = 0; //权值暂时不用

//表头插入法
pEdgeNode->next = G.adjList[m].pFirstEdge;
G.adjList[m].pFirstEdge = pEdgeNode;
}
return SUCCESS;
}


//销毁图
void DestroyGraph( AdjListGraph& G )
{
for ( int i = 0; i < G.iVexNum; ++i )
{
EdgeNode* pEdge = G.adjList[i].pFirstEdge;
while( pEdge )
{
EdgeNode* q = pEdge;
pEdge = pEdge->next;
delete q;
}
G.adjList[i].pFirstEdge = NULL;
}
G.iVexNum = 0;
G.iEdgeNum = 0;
}


//得到顶点的度
int GetVertexDegree( const AdjListGraph& G, VertexType val )
{
int m = GetIndexByVertexVal( G, val );//得到顶点的下标
int iCount = 0; //顶点的度

for ( int i = 0; i < G.iVexNum; ++i )
{
if ( i == m )
{
EdgeNode* pEdgeOut = G.adjList[i].pFirstEdge;
while ( pEdgeOut )
{
++iCount;//累加出度
pEdgeOut = pEdgeOut->next;
}
}
else
{
EdgeNode* pEdgeIn = G.adjList[i].pFirstEdge;
while ( pEdgeIn )
{
if ( pEdgeIn->adjvex == m )
++iCount; //累加入度
pEdgeIn = pEdgeIn->next;
}
}
}
return iCount;
}


//深度优先遍历图
void DFS( const AdjListGraph& G, int i )
{
cout << G.adjList[i].data << " ";
visited[i] = true;

EdgeNode* pEdge = G.adjList[i].pFirstEdge;
while( pEdge )
{
int j = pEdge->adjvex;
if ( !visited[j] )
{
DFS( G, j);
}
pEdge = pEdge->next;
}
}
void DFSTraverse( const AdjListGraph& G )
{
for ( int i = 0; i < G.iVexNum; ++i )
{
visited[i] = false;
}

for ( int i = 0; i < G.iVexNum; ++i )
{
if ( !visited[i] )
DFS( G, i );
}
}

//广度优先遍历
void BFSTraverse( const AdjListGraph& G )
{
for ( int i = 0; i < G.iVexNum; ++i )
{
visited[i] = false;
}

queue<int> Q;
for ( int i = 0; i < G.iVexNum; ++i )
{
if ( !visited[i] )
{
cout << G.adjList[i].data << " ";
visited[i] = true;
Q.push( i );
while( !Q.empty() )
{
int iVex = Q.front();
Q.pop();

EdgeNode* pEdge = G.adjList[iVex].pFirstEdge;
while ( pEdge )
{
if ( !visited[pEdge->adjvex] )
{
cout << G.adjList[pEdge->adjvex].data << " ";
visited[pEdge->adjvex] = true;
Q.push( pEdge->adjvex );
}
pEdge = pEdge->next;
}
}
}
}
}

int main()
{
//创建有向图
AdjListGraph G;
CreateAdjListGraph( G );

//深度优先遍历图
DFSTraverse( G );
cout << endl << endl;

//广度优先遍历图
BFSTraverse( G );
cout << endl << endl;

//结点的度
cout << "输入求度的结点:";
VertexType v;
cin >> v;
cout << "度为:" << GetVertexDegree( G, v ) << endl;

//销毁有向图
DestroyGraph( G );

return 0;
}

数据结构:图(邻接表存储 c++实现)_结点