以下这无向图实现:
这里注意的是在存储的过程中是先1后2在3,其排列是随机的。
代码如下:
// c++_verts.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#define MAXVEX 100
typedef char VertexType; //顶点类型
typedef int EdgeType; //边的权值类型
typedef struct EdgeNode //边表结点
{
int adjvex;
EdgeType weight;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode //顶点表结点
{
VertexType data;
EdgeNode *firstedge;
}VertexNode, AdjList[MAXVEX];
typedef struct
{
AdjList adjList;
int numNodes, numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *Gp)
{
int i, j , k;
EdgeNode *pe;
cout << "输入顶点数和边数(空格分隔):" << endl;
cin >> Gp->numNodes >> Gp->numEdges;
for (i = 0 ; i < Gp->numNodes; ++i)
{
cout << "输入顶点信息: " << endl;
cin >> Gp->adjList[i].data;
Gp->adjList[i].firstedge = NULL; //将边表置为空表
}
for (k = 0; k < Gp->numEdges; ++k) //建立边表
{
cout << "输入边(Vi, Vj)的顶点序号i,j(空格分隔):"<<endl;
cin >> i >> j;
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = j;
pe->next = Gp->adjList[i].firstedge;
Gp->adjList[i].firstedge = pe;
//如果是无向图则需下列
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = i;
pe->next = Gp->adjList[j].firstedge;
Gp->adjList[j].firstedge = pe;
}
cout <<"邻接表创建成功。。" <<endl;
}
void show_adjlist_graph(GraphAdjList *Gp)
{
cout << "邻接表如下:"<<endl;
if (Gp->numNodes == 0 || Gp->numEdges == 0)
{
cout <<"邻接表为空!"<<endl;
}
for (int v = 0; v < Gp->numNodes; ++v)
{
cout<<"V"<< v <<":";
if (Gp->adjList[v].firstedge == NULL)
cout<<"NULL"<<endl;
else
{
EdgeNode *cur = Gp->adjList[v].firstedge;
while (cur != NULL)
{
cout<<"V"<<cur->adjvex<<" ";
cur = cur->next;
}
cout << endl;0
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
GraphAdjList g;
CreateALGraph(&g);
show_adjlist_graph(&g);
system("pause");
return 0;
}
效果如下:
以下为有向图:
图为:
代码如下:
// c++_verts.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
#define MAXVEX 100
typedef char VertexType; //顶点类型
typedef int EdgeType; //边的权值类型
typedef struct EdgeNode //边表结点
{
int adjvex;
EdgeType weight;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode //顶点表结点
{
VertexType data;
EdgeNode *firstedge;
}VertexNode, AdjList[MAXVEX];
typedef struct
{
AdjList adjList;
int numNodes, numEdges;
}GraphAdjList;
void CreateALGraph(GraphAdjList *Gp)
{
int i, j , k;
EdgeNode *pe;
cout << "输入顶点数和边数(空格分隔):" << endl;
cin >> Gp->numNodes >> Gp->numEdges;
for (i = 0 ; i < Gp->numNodes; ++i)
{
cout << "输入顶点信息: " << endl;
cin >> Gp->adjList[i].data;
Gp->adjList[i].firstedge = NULL; //将边表置为空表
}
for (k = 0; k < Gp->numEdges; ++k) //建立边表
{
cout << "输入边(Vi, Vj)的顶点序号i,j(空格分隔):"<<endl;
cin >> i >> j;
pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = j;
pe->next = Gp->adjList[i].firstedge;
Gp->adjList[i].firstedge = pe;
//如果是无向图则需下列
/*pe = (EdgeNode *)malloc(sizeof(EdgeNode));
pe->adjvex = i;
pe->next = Gp->adjList[j].firstedge;
Gp->adjList[j].firstedge = pe;*/
}
cout <<"邻接表创建成功。。" <<endl;
}
void show_adjlist_graph(GraphAdjList *Gp)
{
cout << "邻接表如下:"<<endl;
if (Gp->numNodes == 0 || Gp->numEdges == 0)
{
cout <<"邻接表为空!"<<endl;
}
for (int v = 0; v < Gp->numNodes; ++v)
{
cout<<"V"<< v <<":";
if (Gp->adjList[v].firstedge == NULL)
cout<<"NULL"<<endl;
else
{
EdgeNode *cur = Gp->adjList[v].firstedge;
while (cur != NULL)
{
cout<<"V"<<cur->adjvex<<" ";
cur = cur->next;
}
cout << endl;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
GraphAdjList g;
CreateALGraph(&g);
show_adjlist_graph(&g);
system("pause");
return 0;
}
效果图如下: