+和- 2021-05-21 01:34 采纳率: 100%
浏览 38
已采纳

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

#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;
}
 

  • 写回答

4条回答 默认 最新

  • qzjhjxj 2021-05-21 09:00
    关注

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

    #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;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题