图的邻接表存储,出错了

haolly 2014-02-23 01:32:33
#include "graph.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>

#define NUMNODE 1001
#define NUMEDGE 4229
typedef int GraphElementType;

//图的邻接表示法
typedef struct edgeNode
{
int adjVex; //和节点有边的另一个节点的下标
struct edgeNode* next;
}EdgeNode;

typedef struct
{
GraphElementType data;
int nodeID;
EdgeNode* edgeList;
int totalEdge;
bool isVisited;
}Node;

typedef struct
{
Node* nodeList;
int totalNodes, totalEdges;
}Graph;
int getEdgesNum(FILE* pf)
{
char line[101];
if(fgets(line,100,pf))
{
printf("%s",line);
char* num = strrchr(line,'\t');
num++;
return strToInt(num);
}
return 0;
}
int strToInt(char* str)
{
int ret = 0;
while((*str >= '0')&&(*str <= '9'))
{
ret *= 10;
ret += (*str - '0');
str++;
}
return ret;
}
void makeNodeEdges(Graph g,int i, int edgeNum)
{
printf("function makeNodeEdges,%d,%d\n",i,edgeNum);
EdgeNode* currentNode = g.nodeList[i].edgeList;
while(edgeNum)
{
EdgeNode* tmp = (EdgeNode*)malloc(sizeof(EdgeNode));
tmp->next = NULL;
tmp->adjVex = -1;
currentNode = tmp;
currentNode = currentNode->next;
edgeNum--;
}
}
void getEdges(FILE* pf, int* One, int* Two)
{
char line[101];
if(fgets(line,100,pf))
{
printf("line 52:%s\n",line);
char* nodeOne = strchr(line,'\t');
nodeOne+=2;
char* nodeTwo = strrchr(line,'\t');
nodeTwo++;
printf("nodeOne,nodeTwo,%s,%s\n",nodeOne,nodeTwo);
*One = strToInt(nodeOne);
*Two = strToInt(nodeTwo);

printf("One,two:%d,%d\n",*One,*Two);
}
}
void makeEdges(Graph g,int firstNode, int secondNode)
{
printf("%d,%d,function:makeEdges\n",firstNode,secondNode);
int i;
EdgeNode tmp;
for(i=0;i<g.nodeList[firstNode].totalEdge;i++)
{
printf("71,totalEdge:%d\n",g.nodeList[firstNode].totalEdge);
tmp = g.nodeList[firstNode].edgeList[i];//就这句出错了,,,,,不能打印下一行
printf("73\n");
if(tmp.adjVex!= -1)
{
tmp.adjVex=secondNode;
break;
}
}
}
/**
* 需要在函数调用时申请出g的空间
*/
void makeGraph(Graph g, const char* filename)
{
Node* totalNode = (Node*)malloc(sizeof(Node)*NUMNODE);
//Node* totalNode =
assert(totalNode);
g.totalNodes = NUMNODE;//总节点个数
g.totalEdges = NUMEDGE;//总边个数
g.nodeList = totalNode;

int i;
for(i=1; i<NUMNODE; i++)//从1开始,0没用
{
g.nodeList[i].data = 0; //初始化数据值为0
g.nodeList[i].nodeID = i;
g.nodeList[i].edgeList = NULL;
g.nodeList[i].isVisited = false;
}

printf("%s\n",filename);
FILE* file= fopen(filename,"r");
assert(file);
int edgeNum=0;
for(i=1;i<NUMNODE; i++)//从1开始,0没用
{
edgeNum=getEdgesNum(file);
printf("%d,104line\n",edgeNum);
g.nodeList[i].totalEdge = edgeNum;
makeNodeEdges(g,i,edgeNum);
}
int firstNode, secondNode;
for(i=0;i<NUMEDGE;i++)
{
getEdges(file,&firstNode,&secondNode);
printf("%d,%d,line114\n",firstNode,secondNode);
makeEdges(g,firstNode,secondNode);
}
}

main函数中调用makeGraph

从文件中读取图的节点和边信息,文件结构如下:
1		56
2 192
3 120
4 175
5 186
6 123
7 193
8 131
.......
第一列为节点号,第二列为与它相连接的节点个数

1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
6 6 7
7 7 8
8 8 9
........
后两列表示这两个节点直接有链接
...全文
113 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
haolly 2014-02-23
  • 打赏
  • 举报
回复
更新一下
void makeEdges(Graph g,int firstNode, int secondNode)
{
    printf("%d,%d,function:makeEdges\n",firstNode,secondNode);
	int i;
	EdgeNode* tmp;
	tmp = g.nodeList[firstNode].edgeList;
	for(i=0;i<g.nodeList[firstNode].totalEdge;i++)
	{
	    printf("71,totalEdge:%d\n",g.nodeList[firstNode].totalEdge);

		printf("73\n");
		printf("(*tmp).adjVex:%d",tmp->adjVex);
		if((*tmp).adjVex!= -1)//应该是这里出错了,上面的没有打印出来
		{
			(*tmp).adjVex=secondNode;
			break;
		}
		tmp = g.nodeList[firstNode].edgeList->next;
	}
	printf("llllll");
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