邻接表 数据结构 -更新中。。。。。

来源:互联网 发布:异步网络框架 编辑:程序博客网 时间:2024/06/05 15:55

邻接表定义:点击即可

可实现有向图和无向图

#include<iostream>#include <cstdlib>using namespace std;#define MAXVEX 100typedef int VertexType;typedef int EdgeType;typedef struct EdgeNode{    int adjvex;    struct EdgeNode *next;} EdgeNode;typedef struct VextexNode{    VertexType data;//顶点域,存储顶点信息    int number;//顶点度数    int visit;//标记是否访问过此点    EdgeNode *firstedge;// 边表头指针} VextexNode, AdjList[MAXVEX];typedef struct{    AdjList adjList;    int numNodes, numEdges; // 图中当前顶点数和边数} GraphAdjList;void CreateALGraph_nodir(GraphAdjList *Gp){    int i, j, k;    EdgeNode *pe;    cout<< "这是一个无向图。"<<endl;    cout << "输入顶点数和边数:" << endl;    cin >> Gp->numNodes >> Gp->numEdges;    for (i = 1; i <= Gp->numNodes; i++)//初始化    {        Gp->adjList[i].data=i;        Gp->adjList[i].firstedge = NULL;        Gp->adjList[i].number=0;    }    for (k = 0; k <  Gp->numEdges; k++)//建表    {        cout<<"第"<<k+1<< "条边的顶点序号:" << endl;        cin >> i >> j;        Gp->adjList[i].number++;        Gp->adjList[j].number++;        pe = (EdgeNode *)malloc(sizeof(EdgeNode));        pe->adjvex = j;        pe->next = Gp->adjList[i].firstedge;        Gp->adjList[i].firstedge = pe;        pe = (EdgeNode *)malloc(sizeof(EdgeNode));        pe->adjvex = i;        pe->next = Gp->adjList[j].firstedge;        Gp->adjList[j].firstedge = pe;    }     GraphAdjList *G=Gp;     for (k = 1; k <= G->numNodes; k++){        cout<<k<<"的度数为"<<G->adjList[k].number*2<<endl;        cout<<"与"<<k<<"相连的顶点:";        int d=-1;        while(G->adjList[k].firstedge){            if(G->adjList[k].firstedge->adjvex!=d)            cout<<G->adjList[k].firstedge->adjvex<<"   ";            d=G->adjList[k].firstedge->adjvex;            G->adjList[k].firstedge=G->adjList[k].firstedge->next;        }        cout<<endl;     }}void CreateALGraph_dir(GraphAdjList *Gp){    int i, j, k;    EdgeNode *pe;    cout<<"这是一个有向图。"<<endl;    cout << "输入顶点数和边数:" << endl;    cin >> Gp->numNodes >> Gp->numEdges;   for (i = 1; i <= Gp->numNodes; i++)    {        Gp->adjList[i].data=i;        Gp->adjList[i].firstedge = NULL;//将边表置为空表        Gp->adjList[i].number =0;//度数标记为0    }    for (k = 0; k <  Gp->numEdges; k++)// 建立边表    {        cout<<"第"<<k+1<< "条边的起点和终点序号:" << endl;        cin >> i >> j;        Gp->adjList[i].number++;        Gp->adjList[j].number++;        pe = (EdgeNode *)malloc(sizeof(EdgeNode));        pe->adjvex = j;// 将pe的指针指向当前顶点上指向的结点        pe->next = Gp->adjList[i].firstedge;        Gp->adjList[i].firstedge = pe;// 将当前顶点的指针指向pe    }    GraphAdjList *G=Gp;    for (k = 1; k <= G->numNodes; k++){        cout<<k<<"的度数为"<<G->adjList[k].number<<endl;        cout<<"与"<<k<<"相连的顶点:";        int d=-1;        while(G->adjList[k].firstedge){            if(G->adjList[k].firstedge->adjvex!=d)            cout<<G->adjList[k].firstedge->adjvex<<"   ";            d=G->adjList[k].firstedge->adjvex;            G->adjList[k].firstedge=G->adjList[k].firstedge->next;        }        cout<<endl;    }}int main(void){    while(1){    cout<<"Please enter a directed graph or undirected graph."<<endl;    cout<<"1 indicates directed graph"<<endl<<"2 indicates nodirected graph"<<endl<<"3 indicates end"<<endl;;    int type;    cin>>type;    GraphAdjList GL;    if(type==1)    CreateALGraph_dir(&GL);    else if(type==2)    CreateALGraph_nodir(&GL);     else{        cout<<"the program is end!"<<endl;        break;     }    }return 0;}<u></u>


因为用引用传参的时候一直又问题,所以用的指针,许多功能没有分离开来。所以代码有点丑,更新中。。。。。

0 0
原创粉丝点击