邻接链表显示未赋值 赋值完后又显示访问冲突!改了好几遍也不对 求助!

不想变蠢的蠢 2017-01-04 03:38:25
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define INF 32767
#define MAXV 100

//邻接矩阵

typedef int InfoType;
typedef struct
{
int no;
InfoType info;
}VertexType;
typedef struct
{
int n, e;
int edges[MAXV][MAXV];
VertexType vexs[MAXV];
}MGraph;


//邻接链表

typedef struct ANode
{
int adjvex;
struct ANode *nextarc;
InfoType info;

}ArcNode;

typedef int Vertex;
typedef struct
{
Vertex data;
int count;
ArcNode *firstarc;
}VNode;

typedef VNode AdjList[MAXV];
typedef struct
{
AdjList adjlist;
int n, e;
} ALGraph;


void ArrayToMat(int *A, int n, MGraph g);
void ArrayToList(int *A, int n, ALGraph G);
//void DispMat(MGraph g,ALGraph *&G);
//void DispAdj(ALGraph *G,MGraph &g);
void DispMat(MGraph g);
void DispAdj(ALGraph G);

void ArrayToMat(int *A, int n, MGraph *g)
{
int i, j, count=0;
g->n = n;
for (i = 0; i<g->n; i++)
{
for (j = 0; j<g->n; j++)
{
g->edges[i][j] = A[i*n + j];
if (g->edges[i][j] != 0 && g->edges[i][j] != INF)
{
count++;
}
}
}
g->e = count;
}

void ArrayToList(int *A, int n, ALGraph *G)
{
int i, j, count = 0;
ArcNode *p;
G = (ALGraph *)malloc(sizeof(ALGraph));
G->n = n;
for (i = 0; i<n; i++)
{
G->adjlist[i].firstarc = NULL;
}
for (i = 0; i<n; i++)
{
for (j = n - 1; j >= 0; j++)
{
if (A[i*n + j] != 0)
{
p = (ArcNode*)malloc(sizeof(ArcNode));
p->adjvex = j;
p->info = A[i*n + j];
p->nextarc = G->adjlist[i].firstarc;
G->adjlist[i].firstarc = p;
count++;
}
}
}
G->e = count;
}


void DispMat(MGraph *g)
{
int i, j;
for (i = 0; i<g->n; i++)
{
for (j = 0; j<g->n; j++)
{
if (g->edges[i][j] == INF)
{
printf("oo");
}
else
{
printf("%d", g->edges[i][j]);
}
}
printf("\n");
}
}


void DispAdj(ALGraph *G)
{
int i, j;
ArcNode *p;
for (i = 0; i<G->n; i++)
{
p = G->adjlist[i].firstarc;
while (p != NULL)
{
printf("%d%d", p->adjvex, p->info);
p = p->nextarc;
}
printf("\n");
}
}





int main(void)
{
MGraph *g1, *g2;
ALGraph *G1, *G2;
int A[6][6] =
{ { 0, 5, 0, 7, 0, 0 },
{ 0, 0, 4, 0, 0, 0 },
{ 8, 0, 0, 0, 0, 9 },
{ 0, 0, 5, 0, 0, 6 },
{ 0, 0, 0, 5, 0, 0 },
{ 3, 0, 0, 0, 1, 0 }
};
ArrayToMat(A[0], 6, g1);
printf("有向图的邻接矩阵:\n");
DispMat(g1);

ArrayToList(A[0], 6, G1);
printf("有向图的邻接表:\n");
DispAdj(G1);

//MatToList(g1,G2);
//printf("邻接矩阵转换成邻接表:\n");
//DisAdj(G2);

//ListToMat(G1,g2);
//printf("邻接表转换成邻接矩阵:\n");
//DisMat(g2);

system("pause");
return 0;
}
...全文
144 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
不想变蠢的蠢 2017-01-05
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
“多一少一”问题占程序员常犯错误的10%以上! 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断,掰手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个? <、<=、==、>、>=中的哪个?
赵老师说得好!记下了!
不想变蠢的蠢 2017-01-05
  • 打赏
  • 举报
回复
引用 4 楼 qq423399099 的回复:
1.DispMat(g1);应该是DispMat(&g1); 2.ArrayToMat函数中*g也需要malloc 3.ArrayToList中第二个for循环应该是j--而不是j++

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define INF 32767
#define MAXV 100

//邻接矩阵

typedef int InfoType;
typedef struct
{
	int no;
	InfoType info;
}VertexType;
typedef struct
{
	int n, e;
	int edges[MAXV][MAXV];
	VertexType vexs[MAXV];
}MGraph;


