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 (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;
}
}
收获
#include
#include
#include
#include
#define MAX_VERTEX_NUM 100
typedef int VertexType;
typedef enum {
DG, UDG
}GraphType;
typedef struct ArcNode
{
int adjvex;
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) {
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函数分配内存空间之后增加一个结点实现“加边”