图的深度遍历

#include
#include
#include

#define MaxSize 20
typedef struct Node{
    char vex;
    int location;
    struct Node * next; 
}MapNode; 
int node_num;
int visited[MaxSize];

void createMap(MapNode **p ){
    char ch;
    int i=0;
    MapNode * q,* new_node;
    (*p) = (MapNode *)malloc(sizeof(MapNode)*MaxSize);
    printf("请输入图的节点:\n");
    scanf("%c",&ch);
    while(ch!='\n'){
        (*p)[i].vex = ch;
        (*p)[i].next = NULL;
        (*p)[i].location = i;
        i++;
        scanf("%c",&ch);
    }
    node_num = i;

    for(i=0;ivex);
        scanf("%c",&ch);
        while(ch!='\n'){
             new_node = (MapNode *)malloc(sizeof(MapNode));
            new_node->vex = ch;
            new_node->next = NULL;
            q->next = new_node;
            q = new_node;
            scanf("%c",&ch);
        }
    }
}

int get_index(MapNode *p,char v){
    int i;
    for(i=0;ivex);
    visited[i] = 1;
    q= q->next;
    while(q){
        int index = get_index(p,q->vex);
        if(visited[index]==0){
            depth(p,index);
        }
        q = q->next;
    }
}


void print_depth(MapNode *p){
    int i;
    for(i=0;i",q->vex);
            q = q->next;
        }
        printf("\n");
    }
}

int main(){
    MapNode *p;
    createMap(&p);
    print(p);
    print_depth(p);
    return 0;
}

结果如下图


图的深度遍历_第1张图片
image.png

你可能感兴趣的:(图的深度遍历)