用邻接表建立图

#include <iostream>
#include <stdlib.h>
using namespace std;
#define MaxVertexNum 100


typedef int VertexType;
typedef int EdgeType;


//邻接矩阵表示 
typedef struct MGraph{
VertexType Vex[MaxVertexNum];
EdgeType Edge[MaxVertexNum][MaxVertexNum];
int vexnum,arcnum;
};


//边表 
typedef struct ArcNode{
VertexType data;
struct ArcNode *next;
}ArcNode;


//节点表 
typedef struct VNode{
VertexType data;
ArcNode *firstArcNode; 
}VNode, AdjList[MaxVertexNum];


//邻接表表示 
typedef struct ALGraph{
AdjList vertices;
int vexnum,arcnum;
}ALGraph;


//返回-1表示当前点不存在,返回其他值为当前点的位置 
int isVertexExist(ALGraph &graph, VertexType node)
{
for(int i = 0; i < graph.vexnum; i++)
{
if(graph.vertices[i].data == node) return i;
}
return -1;
}


bool isArcExist(ALGraph &graph, VertexType start_node, VertexType end_node)
{
int start_node_posi = isVertexExist(graph,start_node);
ArcNode *arcNode = graph.vertices[start_node_posi].firstArcNode;
while(arcNode != NULL)
{
if(arcNode->data == end_node) return true;
arcNode = arcNode->next;
}
return false;
}


void insertNode(ALGraph &graph, VertexType node){

//判重
if(isVertexExist(graph,node) != -1) 
{
cout<<"Node:"<<node<<" existed!"<<endl; 
return; 
} 
 
//在节点表中添加当前点 
graph.vertices[graph.vexnum].data = node;
graph.vertices[graph.vexnum++].firstArcNode = NULL;
}


void addArch(ALGraph &graph, VertexType start_node, VertexType end_node)
{
//判断添加的弧的起点是否存在,存在则继续 
//start_node_posi == -1 表示这个点不存在
int start_node_posi = isVertexExist(graph,start_node);
int end_node_posi = isVertexExist(graph,end_node);
if((start_node_posi == -1)||(end_node_posi == -1)) return;

//如果该弧已经存在 
if(isArcExist(graph,start_node,end_node)) return;

//新建一个新的ArcNode 
ArcNode *new_ArcNode, *arcNode;
new_ArcNode = (ArcNode*)malloc(sizeof(ArcNode));
new_ArcNode->data = end_node;
new_ArcNode->next = NULL;

//将当前ArcNode添加进当前节点的链表尾部
if(graph.vertices[start_node_posi].firstArcNode == NULL)
{
graph.vertices[start_node_posi].firstArcNode = new_ArcNode;
}
else
{
arcNode = graph.vertices[start_node_posi].firstArcNode;
while(arcNode->next != NULL)
{
arcNode = arcNode->next;
}
arcNode->next = new_ArcNode;
}
graph.arcnum++;

} 


void initGraph(ALGraph &graph)
{
graph.arcnum = 0;
graph.vexnum = 0;
}


void delNode(ALGraph &graph, VertexType del_node)
{
ArcNode *arcNode, *temp_arcNode;
int temp_node, temp_posi;

int del_node_posi = isVertexExist(graph,del_node);
if(del_node_posi == -1) return;

for(int i = 0; i < graph.vexnum; i++)
{
if(i == del_node_posi) continue;

arcNode = graph.vertices[i].firstArcNode;
if(arcNode == NULL)
{
//cout<<"A"<<endl;
}
else if(arcNode->data == del_node)
{
//cout<<"B"<<endl;
graph.vertices[i].firstArcNode = arcNode->next;
free(arcNode);
graph.arcnum--;
}
else
{
//cout<<"C"<<endl;
temp_arcNode = arcNode;
arcNode = arcNode->next;
while(arcNode != NULL)
{
if(arcNode->data == del_node)
{
temp_arcNode->next = arcNode->next;
free(arcNode);
graph.arcnum--;
}
temp_arcNode = arcNode;
arcNode = arcNode->next;
}
}

}

arcNode = graph.vertices[del_node_posi].firstArcNode;
while(arcNode != NULL)
{
temp_arcNode = arcNode;
arcNode = arcNode->next;
free(temp_arcNode);
graph.arcnum--;
}

//删除该结点
for(int i = del_node_posi; i < graph.vexnum; i++)
{
graph.vertices[i] = graph.vertices[i+1];
}
graph.vexnum--; 
}


void traverse_VNode(ALGraph &graph)
{
cout<<"当前的图节点表遍历结果为:"; 
for(int i = 0; i < graph.vexnum; i++)
{
cout<<graph.vertices[i].data<<"->";
}
cout<<"end"<<endl;
}


void traverse_ArcNode(ALGraph &graph, VertexType start_node)
{
ArcNode *arcNode;

int start_node_posi = isVertexExist(graph,start_node); 
if(start_node_posi == -1) return;

arcNode = graph.vertices[start_node_posi].firstArcNode;
cout<<"以结点 "<<start_node<<" 为起点的边表遍历为:"; 
while(arcNode != NULL)
{
cout<<arcNode->data<<"->";
arcNode = arcNode->next;
}
cout<<"end"<<endl;
}


void traverse_Graph(ALGraph &graph)
{
for(int i = 0; i < graph.vexnum; i++)
{
traverse_ArcNode(graph,graph.vertices[i].data);
}
cout<<"--------------Statistics-------------"<<endl;
cout<<"               节点数:"<<graph.vexnum<<endl;
cout<<"               边  数:"<<graph.arcnum<<endl; 
cout<<"--------------Statistics-------------"<<endl;
}


int main()
{
ALGraph g;
initGraph(g);
insertNode(g,1);
insertNode(g,2);
insertNode(g,3);
insertNode(g,4);
insertNode(g,5);
insertNode(g,6);
traverse_VNode(g);

addArch(g,1,2);
addArch(g,5,4);
addArch(g,1,4);
addArch(g,2,5);
addArch(g,3,5);
addArch(g,3,6);
addArch(g,1,2);
addArch(g,4,2);
addArch(g,5,4);
addArch(g,6,6);
traverse_Graph(g);
delNode(g,5);
traverse_VNode(g);
traverse_Graph(g);
return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值