转换成链表
特点:
可以直接查找一个节点的所有出度,但是想要知道一个几点的入度必须遍历整个图的存储结构.
#include <iostream>
using namespace std;
typedef char VextexType;
typedef int EdgeType;
#define VexNum 5
struct EdgeNode;
struct EdgeNode {
VextexType name;
EdgeType weight;
EdgeNode *next;
};
typedef struct
{
VextexType name;
EdgeNode *next;
}VexNode;
VexNode adjList[VexNum];
void creatGraph()
{
VextexType vextemp;
EdgeType edgetemp;
//input n vextex
for ( int i=0; i<VexNum ; ++i ){
cin>>vextemp;
adjList[i].name=vextemp;
adjList[i].next =NULL;
}
for ( int i=0; i<VexNum*VexNum; ++i ){
cin>>edgetemp;
if ( edgetemp==0 ){
continue;
}
EdgeNode *pEdge = new EdgeNode;
pEdge->name = adjList[i%VexNum].name;
pEdge->weight = edgetemp;
pEdge->next = adjList[i/VexNum].next;
if ( pEdge->next ){
while ( pEdge->next->next ){
pEdge->next =pEdge->next->next;
}
pEdge->next->next = pEdge;
pEdge->next=NULL;
} else {
adjList[i/VexNum].next = pEdge;
pEdge->next = NULL;
}
}
}
void destroyGrape()
{
for ( int i=0; i<VexNum ;++i ){
EdgeNode *p = adjList[i].next;
EdgeNode *q;
while ( p ){
q = p;
p = p->next;
delete q;
}
}
}
void printGrape()
{
for ( int i=0; i<VexNum; ++i ){
cout<<adjList[i].name<<"-->";
EdgeNode *p = adjList[i].next;
while ( p ){
cout<<"("<<p->name<<","<<p->weight<<")";
p = p->next;
}
cout<<endl;
}
}
int main()
{
creatGraph();
printGrape();
destroyGrape();
}