哦.257 2022-05-27 12:18
浏览 37
已结题

邻接矩阵转化为邻接表时,调用函数报错如何解决?急!

问题遇到的现象和发生背景

C语言数据结构——图

问题

已知邻接矩阵,编写CreateAdj函数将邻接矩阵转化为邻接表,但是调用图的顶点数(n)和边数(e)时报错

相关函数:

void CreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e){//根据邻接矩阵建立图的邻接表G 
    int i, j;
    ArcNode *p;
    G=(AdjGraph *)malloc(sizeof(AdjGraph));
    G->n=n;
    G->e=e;
    for(i=0;i<n;i++)
        G->adjlist[i].firstarc=NULL;
    for(i=0;i<n;i++)
        for(j=n-1;j>=0;j--)
            if(A[i][j]!=0&&A[i][j]!=INF){
                p=(ArcNode *)malloc(sizeof(ArcNode));
                p->adjvex=j;
                p->weight=A[i][j];
                p->nextarc=G->adjlist[i].firstarc;
                G->adjlist[i].firstarc=p;
            }
}

主程序:

int main(){
    AdjGraph G;
    int A[6][6]={0,5,INF,7,INF,INF,
                 INF,0,4,INF,INF,INF,
                 8,INF,0,INF,INF,9,
                 INF,INF,5,0,INF,6,
                 INF,INF,5,INF,0,INF,
                 3,INF,INF,INF,1,0};
    CreateAdj(&G,A,6,10);               //这里报错!!!
    printf("深度优先遍历:");
    DFS(G, 0);
    printf("\n广度优先遍历:");
    BFS(G, 0);
    return 0;
}

所有程序:

#include<stdio.h>
#include<malloc.h>
//#include <iostream>
//#include <typeinfo>

#define MAXV 100
#define MAX 100
#define INF 32767
//using namespace std;
typedef int InfoType;
typedef struct ANode{//邻接表存储类型 P258
    int adjvex;
    struct ANode *nextarc;
    int weight;
}ArcNode;
typedef struct Vnode{
    InfoType info;
    int data;
    ArcNode *firstarc;
}VNode;
typedef struct{
    VNode adjlist[MAXV];
    int n, e;
}AdjGraph;
void CreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e){//根据邻接矩阵建立图的邻接表G P260 
    int i, j;
    ArcNode *p;
    G=(AdjGraph *)malloc(sizeof(AdjGraph));
    G->n=n;
    G->e=e;
    for(i=0;i<n;i++)
        G->adjlist[i].firstarc=NULL;
    for(i=0;i<n;i++)
        for(j=n-1;j>=0;j--)
            if(A[i][j]!=0&&A[i][j]!=INF){
                p=(ArcNode *)malloc(sizeof(ArcNode));
                p->adjvex=j;
                p->weight=A[i][j];
                p->nextarc=G->adjlist[i].firstarc;
                G->adjlist[i].firstarc=p;
            }
}

//深度优先遍历 P265
int visitedDFS[MAX]={0}; 
void DFS(AdjGraph *G, int v){
    ArcNode *p;
    visitedDFS[v]=1;
    printf("%d", v);
    p=G->adjlist[v].firstarc;
    while(p!=NULL){
        if(visitedDFS[p->adjvex]==0)
            DFS(G,p->adjvex);
        p=p->nextarc;
    }
}
//广度优先遍历 P266
typedef struct{// P98
    int data[MAX];
    int front, rear;
}SqQueue;
void InitQueue(SqQueue *&q){
    q=(SqQueue *)malloc(sizeof(SqQueue));
    q->front=q->rear=-1;
}
bool enQueue(SqQueue *&q, int e){
    if(q->rear==MAX-1) return false;
    q->rear++;
    q->data[q->rear]=e;
    return true;
}
bool QueueEmpty(SqQueue *q){
    return(q->front==q->rear);
}
bool deQueue(SqQueue *&q, int &e){
    if(q->front==q->rear) return false;
    q->front++;
    e=q->data[q->front];
    return true;
}
void BFS(AdjGraph *G, int v){
    int w, i;
    ArcNode *p;
    SqQueue *qu;
    int visitedBFS[MAXV];
    InitQueue(qu);
    for(i=0;i<G->n;i++)
        visitedBFS[i]=0;
    printf("%2d",v);
    visitedBFS[v]=1;
    enQueue(qu,v);
    while(!QueueEmpty(qu)){
        deQueue(qu,w);
        p=G->adjlist[w].firstarc;
        while(p!=NULL){
            if(visitedBFS[p->adjvex]==0){
                printf("%2d",p->adjvex);
                visitedBFS[p->adjvex]=1;
                enQueue(qu,p->adjvex);
            }
            p=p->nextarc;
        }
    }
    printf("\n");
} 
int main(){
    AdjGraph G;
    int A[6][6]={0,5,INF,7,INF,INF,
                 INF,0,4,INF,INF,INF,
                 8,INF,0,INF,INF,9,
                 INF,INF,5,0,INF,6,
                 INF,INF,5,INF,0,INF,
                 3,INF,INF,INF,1,0};
    CreateAdj(&G,A,6,10);
    printf("深度优先遍历:");
    DFS(G, 0);
    printf("\n广度优先遍历:");
    BFS(G, 0);
    return 0;
}
运行结果及报错内容

CreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e)中“int e"报错,改函数为CreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n),”int n"报错。
为什么会报错?10和6都是int型没问题啊,也没有格式的问题,并且貌似只要是调用的最后一个数据就会报错

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 6月4日
    • 修改了问题 5月27日
    • 创建了问题 5月27日

    悬赏问题

    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 请问这个是什么意思?
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样
    • ¥15 java的GUI的运用
    • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。