邻接表1(C语言)(图)(icoding)

//试在邻接表存储结构上实现图的基本操作 insert_vertex 和 insert_arc,相关定义如下:

typedef int VertexType;

typedef enum{
    DG, UDG
}GraphType;

typedef struct ArcNode
{
    int adjvex;
    InfoPtr *info;
    struct ArcNode *nextarc;

}ArcNode;

typedef struct VNode
{
    VertexType data;
    ArcNode *firstarc;
}VNode;
typedef struct
{
    VNode vertex[MAX_VERTEX_NUM];
    int vexnum, arcnum;
    GraphType type;
}ListGraph;

int locate_vertex(ListGraph* G, VertexType v); //返回顶点 v 在vertex数组中的下标,如果v不存在,返回-1
bool insert_vertex(ListGraph *G, VertexType v);
bool insert_arc(ListGraph *G, VertexType v, VertexType w);

//当成功插入顶点或边时,函数返回true,否则(如顶点或边已存在、插入边时顶点v或w不存在)返回false。

答案

bool insert_vertex(ListGraph* G, VertexType v) {
	if (G->vexnum == MAX_VERTEX_NUM)//判断边的数目是否已达最大数量
		return false;
	for (int i = 0;i<G->vexnum;i++) {
		if (G->vertex[i].data == v)//判断v是否已在表头结点表中
			return false;
	}
	G->vertex[G->vexnum].data = v;//添加结点
	G->vertex[G->vexnum].firstarc = NULL;
	G->vexnum++;
	return true;
}
bool insert_arc(ListGraph* G, VertexType v, VertexType w) {//假设v是弧尾,w是弧头
	int locv = locate_vertex(G, v), locw = locate_vertex(G, w);
	if (locv == -1 || locw == -1)//判断v或w是否不存在
		return false;
	ArcNode* p=G->vertex[locv].firstarc, * p_pre = NULL;
	if (p == NULL) {
		G->vertex[locv].firstarc = (ArcNode*)malloc(sizeof(ArcNode));
		G->vertex[locv].firstarc->adjvex = w;
		G->vertex[locv].firstarc->nextarc = NULL;
	}
	else {
		while (p != NULL) {
			if (p->adjvex == w)
				return false;//即使是无向图,通常两条边是成对存在的,因此此处返回false也是对的(交上去是对的
			else {
				p_pre = p;
				p = p->nextarc;
			}
		}
		p_pre->nextarc= (ArcNode*)malloc(sizeof(ArcNode));//已经遍历到末尾了,需要在链表末尾再分配一块内存空间以“加边”
		p_pre->nextarc->adjvex = w;
		p_pre->nextarc->nextarc = NULL;
	}

	if (G->type==DG) {
		++G->arcnum;
		return true;
	}
	else {
	     p = G->vertex[locw].firstarc,  p_pre = NULL;	
		if (p == NULL) {
			G->vertex[locw].firstarc = (ArcNode*)malloc(sizeof(ArcNode));
			G->vertex[locw].firstarc->adjvex = v;
			G->vertex[locw].firstarc->nextarc = NULL;
		}
		else {
			while (p != NULL) {
			if (p->adjvex == v)
				return false;
			else {
				p_pre = p;
				p = p->nextarc;
			}
		}
			p_pre->nextarc = (ArcNode*)malloc(sizeof(ArcNode));
			p_pre->nextarc->adjvex = v;
			p_pre->nextarc->nextarc = NULL;
		}
		G->arcnum += 2;
		return true;
	}
}

收获

  • 用于调试的完整代码
#include 
#include 
#include 
#include 
#define MAX_VERTEX_NUM 100

typedef int VertexType;
typedef enum {
DG, UDG
}GraphType;
typedef struct ArcNode
{
int adjvex;
//InfoPtr* info;
struct ArcNode* nextarc;
}ArcNode;
typedef struct VNode {
VertexType data;
ArcNode* firstarc;//指向该顶点第一条弧或边的指针
}VNode;
typedef struct {
VNode vertex[MAX_VERTEX_NUM];
int vexnum, arcnum;
GraphType type;
}ListGraph;

int locate_vertex(ListGraph* G, VertexType v) {
for (int i = 0;i < G->vexnum;i++) {
if (G->vertex[i].data == v)
return i;
}
return false;
}
bool insert_vertex(ListGraph* G, VertexType v) {
if (G->vexnum == MAX_VERTEX_NUM)
return false;
for (int i = 0;i<G->vexnum;i++) {
if (G->vertex[i].data == v)
return false;
}
G->vertex[G->vexnum].data = v;
G->vertex[G->vexnum].firstarc = NULL;
G->vexnum++;
return true;
}
bool insert_arc(ListGraph* G, VertexType v, VertexType w) {//假设v是弧尾,w是弧头
int locv = locate_vertex(G, v), locw = locate_vertex(G, w);
if (locv == -1 || locw == -1)
return false;
ArcNode* p=G->vertex[locv].firstarc, * p_pre = NULL;
if (p == NULL) {
G->vertex[locv].firstarc = (ArcNode*)malloc(sizeof(ArcNode));
G->vertex[locv].firstarc->adjvex = w;
G->vertex[locv].firstarc->nextarc = NULL;
}
else {
while (p != NULL) {
if (p->adjvex == w)
return false;
else {
p_pre = p;
p = p->nextarc;
}
}
p_pre->nextarc= (ArcNode*)malloc(sizeof(ArcNode));
p_pre->nextarc->adjvex = w;
p_pre->nextarc->nextarc = NULL;
}

if (G->type==DG) {
++G->arcnum;
return true;
}
else {
     p = G->vertex[locw].firstarc,  p_pre = NULL;
if (p == NULL) {
G->vertex[locw].firstarc = (ArcNode*)malloc(sizeof(ArcNode));
G->vertex[locw].firstarc->adjvex = v;
G->vertex[locw].firstarc->nextarc = NULL;
}
else {
while (p != NULL) {
if (p->adjvex == v)
return false;//
else {
p_pre = p;
p = p->nextarc;
}
}
p_pre->nextarc = (ArcNode*)malloc(sizeof(ArcNode));
p_pre->nextarc->adjvex = v;
p_pre->nextarc->nextarc = NULL;
}
G->arcnum += 2;
return true;
}
}

int main(void) {
freopen("in.txt", "r", stdin);
int vexnum = 0, arcnum = 0, i = 0,del=0;
ListGraph* G;
G = (ListGraph*)malloc(sizeof(ListGraph));
memset(G, 0, sizeof(ListGraph));
scanf("%d %d %d", &vexnum, &arcnum, &G->type);//输入结点数,边数,图的类型
for (i = 0;i < vexnum;i++) {
int v;
scanf("%d", &v);
insert_vertex(G, v);
}
for (i = 0;i < arcnum;i++) {
int v, w;
scanf("%d %d", &v, &w);
insert_arc(G, v, w);
}
scanf("%d", &del);//输入要删除的结点
del_vertex(G, del);
return 0;
}
  • 注意malloc函数的使用,只有用malloc函数分配内存空间之后增加一个结点实现“加边”

你可能感兴趣的:(邻接表1(C语言)(图)(icoding))