图的邻接表表示法
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#define MaxVertexNum 1010
#define INF 65535
using namespace std;
typedef struct ENode
{
int V1, V2;
int Weight;
}*Edge;
struct AdjNode
{
int Adj;
int Weight;
AdjNode *Next;
};
typedef struct VNode
{
AdjNode *Next;
int Data;
}*AdjList[MaxVertexNum];
typedef struct GNode
{
int Vertex_num;
int EdgeNum;
AdjList G;
}*Graph;
Graph InitGraph(int VertexNum)
{
Graph G;
G = (Graph)malloc(sizeof(GNode));
G->Vertex_num = VertexNum;
G->EdgeNum = 0;
for(int i = 0; i < G->Vertex_num; i++)
G->G[i]->Next = NULL;
return G;
}
void InsertEdge(Graph G, Edge E)
{
AdjNode *NewNode;
NewNode = (AdjNode*)malloc(sizeof(AdjNode));
NewNode->Adj = E->V2;
NewNode->Weight = E->Weight;
NewNode->Next = G->G[E->V1]->Next;
G->G[E->V1]->Next = NewNode;
NewNode = (AdjNode*)malloc(sizeof(AdjNode));
NewNode->Adj = E->V1;
NewNode->Weight = E->Weight;
NewNode->Next = G->G[E->V2]->Next;
G->G[E->V2]->Next= NewNode;
}
Graph CreatGraph()
{
Graph G;
Edge E;
int Vertex;
cin >> Vertex;
G = InitGraph(Vertex);
cin >> G->EdgeNum;
if(G->EdgeNum)
{
E = (Edge)malloc(sizeof(ENode));
for(int i = 0; i < G->EdgeNum; i++)
{
cin >> E->V1 >> E->V2 >> E->Weight;
InsertEdge(G, E);
}
}
for(int i = 0; i < G->Vertex_num; i++) cin >> G->G[i]->Data;
}
int main()
{
return 0;
}