Define
typedef struct Anode
{
int no;//该节点编号
int weight;//该边的信息,如权值
struct Anode *nextarc;//指向下一个节点的指针
}ArcNode;//邻接表边类型 即指向结点的边
typedef struct Vnode
{
char info;//头结点信息 注意不是结点编号
ArcNode *firstarc;//指向第一个邻接点的指针
}VNode;//邻接表头节点类型
typedef struct
{
VNode adjlist[MAXV];//顶点数组 MAXV:最大结点数
int n, e;//n:顶点数
}AdjGraph;//图邻接表类型
Create
void CreatAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e){
G = (AdjGraph *)malloc(sizeof(AdjGraph));
G->n = n; G->e = e;
for(int i = 0; i < n; ++i)
G->adjlist[i].firstarc = NULL;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(A[i][j] != 0 && A[i][j] != INF){
ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode));
p->no = j;//notice**
p->weight = A[i][j];
ArcNode *r = NULL;
r = G->adjlist[i].firstarc;
p->nextarc = G->adjlist[i].firstarc;//头插法,顺序和数组是反的
G->adjlist[i].firstarc = p;//这里用不了尾插,因为没有头结点
}
}
Destroy
void Destroy(AdjGraph *&G)
{
ArcNode *q;
for (int i = 0; i < G->n; i++)
{
ArcNode *p = G->adjlist[i].firstarc;
while (p != NULL)
{
q = p;
p = p->nextarc;
free(q);
q = NULL;
}
}
free(G);
G = NULL;
cout << "Destroy adjacent Graph..." << endl;
}