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

题目

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

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 del_vertex(ListGraph *G, VertexType v); //删除顶点 v

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

答案

bool del_vertex(ListGraph* G, VertexType v)
{
    bool flg = false;//要删除的结点是否存在的标志
    int loc = locate_vertex(G, v);

    for (int i = 0; i < G->vexnum; i++) { //删除边链表中的结点
        ArcNode *cur = G->vertex[i].firstarc, *pre = NULL, *cur_del = NULL;
        if (G->vertex[i].data == v) {
            flg = true;

            while (cur != NULL) {//删除结点cur
                cur_del = cur;
                cur = cur->nextarc;
                free(cur_del);
                --G->arcnum;
            }
            for (int j = i; j < G->vexnum; j++) {//将后续结点的数据以及指针域依次“覆盖”到前面
                G->vertex[j].data = G->vertex[j + 1].data;
                G->vertex[j].firstarc = G->vertex[j + 1].firstarc;
            }
            --G->vexnum;//结点数减1
            G->vertex[G->vexnum].data = 0;//清空原表头结点表的最后一个结点以及相应边链表
            G->vertex[G->vexnum].firstarc = NULL;
            --i;
        } else {
            while (cur != NULL) {//删除边表中与v相连的边
                if (cur->adjvex == loc) {
                    if (pre == NULL) {
                        cur = cur->nextarc;
                        free(G->vertex[i].firstarc);
                        G->vertex[i].firstarc = cur;
                    } else {
                        pre = cur;
                        cur = cur->nextarc;
                        pre->nextarc = cur;
                        free(pre);
                    }
                    G->arcnum--;
                    break;
                } else {
                    pre = cur;
                    cur = cur->nextarc;
                }
            }
        }
    }
    return flg;
}

收获

用于调试的完整代码

#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;
}
}

bool del_vertex(ListGraph* G, VertexType v)
{
    bool flg = false;
    int loc = locate_vertex(G, v);

    for (int i = 0; i < G->vexnum; i++) { //删除边链表中的结点
        ArcNode *cur = G->vertex[i].firstarc, *pre = NULL, *cur_del = NULL;
        if (G->vertex[i].data == v) {
            flg = true;

            while (cur != NULL) {
                cur_del = cur;
                cur = cur->nextarc;
                free(cur_del);
                --G->arcnum;
            }
            for (int j = i; j < G->vexnum; j++) {
                G->vertex[j].data = G->vertex[j + 1].data;
                G->vertex[j].firstarc = G->vertex[j + 1].firstarc;
            }
            --G->vexnum;
            G->vertex[G->vexnum].data = 0;
            G->vertex[G->vexnum].firstarc = NULL;
            --i;
        } else {
            while (cur != NULL) {
                if (cur->adjvex == loc) {
                    if (pre == NULL) {
                        cur = cur->nextarc;
                        free(G->vertex[i].firstarc);
                        G->vertex[i].firstarc = cur;
                    } else {
                        pre = cur;
                        cur = cur->nextarc;
                        pre->nextarc = cur;
                        free(pre);
                    }
                    G->arcnum--;
                    break;
                } else {
                    pre = cur;
                    cur = cur->nextarc;
                }
            }
        }
    }
    return flg;
}

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;
}

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