图的存储结构——邻接链表

来源:互联网 发布:ubuntu双系统重装win7 编辑:程序博客网 时间:2024/06/06 06:35
#include<iostream>#include<cstdio>#include<cstdlib>#include<stack>using namespace std;#define MAX_VERTEX_NUM 20const int NumEdges = 50;         //边条数const int NumVertices = 10;    //顶点个数typedef char VertexData;        //顶点数据类型typedef int EdgeData;              //边上权值类型typedef struct node {          //边结点    int dest;                            //目标顶点下标    EdgeData cost;           //边上的权值    struct node * link;           //下一边链接指针} EdgeNode;typedef struct {                   //顶点结点    VertexData data;              //顶点数据域    EdgeNode * firstAdj;       //边链表头指针} VertexNode;typedef struct {                   //图的邻接表    VertexNode VexList [NumVertices]; //邻接表    int n, e;            //图中当前的顶点个数与边数} AdjGraph;void CreateGraph (AdjGraph G) {    int tail,head,weight;    cout<<"请输入顶点个数和边数"<<endl;    scanf ("%d %d", &G.n, &G.e);    //输入顶点个数和边数    cout<<"请输入顶点信息"<<endl;    for ( int i = 0; i < G.n; i++) {        scanf("%c",&G.VexList[i].data ); //输入顶点信息        G.VexList[i].firstAdj = NULL;    }    cout<<"请输入边"<<endl;    for ( int i = 0; i < G.e; i++) {         //逐条边输入        scanf ("%d %d %d",&tail,&head,&weight );        EdgeNode * p ;        p->dest = head;        p->cost = weight;        //链入第 tail 号链表的前端        p->link = G.VexList[tail].firstAdj;        G.VexList[tail].firstAdj = p;        p = new EdgeNode;        p->dest = tail;        p->cost = weight;        //链入第 head 号链表的前端        p->link = G.VexList[head].firstAdj;        G.VexList[head].firstAdj = p;    }}int main() {    AdjGraph G;    CreateGraph ( G );    return 0;}