数据结构 为什么我运行时输入后被强行退出

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define MVNum 100
typedef int OtherInfo;
typedef char VerTexType;
typedef struct ArcNode{
    int adjvex;
    struct ArcNode*nextarc;
    OtherInfo info;
}ArcNode;

typedef struct VNode{
VerTexType data;
ArcNode*firstarc;
}VNode,AdjList[MVNum];

typedef struct{
    AdjList vertices;
    int  vexnum,arcnum;
}ALGraph;

int LocateNode(ALGraph G,VerTexType v){
    int i;
    for(i=0;i<G.vexnum;i++){
        if(v==G.vertices[i].data){
            return i;
        }
    }
    return 0;
}

int  CreateGraph(ALGraph *G){
    printf("------开始创建无向网------\n");
    printf("请输入顶点数和边数:");
    scanf("%d %d",&G->vexnum,&G->arcnum);
    fflush(stdin);
    int n;

for(n=0;n<G->vexnum;n++){
    printf("\n请输入第%d个顶点:",n+1);
    G->vertices[n].data=getche();
    G->vertices[n].firstarc=NULL;
}
 int i,j;
 for(n=0;n<G->arcnum;n++){
    printf("\n请输入第%d条边关联的顶点和权值,以空格间隔:",n+1);
    fflush(stdin);
    VerTexType v1,v2;
    OtherInfo w;
    scanf("%c %c %d",&v1,&v2,&w);
    i=LocateNode(*G,v1);
    j=LocateNode(*G,v2);
    ArcNode *p1=(ArcNode *)malloc(sizeof(ArcNode));
    p1->adjvex=j;
    p1->info=w;
    p1->nextarc=G->vertices[i].firstarc;
    G->vertices[i].firstarc=p1;
    ArcNode *p2=(ArcNode *)malloc(sizeof(ArcNode));
    p2->adjvex=i;
    p2->info=w;
    p2->nextarc=G->vertices[j].firstarc;
    G->vertices[j].firstarc=p2;

 }
 return 0;
}

int ShowGraph(ALGraph *G)
    {int i;
    for (i=0;i<G->vexnum;i++)
    {
        printf("%d->",i+1);
        while(1)
        {
            if(G->vertices[i].firstarc==NULL)
            {
                printf("^");
                break;
            }
            printf("%d->",G->vertices[i].firstarc->adjvex+1);
            G->vertices[i].firstarc=G->vertices[i].firstarc->nextarc;
        }
        printf("\n");
    }
}

int main()
{
    ALGraph *G;
    CreateGraph(G);
    ShowGraph(G);
    return 0;
}
 

问题主要是输入输出缓冲区残留字符的处理,修改如下,供参考:

#include <conio.h> //getche()需要的头文件
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define MVNum 100
typedef int OtherInfo;
typedef char VerTexType;
typedef struct ArcNode{
    int adjvex;
    struct ArcNode*nextarc;
    OtherInfo info;
}ArcNode;

typedef struct VNode{
VerTexType data;
ArcNode*firstarc;
}VNode,AdjList[MVNum];

typedef struct{
    AdjList vertices;
    int  vexnum,arcnum;
}ALGraph;

int LocateNode(ALGraph G,VerTexType v){
    int i;
    for(i=0;i<G.vexnum;i++){
        if(v==G.vertices[i].data){
            return i;
        }
    }
    return 0;
}

int  CreateGraph(ALGraph *G){
    printf("------开始创建无向网------\n");
    printf("请输入顶点数和边数:");
    fflush(stdout);rewind(stdin);
    scanf("%d %d",&G->vexnum,&G->arcnum);
                                   //fflush(stdin);
    int n;

for(n=0;n<G->vexnum;n++){
    printf("\n请输入第%d个顶点:",n+1);
    fflush(stdout);rewind(stdin);
    G->vertices[n].data=getchar();//G->vertices[n].data=getche(); 改用getchar()更合理
    G->vertices[n].firstarc=NULL;
 }
 int i,j;
 for(n=0;n<G->arcnum;n++){
    printf("\n请输入第%d条边关联的顶点和权值,以空格间隔:",n+1);
    fflush(stdout);rewind(stdin);//fflush(stdin);
    VerTexType v1,v2;
    OtherInfo w;
    scanf("%c %c %d",&v1,&v2,&w);
    i=LocateNode(*G,v1);
    j=LocateNode(*G,v2);
    ArcNode *p1=(ArcNode *)malloc(sizeof(ArcNode));
    p1->adjvex=j;
    p1->info=w;
    p1->nextarc=G->vertices[i].firstarc;
    G->vertices[i].firstarc=p1;
    ArcNode *p2=(ArcNode *)malloc(sizeof(ArcNode));
    p2->adjvex=i;
    p2->info=w;
    p2->nextarc=G->vertices[j].firstarc;
    G->vertices[j].firstarc=p2;

 }
 return 0;
}

int ShowGraph(ALGraph *G)
    {int i;
    for (i=0;i<G->vexnum;i++)
    {
        printf("%d->",i+1);
        while(1)
        {
            if(G->vertices[i].firstarc==NULL)
            {
                printf("^");
                break;
            }
            printf("%d->",G->vertices[i].firstarc->adjvex+1);
            G->vertices[i].firstarc=G->vertices[i].firstarc->nextarc;
        }
        printf("\n");
    }
}

int main()
{
    ALGraph *G;
    CreateGraph(G);
    ShowGraph(G);
    
    return 0;
}

 

你这里想要输入数据但是没有写scanf语句,所以会报错

for(n=0;n<G->vexnum;n++){
    printf("\n请输入第%d个顶点:",n+1);
    G->vertices[n].data=getche();
    G->vertices[n].firstarc=NULL;
}

 

printf("\n请输入第%d个顶点:",n+1);
    G->vertices[n].data=getche();

//getche是什么东西,getchar()吧,但是建议不要用getchar(),用getchar()会吧回车符什么的也算进来,用scanf吧。

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632