//邻接链表

typedef struct ANode
{
	int adjvex;
	struct ANode *nextarc;
	InfoType info;

}ArcNode;

typedef int Vertex;
typedef struct
{
	Vertex data;
	int count;
	ArcNode *firstarc;
}VNode;

typedef VNode AdjList[MAXV];
typedef struct
{
	AdjList adjlist;
	int n, e;
} ALGraph;


void ArrayToMat(int *A, int n, MGraph **g);
void ArrayToList(int *A, int n, ALGraph **G);
//void DispMat(MGraph g,ALGraph *&G);
//void DispAdj(ALGraph *G,MGraph &g);
void DispMat(MGraph **g);
void DispAdj(ALGraph **G);

void ArrayToMat(int *A, int n, MGraph **g)
{
	int i, j, count = 0;
	*g = (MGraph *)malloc(sizeof(MGraph));
	(*g)->n = n;
	for (i = 0; i<(*g)->n; i++)
	{
		for (j = 0; j<(*g)->n; j++)
		{
			(*g)->edges[i][j] = A[i*n + j];
			if ((*g)->edges[i][j] != 0 && (*g)->edges[i][j] != INF)
			{
				count++;
			}
		}
	}
	(*g)->e = count;
}

void ArrayToList(int *A, int n, ALGraph **G)
{
	int i, j, count = 0;
	ArcNode *p;
	*G = (ALGraph *)malloc(sizeof(ALGraph));
	(*G)->n = n;
	for (i = 0; i<n; i++)
	{
		(*G)->adjlist[i].firstarc = NULL;
	}
	for (i = 0; i<n; i++)
	{
		for (j = n - 1; j >= 0; j--)
		{
			if (A[i*n + j] != 0)
			{
				p = (ArcNode*)malloc(sizeof(ArcNode));
				p->adjvex = j;
				p->info = A[i*n + j];
				p->nextarc = (*G)->adjlist[i].firstarc;
				(*G)->adjlist[i].firstarc = p;
				count++;
			}
		}
	}
	(*G)->e = count;
}


void DispMat(MGraph **g)
{
	int i, j;
	for (i = 0; i<(*g)->n; i++)
	{
		for (j = 0; j<(*g)->n; j++)
		{
			if ((*g)->edges[i][j] == INF)
			{
				printf("oo");
			}
			else
			{
				printf("%d", (*g)->edges[i][j]);
			}
		}
		printf("\n");
	}
}


void DispAdj(ALGraph **G)
{
	int i, j;
	ArcNode *p;
	for (i = 0; i<(*G)->n; i++)
	{
		p = (*G)->adjlist[i].firstarc;
		while (p != NULL)
		{
			printf("%d%d", p->adjvex, p->info);
			p = p->nextarc;
		}
		printf("\n");
	}
}





int main(void)
{
	MGraph *g1, *g2;
	ALGraph *G1, *G2;
	int A[6][6] =
	{ { 0, 5, 0, 7, 0, 0 },
	{ 0, 0, 4, 0, 0, 0 },
	{ 8, 0, 0, 0, 0, 9 },
	{ 0, 0, 5, 0, 0, 6 },
	{ 0, 0, 0, 5, 0, 0 },
	{ 3, 0, 0, 0, 1, 0 }
	};
	ArrayToMat(A[0], 6, &g1);
	printf("有向图的邻接矩阵:\n");
	DispMat(&g1);

	ArrayToList(A[0], 6, &G1);
	printf("有向图的邻接表:\n");
	DispAdj(&G1);

	//MatToList(g1,G2);
	//printf("邻接矩阵转换成邻接表:\n");
	//DisAdj(G2);

	//ListToMat(G1,g2);
	//printf("邻接表转换成邻接矩阵:\n");
	//DisMat(g2);

	system("pause");
	return 0;
}
malloc错误找出来了!j++那个错误我问老师, 老师对着这个断点看了10分钟告诉我她需要静下心来慢慢看 。多亏层主提醒 要不然我得醉死在这上面 太粗心了 。
赵4老师 2017-01-05
  • 打赏
  • 举报
回复
“多一少一”问题占程序员常犯错误的10%以上! 避免“多一少一”问题的方法之一是将比如<10甚至<5的数代入程序片断,掰手指头心算验证一下程序到底应该写为 x、x-1、x+1中的哪个? <、<=、==、>、>=中的哪个?
小灸舞 版主 2017-01-05
  • 打赏
  • 举报
