Edge* tmp = *p;
while(tmp->link != NULL && tmp->link->idx != i){
tmp = tmp->link;
}
//没有找到边
if(tmp->link == NULL){
return -1;
}
//找到边
else{
f = tmp->link;
tmp->link = tmp->link->link;
free(f);
return 0;
}
}
void remove_edge(GraphLink* g, T v1, T v2){
int p1 = getVertexIndex(g, v1);
int p2 = getVertexIndex(g, v2);
if(p1 == -1 || p2 == -1)return;
int r = remove_edge_(&(g->nodeTable[p1].adj), p2);
if(r == 0){
g->NumEdges--;
remove_edge_(&(g->nodeTable[p2].adj), p1);
g->NumEdges--;
}
}
//删除顶点
void remove_vertex(GraphLink* g, T v){
int p = getVertexIndex(g, v);
if(p == -1)return;
//删除目标顶点以外的顶点,与目标顶点之间的边
for(int i = 0; i < g->NumVertices; ++i){
if(i == p){
continue;
}else{
remove_edge_(&(g->nodeTable[i].adj), p);
}
}
//删除目标顶点行的所有边
Edge* te = g->nodeTable[p].adj;
Edge* tmp;
while(te != NULL){
tmp = te;
te = te->link;
free(tmp);
}
//让被删除顶点那行,指向最后一个顶点那行。
//因为最后一行向上移动了,所以要修改和最后一行有连线的点那行的线的下标。
g->nodeTable[p].data = g->nodeTable[g->NumVertices - 1].data;
g->nodeTable[p].adj = g->nodeTable[g->NumVertices - 1].adj;
tmp = g->nodeTable[p].adj;
Edge* q;
while(tmp != NULL){
q = g->nodeTable[tmp->idx].adj;
while(q != NULL && q->idx != g->NumVertices - 1){
q = q->link;
}
q->idx = p;
tmp = tmp->link;
}
g->NumVertices--;
}
//销毁图
void destroy_graph_link(GraphLink* g){
//释放所有边的内存空间
Edge* t = NULL;
Edge* p = NULL;
for(int i = 0; i< g->NumVertices; ++i){
t = g->nodeTable[i].adj;
while(NULL != t){
p = t;
t = t->link;
free(p);
}
}
//释放所有顶点的内存空间
free(g->nodeTable);
g->nodeTable = NULL;
g->MaxVertices = g->NumVertices = g->NumEdges = 0;
}
//取得指定顶点的第一个后序顶点
int get_first_neighbor(GraphLink* g, T v){
int i = getVertexIndex(g, v);
if (-1 == i)return -1;
Edge* p = g->nodeTable[i].adj;
if(NULL != p)
return p->idx;
else
return -1;
}
//取得指定顶点v1的临街顶点v2的第一个后序顶点
int get_next_neighbor(GraphLink* g, T ve1, T ve2){
int v1 = getVertexIndex(g, ve1);
int v2 = getVertexIndex(g, ve2);
if(v1 == -1 || v2 == -1)return -1;
Edge* t = g->nodeTable[v1].adj;
while(t != NULL && t->idx != v2){
t = t->link;
}
if(NULL != t && t->link != NULL){
return t->link->idx;
}
return -1;
}
graph_linkmain.c
#include "graph_link.h"
int main(){
GraphLink gl;
//初始化图
init_graph_link(&gl);
//插入节点
insert_vertex(&gl, 'A');
insert_vertex(&gl, 'B');
insert_vertex(&gl, 'C');
insert_vertex(&gl, 'D');
insert_vertex(&gl, 'E');
//显示图
//show_graph_link(&gl);
//插入边(尾插)
/*
insert_edge_tail(&gl, 'A', 'B');
insert_edge_tail(&gl, 'A', 'D');
insert_edge_tail(&gl, 'B', 'C');
insert_edge_tail(&gl, 'B', 'E');
insert_edge_tail(&gl, 'C', 'D');
insert_edge_tail(&gl, 'C', 'E');
*/
//插入边(头插)
///*
insert_edge_head(&gl, 'A', 'B');
insert_edge_head(&gl, 'A', 'D');
insert_edge_head(&gl, 'B', 'C');
insert_edge_head(&gl, 'B', 'E');
insert_edge_head(&gl, 'C', 'D');
insert_edge_head(&gl, 'C', 'E');
//*/
//显示图
show_graph_link(&gl);
printf("\n");
//删除边
remove_edge(&gl, 'A', 'D');
//显示图
show_graph_link(&gl);
printf("\n");
//删除顶点
remove_vertex(&gl, 'C');
//显示图
show_graph_link(&gl);