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);
bool insert_vertex(ListGraph *G, VertexType v);
bool insert_arc(ListGraph *G, VertexType v, VertexType w);
答案
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) {
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