回复
1.DispMat(g1);应该是DispMat(&g1); 2.ArrayToMat函数中*g也需要malloc 3.ArrayToList中第二个for循环应该是j--而不是j++

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define INF 32767
#define MAXV 100

//邻接矩阵

typedef int InfoType;
typedef struct
{
	int no;
	InfoType info;
}VertexType;
typedef struct
{
	int n, e;
	int edges[MAXV][MAXV];
	VertexType vexs[MAXV];
}MGraph;


//邻接链表

typedef struct ANode
{
	int adjvex;
	struct ANode *nextarc;
	InfoType info;

}ArcNode;

typedef int Vertex;
typedef struct
{
	Vertex data;
	int count;
	ArcNode *firstarc;
}VNode;

typedef VNode AdjList[MAXV];
typedef struct
{
	AdjList adjlist;
	int n, e;
} ALGraph;


void ArrayToMat(int *A, int n, MGraph **g);
void ArrayToList(int *A, int n, ALGraph **G);
//void DispMat(MGraph g,ALGraph *&G);
//void DispAdj(ALGraph *G,MGraph &g);
void DispMat(MGraph **g);
void DispAdj(ALGraph **G);

void ArrayToMat(int *A, int n, MGraph **g)
{
	int i, j, count = 0;
	*g = (MGraph *)malloc(sizeof(MGraph));
	(*g)->n = n;
	for (i = 0; i<(*g)->n; i++)
	{
		for (j = 0; j<(*g)->n; j++)
		{
			(*g)->edges[i][j] = A[i*n + j];
			if ((*g)->edges[i][j] != 0 && (*g)->edges[i][j] != INF)
			{
				count++;
			}
		}
	}
	(*g)->e = count;
}

void ArrayToList(int *A, int n, ALGraph **G)
{
	int i, j, count = 0;
	ArcNode *p;
	*G = (ALGraph *)malloc(sizeof(ALGraph));
	(*G)->n = n;
	for (i = 0; i<n; i++)
	{
		(*G)->adjlist[i].firstarc = NULL;
	}
	for (i = 0; i<n; i++)
	{
		for (j = n - 1; j >= 0; j--)
		{
			if (A[i*n + j] != 0)
			{
				p = (ArcNode*)malloc(sizeof(ArcNode));
				p->adjvex = j;
				p->info = A[i*n + j];
				p->nextarc = (*G)->adjlist[i].firstarc;
				(*G)->adjlist[i].firstarc = p;
				count++;
			}
		}
	}
	(*G)->e = count;
}


void DispMat(MGraph **g)
{
	int i, j;
	for (i = 0; i<(*g)->n; i++)
	{
		for (j = 0; j<(*g)->n; j++)
		{
			if ((*g)->edges[i][j] == INF)
			{
				printf("oo");
			}
			else
			{
				printf("%d", (*g)->edges[i][j]);
			}
		}
		printf("\n");
	}
}


void DispAdj(ALGraph **G)
{
	int i, j;
	ArcNode *p;
	for (i = 0; i<(*G)->n; i++)
	{
		p = (*G)->adjlist[i].firstarc;
		while (p != NULL)
		{
			printf("%d%d", p->adjvex, p->info);
			p = p->nextarc;
		}
		printf("\n");
	}
}





int main(void)
{
	MGraph *g1, *g2;
	ALGraph *G1, *G2;
	int A[6][6] =
	{ { 0, 5, 0, 7, 0, 0 },
	{ 0, 0, 4, 0, 0, 0 },
	{ 8, 0, 0, 0, 0, 9 },
	{ 0, 0, 5, 0, 0, 6 },
	{ 0, 0, 0, 5, 0, 0 },
	{ 3, 0, 0, 0, 1, 0 }
	};
	ArrayToMat(A[0], 6, &g1);
	printf("有向图的邻接矩阵:\n");
	DispMat(&g1);

	ArrayToList(A[0], 6, &G1);
	printf("有向图的邻接表:\n");
	DispAdj(&G1);

	//MatToList(g1,G2);
	//printf("邻接矩阵转换成邻接表:\n");
	//DisAdj(G2);

	//ListToMat(G1,g2);
	//printf("邻接表转换成邻接矩阵:\n");
	//DisMat(g2);

	system("pause");
	return 0;
}
不想变蠢的蠢 2017-01-05
  • 打赏
  • 举报
回复
引用 1 楼 qq423399099 的回复:
ArrayToMat(A[0], 6, g1);你这样传参是不行的。 你想通过ArrayToMat函数改变g1的指向是不行的,必须传二级指针才行(实参和形参并不是同一个指针,他们只是指向了同一块内存)
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define INF 32767
#define MAXV 100

