无向图的深度优先搜索遍历的实现

adj_list_graph.c

#include "adj_list_graph.h"

bool *visited;

PAdjGraph GraphInit()
{
    
    
    char src[GRAPH_NODE_MAX_SIZE] = {
    
    0};
    PAdjGraph graph = (PAdjGraph)malloc(sizeof(AdjGraph));
    if(graph == NULL) {
    
    
        perror("graph malloc error.");
        return (PAdjGraph)GRAPH_ERR;
    }
    printf("Please input the number of vertex nodes.\n");
    scanf("%d", &graph->nodeSize);
    printf("Please input the number of edges.\n");
    scanf("%d", &graph->edgeSize);
    graph->node = (PVertexNode)malloc(sizeof(VertexNode) * graph->nodeSize);
    if(graph->node == NULL) {
    
    
        perror("vertex node malloc error.");
        return (PAdjGraph)GRAPH_ERR;
    }
    printf("Please input the src nodes.\n");
    scanf("%s", src);
    /* get the node info of graph */
    for(int i=0; i<graph->nodeSize; i++) {
    
    
        printf("Please input the info of Vertex node[%d].\n", i+1);
        graph->node[i].data = src[i];
        graph->node[i].head = NULL;
    }

    int iValue, jValue, weight;
    for(int i=0; i<graph->edgeSize; i++) {
    
    
        printf("please input the edge[%d] by (i,j)\n", i+1);
        scanf("%d,%d", &iValue, &jValue);
        printf("Please input the weight of edge[%d].\n", i+1);
        scanf("%d", &weight);
        if(iValue<0 || iValue>=graph->nodeSize || jValue<0 || jValue>=graph->nodeSize || weight <= 0) {
    
    
            perror("node info error.");
            return (PAdjGraph)GRAPH_ERR;
        }
        
        /* create the assist tags */
        visited = (bool *)malloc(sizeof(bool) * graph->nodeSize);
        if(visited == NULL) {
    
    
            perror("visited [] malloc error.");
            return (PAdjGraph)GRAPH_ERR;
        }
        for(int i=0; i<graph->nodeSize; i++) {
    
    
            visited[i] = false;
        }
        
        PEdgeNode addNodeI = (PEdgeNode)malloc(sizeof(EdgeNode));
        if(addNodeI == NULL) {
    
    
            perror("addNodeI malloc error.");
            return(PAdjGraph)GRAPH_ERR;
        }
        addNodeI->data = jValue;
        addNodeI->weight = weight;
        addNodeI->next = graph->node[iValue].head;
        graph->node[iValue].head = addNodeI;

        PEdgeNode addNodeJ = (PEdgeNode)malloc(sizeof(EdgeNode));
        if(addNodeJ == NULL) {
    
    
            perror("addNodeJ malloc error.");
            return (PAdjGraph)GRAPH_ERR;
        }
        addNodeJ->data = iValue;
        addNodeJ->weight = weight;
        addNodeJ->next = graph->node[jValue].head;
        graph->node[jValue].head = addNodeJ;
    }
    printf("graph init completed.\n");
    return graph;
}

void GraphDestroy(PAdjGraph graph)
{
    
    
    if(graph == NULL) {
    
    
        perror("graph is null.");
        return;
    }
    for(int i=0; i<graph->nodeSize; i++) {
    
    
        while(graph->node[i].head != NULL) {
    
    
            PEdgeNode temp = graph->node[i].head;
            graph->node[i].head = temp->next;
            free(temp);
        }
    }
    free(graph->node);
    graph->node = NULL;
    free(graph);
    graph = NULL;
    free(visited);
    visited = NULL;
    printf("graph destroy completed.\n");
    return;
}

void GraphDfs(const PAdjGraph graph, int index)
{
    
    
    if(graph == NULL) {
    
    
        perror("graph is null.");
        return;
    }
    
    PEdgeNode temp = graph->node[index].head;
    printf("%c ", graph->node[index].data);
    visited[index] = true;
    while(temp) {
    
    
        if(!visited[temp->data]) {
    
    
            GraphDfs(graph, temp->data);
        }
        temp = temp->next;
    }
}

main.c

#include "adj_list_graph.c"

int main(void)
{
    
    
    PAdjGraph myGraph = GraphInit();
    GraphDfs(myGraph, 0);
    printf("\n");
    GraphDestroy(myGraph);
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_37546257/article/details/121055074