鱼C论坛

 找回密码
 立即注册
查看: 1504|回复: 1

关于图的邻接矩阵建立,我真的改不动了

[复制链接]
发表于 2022-12-8 12:35:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//图的邻接矩阵建立
#define    N 100//图最大顶点个数
#define    MAX 32767
#include "stdio.h"

typedef struct//图的邻接矩阵类型
{
        int AdjMatrix[N][N]; //邻接矩阵
        int VexNum, ArcNum; //顶点数,弧数
        //int vexs[N];//存放顶点信息---如该顶点的下一个顶点
} AM_Graph;

//输出图的邻接矩阵
void DisplayAM(AM_Graph g)/*输出邻接矩阵*/
{
        int i, j;
        for (i = 0; i < g.VexNum; i++)
        {
                for (j = 0; j < g.VexNum; j++)
                        if (g.AdjMatrix[i][j] == MAX)
                                printf("%4s", "∞");
                        else  printf("%4d", g.AdjMatrix[i][j]);
                printf("\n");
        }
}



//图的邻接表建立
//图的邻接表存储结构描述
#include "stdlib.h"
#define VERTEX_NUM 8 //顶点数
#define ARC_NUM 9 //边数
typedef int VexType;

typedef struct AdjNode //邻接结点结构
{
        int adjvex;        //邻接点编号
        int weight;   //权值——可选项
        AdjNode* next;   // 邻接点指针
}AL_AdjNode;

typedef struct  //邻接表顶点结点结构
{
        VexType  vertex;   //顶点
        int  indegree;    //入度——此为可选项
        AdjNode* link;     // 邻接点头指针
} AL_VexNode;

typedef struct  //总的邻接表结构
{
        AL_VexNode  VexList[VERTEX_NUM];  //顶点表
        int VexNum, ArcNum;    //顶点数,弧(边)数
} AL_Graph;

//建立邻接表
AL_Graph Create_AdjList(int AdjMatrix[N][N])
{
        int G;
        AL_Graph G = { {0,NULL},VERTEX_NUM,ARC_NUM };
        int j;
        AL_AdjNode* Ptr, * nextPtr;

        for (int i = 0; i < VERTEX_NUM; i++)
        {
                G.VexList[i].vertex = i;
                G.VexList[i].link = NULL;
                j = 0;
                while (j < VERTEX_NUM)
                {
                        if (AdjMatrix[i][j] != 0)//有邻接点
                        {
                                Ptr = (AL_AdjNode*)malloc(sizeof(AL_AdjNode));
                                Ptr->adjvex = j;
                                Ptr->next = NULL;
                                if (G.VexList[i].link == NULL)//首次加入邻接点
                                {
                                        G.VexList[i].link = Ptr;
                                        nextPtr = Ptr;
                                }
                                else
                                {
                                        nextPtr->next = Ptr;
                                        nextPtr = Ptr;
                                }
                        }
                        j++;
                }
        }
        return G;
}
//调试程序
int main()
{
        //邻接表Adjacency List--AL
        int AdjMatrix[N][N] =//邻接矩阵——测试样例1
        { {0,1,1,0,0,0,0,0},
        {1,0,0,1,1,0,0,0},
        {1,0,0,0,0,1,1,0},
        {0,1,0,0,0,0,0,1},
        {0,1,0,0,0,0,0,1},
        {0,0,1,0,0,0,1,0},
        {0,0,1,0,0,1,0,0},
        {0,0,0,1,1,0,0,0} };
        /*===================
        邻接矩阵——测试样例2
        {{0,1,1,1,0,0},
        {0,0,0,0,0,0},
        {0,1,0,0,1,0},
        {0,0,0,0,1,0},
        {0,0,0,0,0,0},
        {0,0,0,1,1,0}};
        =====================*/
        AL_Graph G;
        G = Create_AdjList(AdjMatrix);
        return 0;
}


