圖的鄰接矩陣、鄰接表的表示

關於鄰接矩陣、鄰接表的具體包含哪些數據我就不說了,鄙人時間有點趕,大家自行百度下哈。

程序中圖的例子:
這裏寫圖片描述


2.1號:補充,突然發現我的程序,邊的權重只能爲1位數,如果權重爲多位數,只取個位數的值,因此需要進行下面的修改:(鄰接矩陣、鄰接表都一樣)
1) weight初始化爲0(以前沒有初始化)
2)從控制檯得到:weight = weight * 10 + int(c - ‘0’);(以前爲:weight = int(c - ‘0’);)
這樣weight就可以是int範圍內的任意正整數值了。

GNode.h

#define MAXSIZE 10
//鄰接矩陣
typedef struct AMGraph {
    char** vexs;
    int** arcs;
    int vexnum, arcnum;
}AMGraph;


//鄰接表

typedef struct OtherInfo {
    //權重等
    int weight;
}OtherInfo;

//邊結點
typedef struct ArcNode {
    int index;
    struct ArcNode* next;
    OtherInfo info;
}ArcNode;


//表頭結點
typedef struct VNode {
    char* data;
    struct ArcNode* first;
}VNode;

typedef struct ALGraph {
    VNode* vertices;
    int vexnum, arcnum;
}ALGraph;

main.cpp

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

#include"GNode.h"

//鄰接矩陣
void CreateAMGraph(AMGraph *g);     //創建鄰接矩陣
void GetIndex(char** vex, int size, char* resource, int &result);   //根據輸入頂點信息得到頂點的索引
bool Equal(char* s1, char* s2);     //判斷兩個字符串是否相等
void PrintAMGraphMatrix(AMGraph *g);        //輸出鄰接矩陣

//鄰接表
void CreateALGraph(ALGraph* g);     //創建鄰接表
void GetIndex_s(ALGraph* g, int size, char* resource, int &result); //根據輸入頂點信息得到頂點的索引
void PrintfALGraph(ALGraph* g);     //輸出鄰接表


int main() {

    //鄰接矩陣
    printf("*******鄰接矩陣******:\n");
    AMGraph* g;
    g = (AMGraph*)malloc(sizeof(AMGraph));
    CreateAMGraph(g);       //創建鄰接矩陣
    PrintAMGraphMatrix(g);      //輸出矩陣

    //鄰接表
    printf("\n*******鄰接表******:\n");
    ALGraph* ag;
    ag = (ALGraph *)malloc(sizeof(ALGraph));
    CreateALGraph(ag);      //創建鄰接表
    PrintfALGraph(ag);      //輸出矩陣


    return 0;
}

void CreateAMGraph(AMGraph *g) {
    printf("輸入頂點和邊的個數(例: 8 10): ");
    scanf_s("%d %d", &g->vexnum, &g->arcnum);
    getchar();      //吃掉回車
    char c = 'A';
    int i, j;
    //初始化
    g->vexs = (char **)malloc(sizeof(char *) * g->vexnum);
    g->arcs = (int **)malloc(sizeof(int *) * g->vexnum);
    for (i = 0; i < g->vexnum; i++)
        g->arcs[i] = (int *)malloc(sizeof(int) * g->vexnum);
    for (i = 0; i < g->vexnum; i++)
        for (j = 0; j < g->vexnum; j++)
            g->arcs[i][j] = 0;


    printf("按順序輸入各個頂點的數據 (例: v1 v2 v3 v4): ");
    for (i = 0; i < g->vexnum; i++) {
        g->vexs[i] = (char*)malloc(sizeof(char) * MAXSIZE);
        c = getchar();
        int j = 0;
        while ((c != ' ') && (c != '\n')) {
            g->vexs[i][j] = c;
            ++j;
            c = getchar();
        }
        g->vexs[i][j] = '\0';
    }

    /*
    for (int i = 0; i < g->vexnum; i++)
    printf("%s\n", g->vexs[i]);
    */

    printf("輸入邊信息(點1,點2,權重),(例: (v1,v2,2) (v1,v3,5)):");
    while ((c = getchar()) != '\n') {
        char v1[MAXSIZE];
        char v2[MAXSIZE];
        //**int weight;**
        int weight = 0;
        int count = 0;
        i = 0;
        if (c == '(') {
            while ((c = getchar()) != ')') {    //根據','的個數判斷怎麼賦值
                if (c == ',') {
                    ++i;
                    count = 0;
                    continue;
                }
                if (i == 0) {       //‘,’個數爲0,賦值給v1
                    v1[count++] = c;
                    v1[count] = '\0';
                }
                else if (i == 1) {      //‘,’個數爲1,賦值給v2
                    v2[count++] = c;
                    v2[count] = '\0';
                }
                else if (i == 2)        //‘,’個數爲2,賦值給weight
                    //**weight = int(c - '0');**
                    weight = weight * 10 + int(c - '0');
            }
        }
        int index1, index2;
        index1 = index2 = 0;
        GetIndex(g->vexs, g->vexnum, v1, index1);   //得到與v1中存儲信息相同的頂點索引
        GetIndex(g->vexs, g->vexnum, v2, index2);   //得到與v2中存儲信息相同的頂點索引
        if (index1 < 0 || index2 < 0)
            printf("輸入頂點不存在\n");
        else {
            //有向圖
            //g->arcs[index1][index2] = weight;

            //無向圖
            g->arcs[index1][index2] = g->arcs[index2][index1] = weight;
        }
    }
}

