图的邻接表建立以及DFS和BFS

来源:互联网 发布:coc淘宝买宝石安全吗 编辑:程序博客网 时间:2024/06/07 00:14
  1. 图的存储方式用的邻接表,邻接表链表用的头插法插入数据,图的定点数组用的自定义数据类型数组即struct []
  2. BFS用一个队列实现,学习使用就没有用标准库的队列,用的自己写的一个队列,包含在用户自定义头文件myQueue.h 当中。
  3. DFS使用递归方法遍历图

下面是图的建立和遍历代码,最后面附上自己写的头文件myQueue.h

#include <iostream>#include <stdio.h>#include <stdlib.h>#include "myQueue.h"using namespace std;#define TRUE 1#define FALSE 0#define MAXVERTEX 100int visit[MAXVERTEX] = {FALSE};typedef char VertexType;typedef int EdgeType;struct EdgeNode{    int VertexIndex;    EdgeType data;    EdgeNode *next;};typedef struct VertexNode{    VertexType data;    EdgeNode *firstedge;} VertexList[MAXVERTEX];struct GraphAdjList{    VertexNode VertexNodeList[MAXVERTEX];    int numVertex;    int numEdge;};void initAdjListGraph(GraphAdjList *G){    G->numEdge = 0;    G->numVertex = 0;}void CreateAdjListGraph(GraphAdjList *G){    cout << "输入顶点个数和边的个数(空格分隔,如3 4 后回车键结束):" << endl;    cin >> G->numVertex >> G->numEdge;    cout << "输入定点的命名(空格分隔,如a b c d e 后回车键结束):" << endl;    for (int i = 0; i < G->numVertex; i++)    {        cin >> G->VertexNodeList[i].data;        G->VertexNodeList[i].firstedge = NULL;    }    cout << "输入边的关系(如 0 1 回车键 1 2 回车键。。。。直至所有的边输入完毕):" << endl;    for (int k = 0; k < G->numEdge; k++)    {        int i, j;        cin >> i >> j;        EdgeNode *NewEdgeNode = (EdgeNode *)malloc(sizeof(EdgeNode));        NewEdgeNode->VertexIndex = j;        NewEdgeNode->next = G->VertexNodeList[i].firstedge;        (G->VertexNodeList[i]).firstedge = NewEdgeNode;    }}void DFS(GraphAdjList *G, int i){    EdgeNode *p;    visit[i] = TRUE;    p = G->VertexNodeList[i].firstedge;    cout << G->VertexNodeList[i].data << " ";    while (p != NULL)    {        if (!visit[p->VertexIndex])        {            DFS(G, p->VertexIndex);        }        p = p->next;    }}void BFS(GraphAdjList *G){    Queue q; //这里没有用标准库的queue,头文件中自己写的队列    initQueue(&q);    EdgeNode *p;    for (int i = 0; i < G->numVertex; i++)    {        visit[i] = FALSE; //初始化全局数组,防止DFS搜索改变数组造成的影响    }    for (int i = 0; i < G->numVertex; i++)    {        if (!visit[i])        {            visit[i] = TRUE;            cout << G->VertexNodeList[i].data << " ";            Enqueue(&q, i);            while (!isEmpty(&q)) //直至所有定点遍历完毕退出循环            {                i = Dequeue(&q);                p = G->VertexNodeList[i].firstedge;                while (p != NULL) //直至邻接表后的链表所有定点进入队列退出循环                {                    if (!visit[p->VertexIndex])                    {                        visit[p->VertexIndex] = TRUE;                        cout << G->VertexNodeList[p->VertexIndex].data << " ";                        Enqueue(&q, p->VertexIndex);                    }                    p = p->next;                }            }        }    }}int main(){    GraphAdjList G;    initAdjListGraph(&G);    CreateAdjListGraph(&G);    cout << "深度优先搜索结果:" << endl;    DFS(&G, 0);    cout << endl;    cout << "广度优先搜索结果:" << endl;    BFS(&G);    return 0;}

下面是头文件myQueue.h ,包含在主程序中即可,下面为头文件代码:

#ifndef _HEADERNAME_H //防止头文件重复包含#define _HEADERNAME_H#include <stdio.h>#include <iostream>#include <stdlib.h>#endifusing namespace std;#define FALSE 0 //C语言没有bool类型,这里是用户自定义类型#define TRUE 1typedef int elemtype;struct Node{    elemtype data;    Node *next;};struct Queue{    Node *front;    Node *rear;};void initQueue(Queue *q){    q->front = q->rear = (Node *)malloc(sizeof(Node));    if (!q->front)    {        return;    }    q->front->next = NULL;}int isEmpty(Queue *q){    if (q->front == q->rear)    {        return TRUE;    }    else    {        return FALSE;    }}void Enqueue(Queue *q, elemtype data){    Node *repareInsert = (Node *)malloc(sizeof(Node));    if (repareInsert == NULL)    {        exit(0);    }    q->rear->data = data;    q->rear->next = repareInsert;    q->rear = repareInsert;}elemtype Dequeue(Queue *q){    if (isEmpty(q))    {        exit(0);    }    Node *FrontTmp = q->front;    elemtype data = q->front->data;    q->front = q->front->next;    free(FrontTmp);    return data;}void MakeEmpty(Queue *q){    while (!isEmpty(q))    {        Dequeue(q);    }}void DisposeQueue(Queue *q){    Node *FrontTmp = q->front;    while (FrontTmp != q->rear)    {        cout << FrontTmp->data << endl;        FrontTmp = FrontTmp->next;    }}
0 0
原创粉丝点击