数据机构之图的存储

#ifndef _HEAD_H_
#define _HEAD_H_


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

typedef int DataType;

struct node 
{
    DataType data;
    struct node *next;
};

typedef struct
{
    struct node *front;
    struct node *rear;
}LinkQueue;

extern LinkQueue *create_empty_linkqueue();
extern int is_empty_linkqueue(LinkQueue *q);
extern int enter_linkqueue(LinkQueue *q,DataType data);
//删头法
extern DataType delete_linkqueue(LinkQueue *q);
#endif
#include "head.h"


LinkQueue *create_empty_linkqueue()
{
    LinkQueue *q = NULL;
    struct node *head = NULL;


    head = (struct node *)malloc(sizeof(struct node));
    head->next = NULL;


    q = (LinkQueue *)malloc(sizeof(LinkQueue));
    q->front = q->rear = head;


    return q;
}


int is_empty_linkqueue(LinkQueue *q)
{
    return q->front == q->rear;
}


int enter_linkqueue(LinkQueue *q,DataType data)
{
    struct node *temp = NULL;


    temp = (struct node *)malloc(sizeof(struct node));
    temp->data = data;


    //将新结点尾插法插入链表尾
    temp->next = q->rear->next;
    q->rear->next = temp;


    //更新rear
    q->rear = temp;


    return 0;
}


//删头法
DataType delete_linkqueue(LinkQueue *q)
{
    struct node *temp = NULL;


    //保存原头结点首地址
    temp = q->front;


    //更新front
    q->front = q->front->next;


    free(temp);
    temp = NULL;


    return q->front->data;
}
#if 0
int main()
{
    LinkQueue *q = NULL;
    int i = 0;


    q = create_empty_linkqueue();


    for(i = 0;i < 10;i++)
    {
        enter_linkqueue(q,i);
    }


    while(!is_empty_linkqueue(q))
    {
        printf("%d ",delete_linkqueue(q));
    }
    putchar('\n');


    return 0;
}
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "head.h"
#include <strings.h>


#define N 9


typedef int VType;
typedef struct 
{
    VType v[N];
    int matrix[N][N];
}Graph;


Graph *create_graph()
{
    Graph *g = NULL;
    int i = 0;


    g = (Graph *)malloc(sizeof(Graph));
    //memset(g,0,sizeof(Graph));
    bzero(g,sizeof(Graph));


    for(i = 0;i < N;i++)
    {
        g->v[i] = i;
    }


    return g;
}


void input_edge(Graph *g)
{
    int i = 0,j = 0;


    printf("Input edge like (V0,V1) (V0,V2) ...\n");
    //(V0,V1) (V0,V2) (V0,V3) ...
    while(scanf("(V%d,V%d) ",&i,&j) == 2)//scanf字符串中空白符在输入时可跳过空白符
    {
        //getchar();
        g->matrix[i][j] = g->matrix[j][i] = 1;
    }


    //清输入缓冲区
    while(getchar() != '\n');


    return ;
}


void print_matrix(Graph *g)
{
    int i = 0,j = 0;


    printf("%-4c",' ');
    for(i = 0;i < N;i++)
    {
        printf("V%-3d",i);
    }
    putchar('\n');


    for(i = 0;i < N;i++)
    {
        printf("V%-3d",i);
        for(j = 0;j < N;j++)
        {
            printf("%-4d",g->matrix[i][j]);
        }
        putchar('\n');
    }


    return ;
}


//访问标志数组
int visited[N];


int first_adj(Graph *g,int v)
{
    int i = 0;


    for(i = 0;i < N;i++)
    {
        if(g->matrix[v][i] == 1)
        {
            return i;
        }
    }


    return -1;
}


int next_adj(Graph *g,int v,int u)
{
    int i = 0;


    for(i = u + 1;i < N;i++)
    {
        if(g->matrix[v][i] != 0)
        {
            return i;
        }
    }


    return -1;
}


void DFS(Graph *g,int v)
{
    int u = 0;


    visited[v] = 1;//0
    printf("V%d ",v);


    //first_adj return < 0 表示无第一邻接点
    u = first_adj(g,v);//1


    while(u >= 0)
    {
        if(visited[u] == 0)
        {
            DFS(g,u);//1
            /*visited[u] = 1;*//*{{{*/
            /*printf("V%d ",u);*/
            /*{*/
                /*visited[v] = 1;//1*/
                /*printf("V%d ",v);*/


                /*//first_adj return < 0 表示无第一邻接点*/
                /*u = first_adj(g,v);//0*/


                /*while(u >= 0)*/
                /*{*/
                    /*if(visited[u] == 0)//0 3*/
                    /*{*/
                        /*DFS(g,u);//3*/
                        /*[>visited[u] = 1;<]*/
                        /*[>printf("V%d ",u);<]*/
                        /*{*/


                            /*visited[v] = 1;//3*/
                            /*printf("V%d ",v);*/


                            /*//first_adj return < 0 表示无第一邻接点*/
                            /*u = first_adj(g,v);//1*/


                            /*while(u >= 0)*/
                            /*{*/
                                /*if(visited[u] == 0)//1 4*/
                                /*{*/
                                    /*DFS(g,u);//4*/
                                    /*[>visited[u] = 1;<]*/
                                    /*[>printf("V%d ",u);<]*/
                                /*}*/


                                /*u = next_adj(g,v,u);//4*/
                            /*}*/


                            /*return ;*/


                        /*}*/
                    /*}*/


                    /*u = next_adj(g,v,u);//3*/
                /*}*/


                /*return ;*/


            /*}*//*}}}*/
        }


        u = next_adj(g,v,u);
    }


    //循环结束表示顶点v所有邻接点访问完
    //回溯到上层


    return ;
}


void BFS(Graph *g,int v)
{
    int u = 0;  
    LinkQueue *q = NULL;


    q = create_empty_linkqueue();


    visited[v] = 1;
    enter_linkqueue(q,v);


    while(!is_empty_linkqueue(q))
    {
        v = delete_linkqueue(q);
        printf("V%d ",v);


        u = first_adj(g,v);


        while(u >= 0)
        {
            if(visited[u] == 0)
            {
                visited[u] = 1;
                enter_linkqueue(q,u);
            }


            u = next_adj(g,v,u);
        }
    }


    putchar('\n');


    return ;
}


int main()
{
    Graph *g = NULL;


    g = create_graph();


    input_edge(g);


    print_matrix(g);


//    DFS(g,0);
    BFS(g,0);
    putchar('\n');


    
    /*input_edge(g);*/


    /*puts("=============================");*/
    /*print_matrix(g);*/


    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值