void GetIndex(char** vex, int size, char* resource, int &result) {
    int i;
    int temp = 0;
    for (i = 0; i < size; i++) {
        if (Equal(vex[i], resource)) {
            result = i;
            temp = 1;
            break;
        }
    }
    if (temp == 0)
        result = -1;
}

bool Equal(char* s1, char* s2) {
    int n1 = strlen(s1);
    int n2 = strlen(s2);
    if (n1 != n2)
        return false;
    int i;
    for (i = 0; i < n1; i++) {
        if (s1[i] != s2[i])
            return false;
    }
    if (i == n1)
        return true;
    return false;
}

void PrintAMGraphMatrix(AMGraph *g) {
    int** matrix = g->arcs;
    int row = g->vexnum;
    printf("\n圖對應的鄰接矩陣:\n");
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < row; j++)
            printf("%d\t", matrix[i][j]);
        printf("\n");
    }
}

void CreateALGraph(ALGraph* g) {
    printf("輸入頂點個數和邊個數(例:4 5):");
    scanf_s("%d %d", &g->vexnum, &g->arcnum);
    getchar();
    g->vertices = (VNode*)malloc(sizeof(VNode) * g->vexnum);

    char c;
    int i, j;
    printf("輸入各個頂點的數據(例:v0 v1 v2 v3):");
    for (i = 0; i < g->vexnum; i++) {
        j = 0;
        g->vertices[i].data = (char *)malloc(sizeof(char) * MAXSIZE);
        g->vertices[i].first = NULL;
        c = getchar();
        while (c != ' ' && c != '\n') {
            g->vertices[i].data[j++] = c;
            c = getchar();
        }
        g->vertices[i].data[j] = '\0';
    }

    /*for (i = 0; i < g->vexnum; i++)
    printf("%s\n", g->vertices[i].data);
    */
    printf("輸入邊數據 (例:(v0,v1,3)(v2,v3,5)):");
    char v1[MAXSIZE], v2[MAXSIZE];
    //**int weight;**
    int weight = 0;
    while ((c = getchar()) != '\n') {
        i = j = 0;
        if (c == '(') {
            while ((c = getchar()) != ')') {
                if (c == ',') {
                    ++i;
                    j = 0;
                }
                else {
                    if (i == 0) {
                        v1[j++] = c;
                        v1[j] = '\0';
                    }
                    else if (i == 1) {
                        v2[j++] = c;
                        v2[j] = '\0';
                    }
                    else if (i == 2) {
                        //**weight = int(c - '0');**
                        weight = weight * 10 + int(c - '0');
                    }
                }
            }
        }
        //printf("%s %s %d\n", v1, v2, weight);
        int index1, index2;
        GetIndex_s(g, g->vexnum, v1, index1);
        GetIndex_s(g, g->vexnum, v2, index2);
        if (index1 < 0 || index2 < 0)
            printf("輸入的頂點有誤\n");
        else {
            //有向圖 :第一個頂點爲起點,第二個頂點爲端點
            ArcNode* q = (ArcNode*)malloc(sizeof(ArcNode));
            q->index = index2;
            q->info.weight = weight;
            q->next = NULL;
            ArcNode *p = g->vertices[index1].first;
            if (p == NULL) {
                g->vertices[index1].first = q;
            }
            else {
                while (p->next) {
                    p = p->next;
                }
                p->next = q;
            }

            //無向圖 在有向圖的基礎上加上下面的
            q = (ArcNode*)malloc(sizeof(ArcNode));
            q->index = index1;
            q->info.weight = weight;
            q->next = NULL;
            p = (g->vertices[index2].first);
            if (p == NULL) {
                g->vertices[index2].first = q;
            }
            else {
                while (p->next) {
                    p = p->next;
                }
                p->next = q;
            }
        }
    }
}

void GetIndex_s(ALGraph* g, int size, char* resource, int &result) {
    int temp = 0;
    for (int i = 0; i < size; i++) {
        if (Equal(g->vertices[i].data, resource)) {
            result = i;
            temp = 1;
            break;
        }
    }
    if (temp == 0)
        result = -1;
}

void PrintfALGraph(ALGraph* g) {
    printf("\n圖對應的鄰接表:\n");
    for (int i = 0; i< g->vexnum; i++) {
        ArcNode *p = g->vertices[i].first;
        printf("頂點結點%d鏈表: ", i);
        while (p) {
            printf("(索引:%d, 權重:%d) ", p->index, p->info.weight);
            p = p->next;
        }
        printf("\n");
    }
}

這裏寫圖片描述

發佈了76 篇原創文章 · 獲贊 83 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章