第1关:邻接矩阵表示存储结构,实现顶点和边的插入删除
//算法6.1 采用邻接矩阵表示法创建无向网
#include <iostream>
using namespace std;
#define MaxInt 32767 //表示极大值,即∞
#define MVNum 100 //最大顶点数
#define OK 1
#define ERROR 0
typedef char VerTexType; //假设顶点的数据类型为字符型
typedef int ArcType; //假设边的权值类型为整型
//- - - - -图的邻接矩阵存储表示- - - - -
typedef struct{
VerTexType vexs[MVNum]; //顶点表
ArcType arcs[MVNum][MVNum]; //邻接矩阵
int vexnum,arcnum; //图的当前点数和边数
}AMGraph;
int LocateVex(AMGraph G , VerTexType v){
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vexs[i] == v)
return i;
return -1;
}//LocateVex
int CreateUDN(AMGraph &G){
//采用邻接矩阵表示法,创建无向网G
int i , j , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i){
cin >> G.vexs[i]; //依次输入点的信息
}
for(i = 0; i < G.vexnum; ++i) //初始化邻接矩阵,边的权值均置为极大值MaxInt
for(j = 0; j < G.vexnum; ++j)
G.arcs[i][j] = 0;
for(k = 0; k < G.arcnum;++k){ //构造邻接矩阵
VerTexType v1 , v2;
cin >> v1 >> v2; //输入一条边依附的顶点及权值
i = LocateVex(G, v1); j = LocateVex(G, v2); //确定v1和v2在G中的位置,即顶点数组的下标
G.arcs[i][j] = G.arcs[j][i] = 1; //边<v1, v2>的权值置为w
}//for
return OK;
}//CreateUDN
int InsertVex(AMGraph &G, VerTexType v)
{//在以邻接矩阵形式存储的无向图G中插入顶点v
/**********************Begin***********************/
G.vexs[G.vexnum ++ ] = v;
int n = G.vexnum;
for(int i = 0;i < G.vexnum;i ++ )
G.arcs[i][n - 1] = G.arcs[n - 1][i] = 0;
return OK;
/*********************End**************************/
}
int DeleteVex(AMGraph &G, VerTexType v)
{
/**********************Begin***********************/
int n = G.vexnum;
int t = LocateVex(G,v);
if(t == -1) return ERROR;
for(int i = 0;i < G.vexnum;i ++ )
G.arcs[i][t] = G.arcs[t][i] = G.arcs[i][n - 1];
G.vexs[t] = G.vexs[n - 1];
G.vexnum -- ;
return OK;
/*********************End**************************/
}
int InsertArc(AMGraph &G, VerTexType v, VerTexType w)
{
/**********************Begin***********************/
if(v == w) return ERROR;
int a = LocateVex(G,v),b = LocateVex(G,w);
if(a != -1 && b != -1)
{
G.arcs[a][b] = G.arcs[b][a] = 1;
G.arcnum ++ ;
}
/*********************End**************************/
}
int DeleteArc(AMGraph &G, VerTexType v, VerTexType w)
{
/**********************Begin***********************/
int a = LocateVex(G,v),b = LocateVex(G,w);
if(a != -1 && b != -1)
{
G.arcs[a][b] = G.arcs[b][a] = 0;
G.arcnum -- ;
}
/*********************End**************************/
}
void show(AMGraph G)
{
for(int i = 0 ; i < G.vexnum ; ++i){
for(int j = 0; j < G.vexnum; ++j){
if(j != G.vexnum - 1){
if(G.arcs[i][j] != MaxInt)
cout << G.arcs[i][j] << " ";
else
cout << "0 " << " ";
}
else{
if(G.arcs[i][j] != MaxInt)
cout << G.arcs[i][j] <<endl;
else
cout << "0 " <<endl;
}
}
}//for
}
int main(){
AMGraph G;
CreateUDN(G);
int choose;
cin >> choose;
while(choose --)
{
string op;
cin >> op;
VerTexType v, w;
if(op == "IV")
{
cin >> v;
InsertVex(G, v);
}
else if(op == "DV")
{
cin >> v;
DeleteVex(G, v);
}
else if(op == "IA")
{
cin >> v >> w;
InsertArc(G, v, w);
}
else
{
cin >> v >> w;
DeleteArc(G, v, w);
}
}
show(G);
return 0;
}//main
第2关:邻接表表示存储结构,实现顶点和边的插入与删除
//算法6.2 采用邻接表表示法创建无向图
#include <iostream>
using namespace std;
#define MVNum 100 //最大顶点数
#define OK 1
#define ERROR 0
typedef char VerTexType; //顶点信息
typedef int OtherInfo; //和边相关的信息
//- - - - -图的邻接表存储表示- - - - -
typedef struct ArcNode{ //边结点
int adjvex; //该边所指向的顶点的位置
struct ArcNode *nextarc; //指向下一条边的指针
OtherInfo info; //和边相关的信息
}ArcNode;
typedef struct VNode{
VerTexType data; //顶点信息
ArcNode *firstarc; //指向第一条依附该顶点的边的指针
}VNode, AdjList[MVNum]; //AdjList表示邻接表类型
typedef struct{
AdjList vertices; //邻接表
int vexnum, arcnum; //图的当前顶点数和边数
}ALGraph;
int LocateVex(ALGraph G , VerTexType v)
{
//确定点v在G中的位置
for(int i = 0; i < G.vexnum; ++i)
if(G.vertices[i].data == v)
return i;
return -1;
}//LocateVex
int CreateUDG(ALGraph &G)
{
//采用邻接表表示法,创建无向图G
int i , k;
cin >> G.vexnum >> G.arcnum; //输入总顶点数,总边数
for(i = 0; i < G.vexnum; ++i)
{ //输入各点,构造表头结点表
cin >> G.vertices[i].data; //输入顶点值
G.vertices[i].firstarc=NULL; //初始化表头结点的指针域为NULL
}//for
for(k = 0; k < G.arcnum;++k)
{ //输入各边,构造邻接表
VerTexType v1 , v2;
int i , j;
cin >> v1 >> v2; //输入一条边依附的两个顶点
i = LocateVex(G, v1); j = LocateVex(G, v2);
//确定v1和v2在G中位置,即顶点在G.vertices中的序号
ArcNode *p1=new ArcNode; //生成一个新的边结点*p1
p1->adjvex=j; //邻接点序号为j
p1->nextarc= G.vertices[i].firstarc; G.vertices[i].firstarc=p1;
//将新结点*p1插入顶点vi的边表头部
ArcNode *p2=new ArcNode; //生成另一个对称的新的边结点*p2
p2->adjvex=i; //邻接点序号为i
p2->nextarc= G.vertices[j].firstarc; G.vertices[j].firstarc=p2;
//将新结点*p2插入顶点vj的边表头部
}//for
return OK;
}//CreateUDG
int InsertVex(ALGraph &G, VerTexType v)
{
/*******************************Begin***********************/
int n = G.vexnum;
if(n == MVNum) return ERROR;
for(int i = 0;i < n;i ++ )
if(G.vertices[i].data == v) return ERROR;
G.vertices[G.vexnum].data = v;
G.vertices[G.vexnum].firstarc = NULL;
G.vexnum ++ ;
return OK;
/******************************End**************************/
}
int DeleteVex(ALGraph &G, VerTexType v)
{
/*******************************Begin***********************/
int a = LocateVex(G,v);
if(a == -1) return ERROR;
ArcNode *p,*del,*pre;
p = G.vertices[a].firstarc;
while(p)
{
G.arcnum -- ;
p = p -> nextarc;
}
int i;
for(i = a;i < G.vexnum - 1;i ++ )
{ G.vertices[i].data = G.vertices[i+1].data;
G.vertices[i].firstarc = G.vertices[i+1].firstarc;
}
G.vertices[G.vexnum - 1].data = -1;
G.vertices[G.vexnum - 1].firstarc = NULL;
G.vexnum--;
for(i = 0;i < G.vexnum;i ++ )
{
p = G.vertices[i].firstarc;
while(p)
{
if(p -> adjvex == a)
{
if(p == G.vertices[i].firstarc)
{
del=p;
p=p->nextarc;
G.vertices[i].firstarc=p;
pre=NULL;
free(del);
G.arcnum -- ;
break;
}
else
{
del=p;
p=p->nextarc;
pre->nextarc=p;
free(del);
G.arcnum--;
break;
}
}
else if(p->adjvex > a)
{
p->adjvex--;
}
pre=p;
p=p->nextarc;
}
}
return OK;
/******************************End**************************/
}
int InsertArc(ALGraph &G, VerTexType v, VerTexType w)
{
/*******************************Begin***********************/
int a = LocateVex(G,v),b = LocateVex(G,w);
if(a == -1 || b == -1) return ERROR;
ArcNode *p = new ArcNode,*q = new ArcNode;
p -> adjvex = b;
p -> nextarc = G.vertices[a].firstarc;
G.vertices[a].firstarc = p;
G.arcnum ++ ;
q -> adjvex = a;
q -> nextarc = G.vertices[b].firstarc;
G.vertices[b].firstarc = q;
G.arcnum ++ ;
return OK;
/******************************End**************************/
}
int DeleteArc(ALGraph &G, VerTexType v, VerTexType w)
{
/**********************Begin********************************/
int a = LocateVex(G,v),b = LocateVex(G,w);
if(a == -1 || b == -1) return ERROR;
ArcNode *p = G.vertices[a].firstarc,*temp;
if(p -> adjvex == b)
{
temp = p;
free(p);
G.vertices[a].firstarc = temp -> nextarc;
}
while(p -> nextarc)
{
if(p -> nextarc -> adjvex == b)
{
temp = p -> nextarc;
p -> nextarc = temp -> nextarc;
free(temp);
break;
}
else p = p -> nextarc;
}
G.arcnum -- ;
ArcNode *q = G.vertices[b].firstarc;
if(q -> adjvex == a)
{
temp = q;
free(q);
G.vertices[b].firstarc = temp -> nextarc;
}
while(q -> nextarc)
{
if(q -> nextarc -> adjvex == a)
{
temp = q -> nextarc;
q -> nextarc = temp -> nextarc;
free(temp);
break;
}
else q = q -> nextarc;
}
G.arcnum -- ;
return OK;
/***********************End*********************************/
}
void show(ALGraph G)
{
for(int i = 0 ; i < G.vexnum ; ++i)
{
VNode temp = G.vertices[i];
ArcNode *p = temp.firstarc;
if(p == NULL)
{
cout << G.vertices[i].data;
cout << endl;
}
else
{
cout << temp.data;
while(p)
{
cout << "->";
cout << G.vertices[p->adjvex].data;
p = p->nextarc;
}
}
cout << endl;
}
}
int main()
{
ALGraph G;
CreateUDG(G);
int choose;
cin >> choose;
while(choose --)
{
string op;
cin >> op;
VerTexType v, w;
if(op == "IV")
{
cin >> v;
InsertVex(G, v);
}
else if(op == "DV")
{
cin >> v;
DeleteVex(G, v);
}
else if(op == "IA")
{
cin >> v >> w;
InsertArc(G, v, w);
}
else
{
cin >> v >> w;
DeleteArc(G, v, w);
}
}
show(G);
return 0;
}//main