C语言 program received signal sigsegv问题求解决

#include<stdio.h> 
#include<cstring>
#define MaxV 100                    //宏定义最大顶点数
int visited[MaxV];                  //设置标记数组
typedef struct ArcNode
{
     int adjvex;              		//邻接点域,存储该邻点顶点对应的下标
     struct ArcNode *nextarc;		//邻节点
}ArcNode;
typedef struct VNode
{
     char data;	   			        //顶点对应的数据
     ArcNode  *firstarc;            //边表头指针指向邻顶点
}VNode,AdjList[MaxV];
typedef struct
{
     AdjList  vertices;             //MaxV个顶点结点
     int vexnum,arcnum;			    //顶点数,边数
}ALGraph;
typedef struct QNode
{
    int data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct {
    int    data[MaxV];
    int    front,rear;
}Queue;                             //队列结构的定义
//void CreateALGraph(ALGraph *G);     //无向图的创建
//void DFS(ALGraph G,int i);          //深度优先搜索遍历
//void InitQueue(Queue &Q);           //初始化队列
//void EnQueue(Queue &Q,int e);       //入队
//bool QueueEmpty(Queue &Q);          //判断队列是否为空
//void DeQueue(Queue &Q,int &e);      //出队
//void BFS(ALGraph &G);               //广度优先搜索遍历
void CreateALGraph(ALGraph *G)
{
	printf("分别输入总顶点与总边的数目:\n");
	scanf("%d %d",&G->vexnum,&G->arcnum);
	printf("依次输入%d个顶点值:\n",G->vexnum);
	int k;
    for(k=1;k<=G->vexnum;k++)
    {
    	scanf("%c",&G->vertices[k].data);
        G->vertices[k].firstarc=NULL;
    }
    printf("依次输入%d条边起始点与结束点对应的下标:\n",G->arcnum);
    for(k=1;k<=G->arcnum;k++)
    {
        int i,j;                    //i表示这条边起始点对应的下标
                                    //j表示这条边结束点对应的下标
        scanf("%d%d",&i,&j);
        ArcNode *p=new ArcNode;
        p->adjvex=j;
        p->nextarc=G->vertices[i].firstarc;
        G->vertices[i].firstarc=p;
        ArcNode *q=new ArcNode;
        q->adjvex=i;
        q->nextarc=G->vertices[j].firstarc;
        G->vertices[j].firstarc=q;
    }
}
void DFS(ALGraph G,int i)
{
    visited[i]=1;                    //将要访问过的置为1
    printf("%c ",G.vertices[i].data);   //输出顶点
    ArcNode *p=G.vertices[i].firstarc;
    while(p)
    {
        int j;
		j=p->adjvex;
        if(!visited[j])
            DFS(G,j);
        p=p->nextarc;
    }
}
void InitQueue(Queue &Q)
{
    Q.front=Q.rear=0;
}
void EnQueue(Queue &Q,int e)
{
    if ((Q.rear+1)%MaxV == Q.front)
        return;
    Q.data[Q.rear]=e;
    Q.rear=(Q.rear+1)%MaxV;
}
bool QueueEmpty(Queue &Q)
{
    if (Q.front==Q.rear)
        return true;
    else
        return false;
}
void DeQueue(Queue &Q,int &e)
{
    if (Q.front==Q.rear)
        return;
    e=Q.data[Q.front];
    Q.front=(Q.front+1)%MaxV;
}
void BFS(ALGraph &G)
{
    ArcNode *p;
   	Queue Q;
   	int j;
   	InitQueue(Q);
    for(j = 1; j<=G.vexnum; j++)
    {
        if(!visited[j])
        {
        	printf("%c ",G.vertices[j].data); //打印顶点
			visited[j] = 1;
            EnQueue(Q,j);
            while(!QueueEmpty(Q))
            {
                int m;
                DeQueue(Q,m);      		 //访问点出队列
                p=G.vertices[m].firstarc;//找到当前顶点边表链表头指针
                while(p)
                {
                    if(!visited[p->adjvex])
                    {
                    	printf("%c ",G.vertices[p->adjvex].data);
                        visited[p->adjvex] = 1;
                        EnQueue(Q,p->adjvex);
                    }
                    p=p->nextarc;
                }
            }
        }
    }
}
int main()
{
    ALGraph G;                         //定义无向图G
    CreateALGraph(&G);                 //创建邻接表
    memset(visited,0,sizeof(visited)); //将标记数组置为0
    printf("深度优先搜索遍历:");
    DFS(G,1);                          //从第一个顶点进行深度优先遍历
    printf("\n");
    memset(visited,0,sizeof(visited)); //将标记数组置为0
    printf("广度优先搜索遍历:");
    BFS(G);                            //广度优先搜索遍历
    return 0;
}

c语言的代码,是用C++语言改编的,C++环境下可以运行,但是改了代码后运行不了,调试过程中会出现program received signal sigsegv问题,求解!

一般是访问了错误的地址,从头到尾跟一下看看哪个地方出的问题。

另外,c语言中没有cstring这个头文件,#include<cstring>改成#include <string>看看是不是这个问题。

如有帮助,请采纳一下,谢谢。

改得不彻底

1) <cstring>要改为<string.h>