//邻接矩阵

typedef int InfoType;
typedef struct
{
	int no;
	InfoType info;
}VertexType;
typedef struct
{
	int n, e;
	int edges[MAXV][MAXV];
	VertexType vexs[MAXV];
}MGraph;


//邻接链表

typedef struct ANode
{
	int adjvex;
	struct ANode *nextarc;
	InfoType info;

}ArcNode;

typedef int Vertex;
typedef struct
{
	Vertex data;
	int count;
	ArcNode *firstarc;
}VNode;

typedef VNode AdjList[MAXV];
typedef struct
{
	AdjList adjlist;
	int n, e;
} ALGraph;


void ArrayToMat(int *A, int n, MGraph **g);
void ArrayToList(int *A, int n, ALGraph **G);
//void DispMat(MGraph g,ALGraph *&G);
//void DispAdj(ALGraph *G,MGraph &g);
void DispMat(MGraph **g);
void DispAdj(ALGraph **G);

void ArrayToMat(int *A, int n, MGraph **g)
{
	int i, j, count=0;
	(*g)->n = n;
	for (i = 0; i<(*g)->n; i++)
	{
		for (j = 0; j<(*g)->n; j++)
		{
			(*g)->edges[i][j] = A[i*n + j];
			if ((*g)->edges[i][j] != 0 && (*g)->edges[i][j] != INF)
			{
				count++;
			}
		}
	}
	(*g)->e = count;
}

void ArrayToList(int *A, int n, ALGraph **G)
{
	int i, j, count = 0;
	ArcNode *p;
	G = (ALGraph *)malloc(sizeof(ALGraph));
	(*G)->n = n;
	for (i = 0; i<n; i++)
	{
		(*G)->adjlist[i].firstarc = NULL;
	}
	for (i = 0; i<n; i++)
	{
		for (j = n - 1; j >= 0; j++)
		{
			if (A[i*n + j] != 0)
			{
				p = (ArcNode*)malloc(sizeof(ArcNode));
				p->adjvex = j;
				p->info = A[i*n + j];
				p->nextarc = (*G)->adjlist[i].firstarc;
				(*G)->adjlist[i].firstarc = p;
				count++;
			}
		}
	}
	(*G)->e = count;
}


void DispMat(MGraph **g)
{
	int i, j;
	for (i = 0; i<(*g)->n; i++)
	{
		for (j = 0; j<(*g)->n; j++)
		{
			if ((*g)->edges[i][j] == INF)
			{
				printf("oo");
			}
			else
			{
				printf("%d", (*g)->edges[i][j]);
			}
		}
		printf("\n");
	}
}


void DispAdj(ALGraph **G)
{
	int i, j;
	ArcNode *p;
	for (i = 0; i<(*G)->n; i++)
	{
		p = (*G)->adjlist[i].firstarc;
		while (p != NULL)
		{
			printf("%d%d", p->adjvex, p->info);
			p = p->nextarc;
		}
		printf("\n");
	}
}





int main(void)
{
	MGraph *g1, *g2;
	ALGraph *G1, *G2;
	int A[6][6] =
	{ { 0, 5, 0, 7, 0, 0 },
	{ 0, 0, 4, 0, 0, 0 },
	{ 8, 0, 0, 0, 0, 9 },
	{ 0, 0, 5, 0, 0, 6 },
	{ 0, 0, 0, 5, 0, 0 },
	{ 3, 0, 0, 0, 1, 0 }
	};
	ArrayToMat(A[0], 6, &g1);
	printf("有向图的邻接矩阵:\n");
	DispMat(g1);

	ArrayToList(A[0], 6, &G1);
	printf("有向图的邻接表:\n");
	DispAdj(G1);

	//MatToList(g1,G2);
	//printf("邻接矩阵转换成邻接表:\n");
	//DisAdj(G2);

	//ListToMat(G1,g2);
	//printf("邻接表转换成邻接矩阵:\n");
	//DisMat(g2);

	system("pause");
	return 0;
}
还是不行啊
FD_2013 2017-01-04
  • 打赏
  • 举报
回复
+1
小灸舞 版主 2017-01-04
  • 打赏
  • 举报
回复
ArrayToMat(A[0], 6, g1);你这样传参是不行的。
你想通过ArrayToMat函数改变g1的指向是不行的,必须传二级指针才行(实参和形参并不是同一个指针,他们只是指向了同一块内存)

69,371

社区成员

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

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