|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
1、代码:
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXVEX 100
- #define NULL ((void *)0)
- typedef struct EdgeNode // 边表结点
- {
- int adjvex; // 邻接点域,存储该顶点对应的下标
- int weight; // 用于存储权值,对于非网图可以不需要
- struct EdgeNode *next; // 链域,指向下一个邻接点
- } EdgeNode;
- typedef struct VertexNode // 顶点表结点
- {
- char data; // 顶点域,存储顶点信息
- EdgeNode *firstEdge; // 边表头指针
- } VertexNode, AdjList[MAXVEX];
- typedef struct
- {
- AdjList adjList;
- int numVertexes, numEdges; // 图中当前顶点数和边数
- } GraphAdjList;
- // 建立图的邻接表结构
- void CreateALGraph(GraphAdjList *G)
- {
- int i, j, k;
- EdgeNode *e;
-
- printf("请输入顶点数和边数:\n");
- scanf("%d %d", &G->numVertexes, &G->numEdges);
-
- // 读取顶点信息,建立顶点表
- for( i=0; i < G->numVertexes; i++ )
- {
- scanf("%c", &G->adjList[i].data);
- G->adjList[i].firstEdge = NULL; // 初始化置为空表
- }
-
- for( k=0; k < G->numEdges; k++ )
- {
- printf("请输入边(Vi,Vj)上的顶点序号:\n");
- scanf("%d %d", &i, &j);
-
- e = (EdgeNode *)malloc(sizeof(EdgeNode));
- e->adjvex = j; // 邻接序号为j
- e->next = G->adjList[i].firstEdge;
- G->adjList[i].firstEdge = e;
-
- e = (EdgeNode *)malloc(sizeof(EdgeNode));
- e->adjvex = i; // 邻接序号为i
- e->next = G->adjList[j].firstEdge;
- G->adjList[j].firstEdge = e;
- }
- }
- int main(){
-
- GraphAdjList * G;
-
- CreateALGraph(G);
-
- return 0;
- }
复制代码
2、问题描述:
代码是小甲鱼数据结构与算法图的存储结构那一节的代码,就是用邻接表的形式创建一个图,在原来代码的基础上,我把库和main函数加上去了,能运行:
但是输入顶点和边的数目就动不了了,然后就结束了。我试着调试一下,直接就弹出了program received signal sigsegv,我去网上查了一些好像是什么指针越界,反正挺复杂的,我实在不太明白,不知道这个代码怎么才能运行起来。
- 1>c:\visualstudioprojects\c\c\main.c(61): warning C4700: 使用了未初始化的局部变量“G”
复制代码
你的编译器没有告诉你吗?
|
|