2) C++没有new,要用malloc;

3) C++没有引用,要用指针

4)C++没有bool类型,用整数好了。

我把这些改好了,可以运行了。

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>

#define MaxV 100                    //宏定义最大顶点数
int visited[MaxV];                  //设置标记数组
typedef struct ArcNode
{
     int adjvex;              		//邻接点域,存储该邻点顶点对应的下标
     struct ArcNode *nextarc;		//邻节点
}ArcNode;
typedef struct VNode
{
     char data;	   			        //顶点对应的数据
     ArcNode  *firstarc;            //边表头指针指向邻顶点
}VNode,AdjList[MaxV];
typedef struct
{
     AdjList  vertices;             //MaxV个顶点结点
     int vexnum,arcnum;			    //顶点数,边数
}ALGraph;
typedef struct QNode
{
    int data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct {
    int    data[MaxV];
    int    front,rear;
}Queue;                             //队列结构的定义
//void CreateALGraph(ALGraph *G);     //无向图的创建
//void DFS(ALGraph G,int i);          //深度优先搜索遍历
//void InitQueue(Queue &Q);           //初始化队列
//void EnQueue(Queue &Q,int e);       //入队
//bool QueueEmpty(Queue &Q);          //判断队列是否为空
//void DeQueue(Queue &Q,int &e);      //出队
//void BFS(ALGraph &G);               //广度优先搜索遍历
void CreateALGraph(ALGraph *G)
{
	printf("分别输入总顶点与总边的数目:\n");
	scanf("%d %d",&G->vexnum,&G->arcnum);
	printf("依次输入%d个顶点值:\n",G->vexnum);
	int k;
    for(k=1;k<=G->vexnum;k++)
    {
    	scanf("%c",&G->vertices[k].data);
        G->vertices[k].firstarc=NULL;
    }
    printf("依次输入%d条边起始点与结束点对应的下标:\n",G->arcnum);
    for(k=1;k<=G->arcnum;k++)
    {
        int i,j;                    //i表示这条边起始点对应的下标
                                    //j表示这条边结束点对应的下标
        scanf("%d%d",&i,&j);
        ArcNode *p= (ArcNode*)malloc(sizeof(ArcNode));
        p->adjvex=j;
        p->nextarc=G->vertices[i].firstarc;
        G->vertices[i].firstarc=p;
        ArcNode *q=(ArcNode*)malloc(sizeof(ArcNode));
        q->adjvex=i;
        q->nextarc=G->vertices[j].firstarc;
        G->vertices[j].firstarc=q;
    }
}
void DFS(ALGraph *G,int i)
{
    visited[i]=1;                    //将要访问过的置为1
    printf("%c ",G->vertices[i].data);   //输出顶点
    ArcNode *p=G->vertices[i].firstarc;
    while(p)
    {
        int j;
		j=p->adjvex;
        if(!visited[j])
            DFS(G,j);
        p=p->nextarc;
    }
}
void InitQueue(Queue *Q)
{
    Q->front = Q->rear=0;
}
void EnQueue(Queue *Q, int e)
{
    if ((Q->rear+1)%MaxV == Q->front)
        return;
    Q->data[Q->rear]=e;
    Q->rear=(Q->rear+1)%MaxV;
}
int QueueEmpty(Queue *Q)
{
    if (Q->front==Q->rear)
        return 1;
    else
        return 0;
}
int DeQueue(Queue *Q)
{
    int e;
    if (Q->front==Q->rear)
        return -1;
    e=Q->data[Q->front];
    Q->front=(Q->front+1)%MaxV;
}
void BFS(ALGraph *G)
{
    ArcNode *p;
   	Queue Q;
   	int j;
   	InitQueue(&Q);
    for(j = 1; j<=G->vexnum; j++)
    {
        if(!visited[j])
        {
        	printf("%c ",G->vertices[j].data); //打印顶点
			visited[j] = 1;
            EnQueue(&Q,j);
            while(!QueueEmpty(&Q))
            {
                int m;
                m = DeQueue(&Q);      		 //访问点出队列
                p=G->vertices[m].firstarc;//找到当前顶点边表链表头指针
                while(p)
                {
                    if(!visited[p->adjvex])
                    {
                    	printf("%c ",G->vertices[p->adjvex].data);
                        visited[p->adjvex] = 1;
                        EnQueue(&Q,p->adjvex);
                    }
                    p=p->nextarc;
                }
            }
        }
    }
}
int main()
{
    ALGraph G;                         //定义无向图G
    CreateALGraph(&G);                 //创建邻接表
    memset(visited,0,sizeof(visited)); //将标记数组置为0
    printf("深度优先搜索遍历:");
    DFS(&G,1);                          //从第一个顶点进行深度优先遍历
    printf("\n");
    memset(visited,0,sizeof(visited)); //将标记数组置为0
    printf("广度优先搜索遍历:");
    BFS(&G);                            //广度优先搜索遍历
    return 0;
}

 

输入截屏