69,371
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}
#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;
}
还是不行啊