1. 程式人生 > >資料結構:實驗八(圖的操作及入度和出度的計算)

資料結構:實驗八(圖的操作及入度和出度的計算)

題目:自己畫一張圖,建立鄰接矩陣,並求出每個頂點的度
這裡寫圖片描述

#include "AdjMGraph.h"
void CreatGraph(AdjMGraph *g, DataType V[],int n, RowColWeight E[], int e)//建立圖
{
    Initiate(g, n);
    for (int i = 0; i < n; i++)
        InsertVertex(g, V[i]);
    for (int i = 0; i < e; i++)
        InsertEdge(g, E[i].row, E[i].col, E[i].weight);
}
void GetDu(AdjMGraph g)//得到節點的度
{
    int
n = 0; printf("出度:\n"); for (int i = 0; i < g.Vertices.size; i++) { n = 0; for (int j = 0; j < g.Vertices.size; j++) { if (g.edge[i][j] != 0 && g.edge[i][j] < MaxWeight) n++; } printf("%c%d\n", g.Vertices.list[i], n); } printf
("入度:\n"); for (int j = 0; j < g.Vertices.size; j++) { n = 0; for (int i = 0; i < g.Vertices.size; i++) { if (g.edge[i][j] != 0 && g.edge[i][j] < MaxWeight) n++; } printf("%c%d\n", g.Vertices.list[j], n); } } int
main() { AdjMGraph g; DataType v[] = { 'A','B','C','D' }; RowColWeight rcw[] = { {0,1,1},{1,2,1},{2,3,1},{3,0,1},{0,2,1} }; CreatGraph(&g, v, 4, rcw, 5); printf("%頂點序列:\n"); for (int i = 0; i < g.Vertices.size; i++) printf("%c ", g.Vertices.list[i]); printf("\n\n"); printf("鄰接矩陣:\n"); for (int i = 0; i < g.Vertices.size; i++) { for (int j = 0; j < g.Vertices.size; j++) printf("(%c,%c):%5d ", g.Vertices.list[i], g.Vertices.list[j], g.edge[i][j]); printf("\n"); } printf("\n"); printf("頂點的度:\n"); GetDu(g); return 0; }

執行結果:
這裡寫圖片描述

*標頭檔案:*
SeqList:

#pragma once
#pragma once
#include "stdio.h"
#define MaxSize 100
typedef int DataType;
typedef struct {
    DataType list[MaxSize];
    int size;
}SeqList;
void ListInitiate(SeqList *L) {//初始化順序表
    L->size = 0;
}
int ListLength(SeqList L) {//返回順序表長度
    return L.size;
}
int ListInerst(SeqList *L, int i, DataType x) {//插入元素
    int j;
    if (L->size >= MaxSize) {
        printf("順序表已滿無法插入!");
        return -1;
    }
    else if (i<0 || i>L->size) {
        printf("輸入引數有誤!");
        return -1;
    }
    else {
        for (int j = L->size; j > i; j--)
            L->list[j] = L->list[j - 1];
        L->list[i] = x;
        L->size++;
        return 1;
    }
}
int ListDelete(SeqList *L, int i, DataType *x) {//刪除元素
    int j;
    if (L->size <= 0) {
        printf("順序表已空,無資料可刪!");
        return -1;
    }
    else if (i<0 || i>L->size - 1) {
        printf("輸入引數有誤!");
        return -1;
    }
    else {
        *x = L->list[i];
        for (j = i + 1; j <= L->size - 1; j++)
            L->list[j - 1] = L->list[j];
        L->size--;
        return 1;
    }
}
int ListGet(SeqList L, int i, DataType *x) {//取出索引為i處的元素
    if (i < 0 || i>L.size - 1) {
        printf("引數不合法!");
        return -1;
    }
    else {
        *x = L.list[i];
        return 1;
    }
}
int ListFind(SeqList L, DataType x) {//查詢元素並返回索引
    int i;
    bool hasFind = false;
    for (i = 0; i < L.size; i++) {
        if (x == L.list[i]) {
            return i;
            hasFind = true;
            break;
        }
    }
    if (!hasFind) {
        printf("順序表中沒有該資料!\n");
        return -1;
    }
}
int SortListInerst(SeqList *L, DataType x) {
    if (L->size == 0) {
        L->list[0] = x;
        L->size++;
        return 1;
    }
    else {
        for (int i = 0; i < L->size; i++)
            if (x < L->list[i]) {
                for (int j = L->size; j > i; j--)
                    L->list[j] = L->list[j - 1];
                L->list[i] = x;
                break;
            }
            else
                L->list[L->size] = x;
        L->size++;
        return 1;
    }
}


