俺也不知道叫什么好
2021-05-22 11:05
采纳率: 100%
浏览 9

图的邻接表 BFS算法遍历图 出现指针异常问题

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100

typedef struct ArcNode{
    int adjvex;
    struct ArcNode *next;
    int *info;
}ArcNode;

typedef struct VNode{
    int data;
    ArcNode *next;
}VNode,AdjList;

typedef struct {
    AdjList vertices[MAXSIZE];
    int vexnum,arcnum;
    int kind;
}ALGraph;

typedef struct QNode{
    int data;
    struct QNode *next;
}*Queue;

typedef struct{
    Queue front;
    Queue rear;
}LinkQueue;

bool visitedD[MAXSIZE];
bool visitedB[MAXSIZE];

void CreateLink(ALGraph &ALG,int kind){
    int i,j,v1,v2;
    ArcNode *AN;
    printf("请输入图的顶点数:");
    scanf("%d",&ALG.vexnum);
    printf("请输入图的边数:");
    scanf("%d",&ALG.arcnum);
    printf("请输入顶点信息:");
    for(i=1;i<=ALG.vexnum;i++){
        scanf("%d",&ALG.vertices[i].data);
        ALG.vertices[i].next=NULL;
    }
    for(j=1;j<=ALG.arcnum;j++){
        printf("输入第%d边的两个顶点",j);
        scanf("%d,%d",&v1,&v2);
            AN=(ArcNode *)malloc(sizeof(ArcNode));
            AN->adjvex=v2;
            AN->next=ALG.vertices[v1].next;
            ALG.vertices[v1].next=AN;
        if(kind==0){
            AN=(ArcNode *)malloc(sizeof(ArcNode));
            AN->adjvex=v1;
            AN->next=ALG.vertices[v2].next;
            ALG.vertices[v2].next=AN;
        }
    }
}

void printGraph(ALGraph &G){
    ArcNode *p;
    int i;
    printf("一共有%d个结点,%d条边\n",G.vexnum,G.arcnum);
    for(i=1;i<=G.vexnum;i++){
        p=G.vertices[i].next;
        printf("V%d -> ",G.vertices[i].data);
        while(p){
            printf("%d",p->adjvex);
            if(p->next)
                printf(" -> ");
            p=p->next;
        }
        printf("\n");
    }
}

void DFS(ALGraph G,int k);

void DFSTraverse(ALGraph G){
    for(int i=1;i<=G.vexnum;i++){
        visitedD[i]=false;
    }
    for(int j=1;j<=G.vexnum;j++){
        if(!visitedD[j])
            DFS(G,j);
    }
}

void DFS(ALGraph G,int k){
        visitedD[k]=true;
        printf("%d ",G.vertices[k].data);
}

void initQueue(LinkQueue &q){
    q.front=q.rear=(Queue)malloc(sizeof(Queue));
    if(!q.front){
        exit(0);
    }
    q.front->next=NULL;
}

void EnQueue(LinkQueue &q,int i){
    Queue p;
    p=(Queue)malloc(sizeof(Queue));
    p->data=i;
    p->next=NULL;
    q.rear->next=p;
    q.rear=p;
}

void DeQueue(LinkQueue &q,int &e){
    if(q.front==q.rear)
        exit(0);
    Queue p;
    p=q.front;
    e=p->data;
    q.front->next=p->next;
    if(p==q.rear)
        q.rear=q.front;
}

bool EmptyQueue(LinkQueue q){
    if(q.front==q.rear)
        return false;
    return true;
}

void visit(ALGraph G,int v){
    printf("%d",G.vertices[v].data);
}

void BFS(ALGraph G){
    LinkQueue q;
    initQueue(q);
    int u;
    for(int i=1;i<=G.vexnum;i++)
        visitedB[i]=false;
    for(int v=1;v<G.vexnum;v++ && !visitedB[v]){
        printf("%d ",G.vertices[v].data);
        visitedB[v]=true;
        EnQueue(q, v);
        while (EmptyQueue(q)){
            DeQueue(q,u);
            ArcNode *p;
            p=G.vertices[u].next;
            while(p){
                if(!visitedB[u]){
                    printf("%d ",G.vertices[u].next->adjvex);
                    visitedB[u]=true;
                    EnQueue(q, p->adjvex);
                }
                p=p->next;
            }
        }
    }
}

int main(){
    ALGraph ALG;
    CreateLink(ALG,1);
    printGraph(ALG);
    printf("深度优先遍历");
    DFSTraverse(ALG);
    printf("\n");
    printf("广度优先遍历");
    BFS(ALG);
}

p=p->next;为什么出现错误了呀。求大佬解答一下叭

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

相关推荐 更多相似问题