zoukankan      html  css  js  c++  java
  • 广度优先遍历

    额。。一到五个结点就报错。。。

    重要的是我调试的时候,他竟然就正常了。。。。

    typedef struct
    {
        int **table;
        int num;
    }Graph; 
    
    typedef struct Queue
    {
        int data;
        struct Queue *next;
    }Queue;
    
    
    void createTable(Graph *graph);
    void des(Graph *graph); 
    void travel(Graph *graph, int first);
    void init(Queue *q);
    void enQueue(Queue *q, int num);
    void deQueue(Queue *q, int *num); 
    int emptyQueue(Queue *q); 
     
    int main(int argc, char *argv[])
    {
        Graph graph;
        
        printf("请输入结点数:");
        scanf("%d", &(graph.num)); 
        createTable(&graph);
        
        travel(&graph, 2); 
        
        
        des(&graph);
        return 0;
    }
    
    void travel(Graph *graph, int first)
    {
        first -= 1;
        int num = graph->num;
        int **table = graph->table;
        Queue q;
        int i, cur;
        int visit[num];
        
        for(i=0;i<num;i++)
        {
            visit[i] = 0;
         } 
        
        init(&q);
        enQueue(&q, first);
        
        while(!emptyQueue(&q))
        {
            deQueue(&q, &cur);
            printf("%d ", cur+1);
            visit[cur] = 1;
            
            for(i=0; i<num; i++)
            {
                if(table[cur][i] && (!visit[i]))
                {
                    visit[i] = 1;
                    enQueue(&q, i);
                }
            }
        }
        
    }
    
    void init(Queue *q)
    {
        q->data = 0;
        q->next = NULL;
    }
    
    void enQueue(Queue *q, int num)
    {
        Queue *p = q;
        while(p->next)
        {
            p = p->next;
        }
        p->next = (Queue *)malloc(sizeof(Queue));
        p = p->next;
        p->next = NULL;
        p->data = num;    
    }
    
    void deQueue(Queue *q, int *num)
    {
        Queue *p = q;
        Queue *temp;
        
        if(emptyQueue(q))
        {
            return;
        }
        
        temp = p->next->next;
        *num = p->next->data;
        
        free(p->next);
        p->next = temp;
    }
    
    int emptyQueue(Queue *q)
    {
        if(q->next)
        {
            return 0;
        }else {
            return 1;
        }
    }
    
    void createTable(Graph *graph) 
    {
        int num = graph->num; 
        int **table;
        int i, j, temp;
        
        table = (int **)malloc(sizeof(int)*num);
        for(i=0; i<num; i++)
        {
            table[i] = (int *)malloc(sizeof(int)*num);
        }
        
        for(i=0; i<num; i++)
        {
            printf("第 %d 行:", i+1);
            for(j=0;j<num;j++)
            {
                scanf("%d", &temp);
                if(temp == ' ')
                {
                    j -= 1;
                }else {
                    (table)[i][j] = temp;
                }
            }
            getchar();
        }
    
        graph->table = table; 
        
    }
    
    void des(Graph *graph)
    {
        int num = graph->num;
        int i, j;
        
        printf("
    ");
        for(i=0; i<num; i++)
        {
            for(j=0;j<num;j++)
            {
                printf("%d ", (graph->table)[i][j]);
            }
            printf("
    ");
            //free((graph->table)[i]); 
        }
        //free(graph->table);
        graph->num = 0;
    }
  • 相关阅读:
    GridView多表头固定+分组+总计
    ajaxpro.2.dll使用【转帖】
    表达式计算易错题
    uclibc下使用libcurl的段错误(缺少hosts文件)
    《java.util.concurrent 包源码阅读》01 源码包的结构
    Linux学习笔记【2】Install Software under RedHat enterprise 5.4
    Windows语言包的那些事
    Let outlook work background when it is minimal
    DB2 Error Message
    db2 系统表信息
  • 原文地址:https://www.cnblogs.com/buerr/p/7470233.html
Copyright © 2011-2022 走看看