AdjMGraph.h

#pragma once
#include "Limits.h"
#include"SeqList.h"
#define MaxVertices 100
#define MaxWeight 100
typedef struct
{
    SeqList Vertices; //存放結點的順序表
    int edge[MaxVertices][MaxVertices];  //存放邊的鄰接矩陣
    int numOfEdges;   //邊的條數
}AdjMGraph;  //邊的結構體定義
typedef struct
{
    int row;
    int col;
    int weight;
}RowColWeight;
void Initiate(AdjMGraph *G, int n)   //初始化
{
    int i, j;
    for (i = 0; i<n; i++)
        for (j = 0; j<n; j++)
        {
            if (i == j)
                G->edge[i][j] = 0;
            else
                G->edge[i][j] = MaxWeight;
        }
    G->numOfEdges = 0;    //邊的條數置為0
    ListInitiate(&G->Vertices);  //順序表初始化
}

void InsertVertex(AdjMGraph *G, DataType vertex)  //在圖G中插入結點vertex
{
    ListInerst(&G->Vertices, G->Vertices.size, vertex);  //順序表尾插入
}

void InsertEdge(AdjMGraph *G, int v1, int v2, int weight)
//在圖G中插入邊<v1,v2>,邊<v1,v2>的權為weight
{
    if (v1<0 || v1>G->Vertices.size || v2<0 || v2>G->Vertices.size)
    {
        printf("引數v1或v2越界出錯!\n");
        return;
    }
    G->edge[v1][v2] = weight;
    G->numOfEdges++;
}

void DeleteEdge(AdjMGraph *G, int v1, int v2)  //在圖中刪除邊<v1,v2>
{
    if (v1<0 || v1>G->Vertices.size || v2<0 || v2>G->Vertices.size || v1 == v2)
    {
        printf("引數v1或v2越界出錯!\n");
        return;
    }
    if (G->edge[v1][v2] == MaxWeight || v1 == v2)
    {
        printf("該邊不存在!\n");
        return;
    }
    G->edge[v1][v2] = MaxWeight;
    G->numOfEdges--;
}

void DeleteVerten(AdjMGraph *G, int v)  //刪除結點v
{
    int n = ListLength(G->Vertices), i, j;
    DataType x;
    for (i = 0; i<n; i++)  //計算刪除後的邊數
    {
        for (j = 0; j<n; j++)
            if ((i == v || j == v) && G->edge[i][j]>0 && G->edge[i][j]<MaxWeight)
                G->numOfEdges--;  //計算被刪除邊
    }
    for (i = v; i<n; i++)  //刪除第v行
    {
        for (j = 0; j<n; j++)
            G->edge[i][j] = G->edge[i + 1][j];
    }
    for (i = 0; i<n; i++)  //刪除第v列
    {
        for (j = v; j<n; j++)
            G->edge[i][j] = G->edge[i][j + 1];
    }
    ListDelete(&G->Vertices, v, &x);  //刪除結點v
}

int GetFistVex(AdjMGraph *G, int v)
//在圖G中尋找序號為v的結點的第一個鄰接結點
//如果這樣的鄰接結點存在,返回該鄰接結點的序號;否則,返回-1
{
    int col;

    if (v<0 || v>G->Vertices.size)
    {
        printf("引數v1越界出錯!\n");
        return 0;
    }
    for (col = 0; col<G->Vertices.size; col++)
        if (G->edge[v][col]>0 && G->edge[v][col]<MaxWeight)return col;
    return -1;
}

int GetNextVex(AdjMGraph*G, int v1, int v2)
//在圖中尋找v1結點的鄰接結點v2的下一個鄰接結點
//如果這樣的結點存在,返回該鄰接結點的序號;否則,返回-1
//v1和v2都是相應結點的序號
{
    int col;
    if (v1<0 || v1>G->Vertices.size || v2<0 || v2>G->Vertices.size)
    {
        printf("引數v1或v2越界出錯!\n");
        return 0;
    }
    for (col = v2 + 1; col<G->Vertices.size; col++)
        if (G->edge[v1][col]>0 && G->edge[v1][col]<MaxWeight)return col;
    return -1;
}