图的建立与查找

本人做此文档用于复习,如有什么不明白的地方,可以留言。

本文档是一步一步教你怎么写算法的,首先是建立一个节点,然后建立了一条线,之后用一个函数把线和节点联系起来,最后在把线和节点组成图。

#include <stdio.h>  //导入io包 
#include <stdlib.h> 
#include <assert.h> //设置宏,用于断点检查错误。 

//定义数据结构

//定义线数据结构 
typedef struct _LINE  
    {  
        int end;  
        int weight;  
        struct _LINE* next;  
    }LINE;  
//定义节点数据结构 
typedef struct _VECTEX  
    {  
        int start;  
        int number;  
        LINE* neighbor;  
        struct _VECTEX* next;  
    }VECTEX;  
//定义图 
typedef struct _GRAPH  
    {  
        int count;  
        VECTEX* head;  
    }GRAPH;  

//创建一个节点 
VECTEX* create_new_vectex(int start)  
    {  
        VECTEX* pVextex = (VECTEX*)malloc(sizeof(VECTEX));  
        assert(NULL != pVextex);  

        pVextex->start = start;  
        pVextex->number = 0;  
        pVextex->neighbor = NULL;  
        pVextex->next = NULL;  
        return pVextex;  
    }  

//创建一条线    
LINE* create_new_line(int end, int weight)  
    {  
        LINE* pLine = (LINE*)malloc(sizeof(LINE));  
        assert(NULL != pLine);  

        pLine->end = end;  
        pLine->weight = weight;  
        pLine->next = NULL;  
        return pLine;  
    }  
//创建一条线    
VECTEX* create_new_vectex_for_graph(int start, int end, int weight)  
    {  
        VECTEX* pVectex = create_new_vectex(start);  
        assert(NULL != pVectex);  

        pVectex->neighbor = create_new_line(end, weight);  
        assert(NULL != pVectex->neighbor);  

        return pVectex;  
    }  
//创建一个图     
GRAPH* create_new_graph(int start, int end, int weight)  
    {  
        GRAPH* pGraph = (GRAPH*)malloc(sizeof(GRAPH));  
        assert(NULL != pGraph);  

        pGraph->count = 1;  
        pGraph->head = create_new_vectex_for_graph(start, end, weight);  
        assert(NULL != pGraph->head);  

        return pGraph;  
    }  
//查找图中的节点函数    
VECTEX* find_vectex_in_graph(VECTEX* pVectex, int start)  
    {  
        if(NULL == pVectex)  
            return NULL;  

        while(pVectex){  
            if(start == pVectex->start)  
                return pVectex;  
            pVectex = pVectex->next;  
        }  

        return NULL;  
    }  
//查找图中的线函数 
LINE* find_line_in_graph(LINE* pLine, int end)  
    {  
        if(NULL == pLine)  
            return NULL;  

        while(pLine){  
            if(end == pLine->end)  
                return pLine;  

            pLine = pLine->next;  
        }  

        return NULL;  
    }  

 //主函数的定义 
    int main(){
        printf("创建图\n");
        GRAPH* g = create_new_graph(1,2,1);

        printf("%d\n",g->head->start);

        VECTEX* v = (VECTEX *)malloc(sizeof(VECTEX));
        //这些可以不用定义,也可以进行定义。 
        //      v->neighbor = NULL;
        //      v->next = NULL;
        //      v->number = 2;
        v->start = 1;
        VECTEX* vv = find_vectex_in_graph (v,1);
        printf("查找start = 1 的节点值为 %d \n",vv->number);
        return 0;
    }

参考博客:
http://blog.csdn.net/feixiaoxing/article/details/6922766

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值