感觉没毛病,但是一运行就各种未声明的标识符,可是明明声明了,声明顺序也是对的,哭死
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-9 13:01:46 | 显示全部楼层
  1. #define    N 100//图最大顶点个数
  2. #define    MAX 32767
  3. #include "stdio.h"

  4. typedef struct//图的邻接矩阵类型
  5. {
  6.         int AdjMatrix[N][N]; //邻接矩阵
  7.         int VexNum, ArcNum; //顶点数,弧数
  8.         //int vexs[N];//存放顶点信息---如该顶点的下一个顶点
  9. } AM_Graph;

  10. //输出图的邻接矩阵
  11. void DisplayAM(AM_Graph g)/*输出邻接矩阵*/
  12. {
  13.         int i, j;
  14.         for (i = 0; i < g.VexNum; i++)
  15.         {
  16.                 for (j = 0; j < g.VexNum; j++)
  17.                         if (g.AdjMatrix[i][j] == MAX)  // CHANGE
  18.                                 printf("%4s", "∞");
  19.                         else  printf("%4d", g.AdjMatrix[i][j]);  // CHANGE
  20.                 printf("\n");
  21.         }
  22. }



  23. //图的邻接表建立
  24. //图的邻接表存储结构描述
  25. #include "stdlib.h"
  26. #define VERTEX_NUM 8 //顶点数
  27. #define ARC_NUM 9 //边数
  28. typedef int VexType;

  29. typedef struct AdjNode //邻接结点结构
  30. {
  31.         int adjvex;        //邻接点编号
  32.         int weight;   //权值——可选项
  33.         struct AdjNode* next;   // 邻接点指针  CHANGE
  34. }AL_AdjNode;

  35. typedef struct  //邻接表顶点结点结构
  36. {
  37.         VexType  vertex;   //顶点
  38.         int  indegree;    //入度——此为可选项
  39.         struct AdjNode* link;     // 邻接点头指针  CHANGE
  40. } AL_VexNode;

  41. typedef struct  //总的邻接表结构
  42. {
  43.         AL_VexNode  VexList[VERTEX_NUM];  //顶点表
  44.         int VexNum, ArcNum;    //顶点数,弧(边)数
  45. } AL_Graph;

  46. //建立邻接表
  47. AL_Graph Create_AdjList(int AdjMatrix[N][N])
  48. {
  49.         //int G;
  50.         AL_Graph G = { {0,NULL},VERTEX_NUM,ARC_NUM };
  51.         int j;
  52.         AL_AdjNode* Ptr, * nextPtr;

  53.         for (int i = 0; i < VERTEX_NUM; i++)
  54.         {
  55.                 G.VexList->vertex = i;
  56.                 G.VexList->link = NULL;
  57.                 j = 0;
  58.                 while (j < VERTEX_NUM)
  59.                 {
  60.                         if (AdjMatrix[j] != 0)//有邻接点
  61.                         {
  62.                                 Ptr = (AL_AdjNode*)malloc(sizeof(AL_AdjNode));
  63.                                 Ptr->adjvex = j;
  64.                                 Ptr->next = NULL;
  65.                                 if (G.VexList->link == NULL)//首次加入邻接点
  66.                                 {
  67.                                         G.VexList->link = Ptr;
  68.                                         nextPtr = Ptr;
  69.                                 }
  70.                                 else
  71.                                 {
  72.                                         nextPtr->next = Ptr;
  73.                                         nextPtr = Ptr;
  74.                                 }
  75.                         }
  76.                         j++;
  77.                 }
  78.         }
  79.         return G;
  80. }
  81. //调试程序
  82. int main()
  83. {
  84.         //邻接表Adjacency List--AL
  85.         int AdjMatrix[N][N] =//邻接矩阵——测试样例1
  86.         { {0,1,1,0,0,0,0,0},
  87.         {1,0,0,1,1,0,0,0},
  88.         {1,0,0,0,0,1,1,0},
  89.         {0,1,0,0,0,0,0,1},
  90.         {0,1,0,0,0,0,0,1},
  91.         {0,0,1,0,0,0,1,0},
  92.         {0,0,1,0,0,1,0,0},
  93.         {0,0,0,1,1,0,0,0} };
  94.         /*===================
  95.         邻接矩阵——测试样例2
  96.         {{0,1,1,1,0,0},
  97.         {0,0,0,0,0,0},
  98.         {0,1,0,0,1,0},
  99.         {0,0,0,0,1,0},
  100.         {0,0,0,0,0,0},
  101.         {0,0,0,1,1,0}};
  102.         =====================*/
  103.         AL_Graph G;
  104.         G = Create_AdjList(AdjMatrix);
  105.         return 0;
  106. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 06:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表