实验6 图的遍历

来源:互联网 发布:apt-get insall yum 编辑:程序博客网 时间:2024/06/17 05:51
#include<stdio.h>#include<stdlib.h>#define MaxVex 255#define TRUE   1#define FALSE  0typedef char VertexType;  //顶点类型用的是char型typedef int Bool;Bool visited[MaxVex];  //用数组表示是否访问typedef struct EdgeNode {    int adjvex;    //邻接点在顶点数组中的下标    struct EdgeNode *next;   //指向下一个邻接点}EdgeNode;typedef struct VertexNode { //头节点    VertexType data;  //顶点的信息    EdgeNode *firstedge;}VertexNode,AdjList[MaxVex]; //顶点数组typedef struct Graph{    AdjList adjList;    int numVertexes,numEdges;  //图的结点数以及边数}Graph,*GraphAdjList;//有关队列的用到的函数typedef struct LoopQueue{    int data[MaxVex];    int front,rear;}LoopQueue,*Queue;//队列初始化void initQueue(Queue &Q){    Q->front=Q->rear=0;}//判断队列是否为空Bool QueueEmpty(Queue &Q){    if(Q->front == Q->rear){        return TRUE;    }else{        return FALSE;    }}//判断队列是否满Bool QueueFull(Queue &Q){    if((Q->rear+1)%MaxVex == Q->front){        return TRUE;    }else{        return FALSE;    }}//在队列的队尾插入元素void EnQueue(Queue &Q,int e){    if(!QueueFull(Q)){        Q->data[Q->rear] = e;        Q->rear = (Q->rear+1)%MaxVex;    }}//在队列的队头删除元素void DeQueue(Queue &Q,int *e){    if(!QueueEmpty(Q)){        *e = Q->data[Q->front];        Q->front = (Q->front+1)%MaxVex;    }} //建立图的邻接表结构(无向图)void CreateALGraph(GraphAdjList &G){    int i, j, k;    if(G==NULL){        G = (GraphAdjList)malloc(sizeof(Graph));    }    printf("输入图的结点数以及边数: ");    scanf("%d%d",&G->numVertexes,&G->numEdges);    fflush(stdin);   // 输入图的结点数以及边数    printf("输入各个顶点的数据:\n");    for (i=0; i<G->numVertexes; ++i){        printf("顶点%d: ",i+1);        scanf("%c", &(G->adjList[i].data));        G->adjList[i].firstedge = NULL;        fflush(stdin);    }//char型的顶点数据,以161页的(a)的图为例子    for (k=0; k<G->numEdges; ++k){        printf("输入(vi,vj)上的顶点序号: ");        scanf("%d%d",&i,&j);    //按照(vi,vj)的形式输入顶点的序号        EdgeNode *ptrEdgeNode = (EdgeNode*)malloc(sizeof(EdgeNode));        ptrEdgeNode->adjvex = j;        ptrEdgeNode->next = G->adjList[i].firstedge;        G->adjList[i].firstedge = ptrEdgeNode;        ptrEdgeNode = (EdgeNode*)malloc(sizeof(EdgeNode));        ptrEdgeNode->adjvex = i;        ptrEdgeNode->next = G->adjList[j].firstedge;        G->adjList[j].firstedge = ptrEdgeNode;    }//建立图的邻接表} //递归深度遍历的DSF函数void DFS(GraphAdjList &G, int i){    visited[i] = TRUE;    printf("%c ", G->adjList[i].data);    EdgeNode *p = G->adjList[i].firstedge;    while(p){        if(!visited[p->adjvex]){            DFS(G,p->adjvex);        }        p= p->next;    }}//深度优先遍历void DFSTraverse(GraphAdjList &G){    int i;    for (i=0; i<G->numVertexes; ++i){        visited[i] = FALSE;    }    for (i=0; i<G->numVertexes; ++i){        if(!visited[i]){            DFS(G,i);        }    }}//广度优先遍历void BFSTraverse(GraphAdjList &G){    int i;    Queue Q = (Queue)malloc(sizeof(LoopQueue));    for (i=0; i<G->numVertexes; ++i){        visited[i] = FALSE; //没有被访问的设置为FALSE    }    initQueue(Q);    for (i=0; i<G->numVertexes; ++i){        if(!visited[i]){            visited[i] = TRUE;            printf("%c ", G->adjList[i].data);            EnQueue(Q, i);    //如果该节点之前没被访问过,设置为TRUE,并且入队            while (!QueueEmpty(Q)){                DeQueue(Q, &i);                EdgeNode *p = G->adjList[i].firstedge;                while (p){                    if (!visited[p->adjvex]){                        visited[p->adjvex] = TRUE;                        printf("%c ", G->adjList[p->adjvex].data);                        EnQueue(Q, p->adjvex);                    }                    p = p->next;                }            }        }    }}//主函数int main(){    GraphAdjList G = NULL;    CreateALGraph(G);    printf("\n图的深度优先遍历: ");    DFSTraverse(G);    printf("\n图的广度优先遍历: ");    BFSTraverse(G);    printf("\n");    return 0;}

原创粉丝点击