图邻接表存储 深度优先和广度优先遍历

邻接表    

是图的常用储存结构之一。邻接表由表头结点和表结点两部分组成,其中图中每个顶点均对应一个存储在数组中的表头结点。如图:
这里写图片描述
下面直接上代码:

#include<stdio.h>
#include<stdlib.h>
#include"my_queue.h"

int visit[MAXSIZE] = {0};              //作为输出判断数组

AdjList *build_Graph();
void find_insert(AdjList *,char,char);
void findHead(AdjList *,char,int *);
void insert(ArcNode **,int);
void ShowAdjList(AdjList *);
void DFS(AdjList *,int);
void DFS_traverse(AdjList *);
void CleanVisit();
void BFS(AdjList *,int);

void BFS(AdjList *G,int v1)
{ 
    ArcNode *p;
    Queue Qhead = init();
    printf("%c ",G->vertex[v1].vexdata);                    //先访问
    p = G->vertex[v1].head;
    visit[v1] = 1;
    push(Qhead,v1);                                         //入队列
    while(Qhead->rear != Qhead->front) {                    //只要队列不为空
        v1 = pop(Qhead);                                    //出队列一个元素
        p = G->vertex[v1].head;                             //记录节点
        while(p) {                                          //依次将次节点后面的节点入队列
            if(p && (visit[p->adjvex] == 0)) {
                printf("%c ",G->vertex[p->adjvex].vexdata);
                visit[p->adjvex] = 1;
                push(Qhead,p->adjvex);
            }
            p = p->next;
        }
    }
}
void CleanVisit() 
{
    int i = 0;
    for(i = 0;i < MAXSIZE;i++) {
        visit[i] = 0;
    }
}
void DFS_traverse(AdjList *G)                       //当图不连通的时候
{
    int i;
    for(i = 1;i <= G->vexnum;i++) {
        if(!visit[i]) {
            DFS(G,i);
        }
    }
}
void DFS(AdjList *G,int v1)                         //深度优先搜索连通子图
{
    ArcNode *p;
    printf("%c ",G->vertex[v1].vexdata);
    visit[v1] = 1;
    p = G->vertex[v1].head;
    while(p) {
        if(visit[p->adjvex] == 0) {
            DFS(G,p->adjvex);
        }
        p = p->next;
    }
}
void ShowAdjList(AdjList *G)                        //将邻接矩阵输出
{
    int i;
    ArcNode *p;
    printf("\n");
    for(i = 1;i <= G->vexnum;i++) {
        printf("%c ",G->vertex[i].vexdata);
        for(p = G->vertex[i].head;p;p = p->next) {
            printf("%d ",p->adjvex);
        }
        printf("\n");
    }

}
void insert(ArcNode **head,int index)
{
    ArcNode *p,*q = NULL,*node;
    node = (ArcNode *)malloc(sizeof(ArcNode));              //创建一个节点
    node->adjvex = index;
    node->next = NULL;
    int flag = 0;
    for(q = *head;q != NULL;q = q->next) {
        if((node->adjvex < q->adjvex) && (q == *head)) {    //当只有一个节点的时候,直接插到头
            node->next = q;
            *head = node;
            return ;
        }
        if((node->adjvex < q->adjvex) && (q != *head)) {
            node->next = q;
            p->next = node;
            return ;
        }
        p = q;
        flag = 1;
    }
    if(flag == 0) {                                         //一个节点都没有的时候
        *head = node;
        return ;
    }
    if(q == NULL) {                                         //目前最大的节点
        p->next = node;
    }

}
void findHead(AdjList *G,char x,int *index)
{
    int i;
    for(i = 1;i <= G->vexnum;i++) {
        if(G->vertex[i].vexdata == x) {
            *index = i;
        }
    }
}
void find_insert(AdjList *G,char x1,char x2)
{
    ArcNode *head1,*head2,*p;
    int index1,index2;
    findHead(G,x1,&index1);                              //找到x1对应的在vertex中的下标
    findHead(G,x2,&index2);
    insert(&G->vertex[index1].head,index2);              //通过比较大小插入相应的位置
    insert(&G->vertex[index2].head,index1);
}
AdjList *build_Graph()
{
    int i,j,k;
    char x1,x2;
    AdjList *G;
    G = (AdjList *)malloc(sizeof(AdjList));
    printf("请输入节点个数和弧数:");
    scanf("%d",&G->vexnum);
    scanf("%d",&G->arcnum);
    printf("请依次输入各节点:");
    for(i = 1;i <= G->vexnum;i++) {
        scanf(" %c",&G->vertex[i].vexdata);
    }
    printf("请输入各节点间关系:");
    for(i = 0;i < G->arcnum; i++) {
        scanf(" %c%c",&x1,&x2);
        find_insert(G,x1,x2);
    }
    return G;
}

int main(int argc,char *argv[])
{
    AdjList *G;
    G = build_Graph();                   //建立邻接表
    ShowAdjList(G);                      //显示邻接表
    DFS(G,1);                            //深度优先遍历连通子图
    printf("\n");
    CleanVisit();                        //清空visit数组
    DFS_traverse(G);                     //深度优先遍历图
    printf("\n");
    CleanVisit();                        //清空visit数组
    BFS(G,1);                            //广度优先遍历
    printf("\n");
    return 0;
}

下面是my_queue.h 的代码

#ifndef _MY_QUEUE_H
#define _MY_QUEUE_H
#define MAXSIZE 30

typedef struct ArcNode
{
    int adjvex;               //节点的下标位置
    int weight;               //节点的权值
    struct ArcNode *next;
}ArcNode;
typedef struct VertexNode
{
    char vexdata;             //头数据
    ArcNode * head;           //指针
}VertexNode;
typedef struct
{
    VertexNode vertex[MAXSIZE];//20个链表 
    int vexnum;                //节点数
    int arcnum;                //弧数,也就是关系数
}AdjList;

typedef struct queue
{
    int data[MAXSIZE];
    int front;
    int rear;
}queue,*Queue;

Queue init()
{
    Queue Qhead;
    Qhead = (Queue)malloc(sizeof(queue));
    Qhead->front = -1;
    Qhead->rear = -1;
}
void push(Queue Qhead,int v1) 
{
    Qhead->front++;
    Qhead->data[Qhead->front] = v1;
}
int pop(Queue Qhead)
{
    Qhead->rear++;
    return Qhead->data[Qhead->rear];
}



#endif
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨博东的博客

请我喝瓶可乐鼓励下~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值