请问怎么解决在debug的时候在transfer函数 p->node[i]->firstarc=NULL卡住了的问题

lim_porket 2018-06-13 10:19:00
#include <stdio.h>
#include <stdlib.h>

typedef struct st_arc
{
int adivex;
struct st_arc *nextarc;
} starc;


typedef struct head_node
{
int vexdata;
starc *firstarc;
}hnode;


typedef struct hnode_array
{
int num;
hnode *node [6];
}hnarry;


void transfer (int (*A)[6],hnarry *p);
void output (hnarry *p);



int main ()
{
int i,j;
int A[6][6];
hnarry *p;
p=(hnarry *)malloc(sizeof (hnarry));
printf ("Please input the adjacency list:\n");
for (i=0;i<6;i++)
{
for (j=0;j<6;j++)
{
scanf ("%d",&A[i][j]);
}
}
transfer (A,p);
output (p);
return 0;
}



void transfer (int (*A)[6],hnarry *p)
{
int i,j;
starc *m,*n;
p->num=0;
for (i=0;i<6;i++)
{
p->node[i]->vexdata=i;
p->node[i]->firstarc=NULL;
p->num++;
}

for (i=0;i<6;i++)
{
for (j=0;j<6;j++)
{
if(A[i][j]!=0)
{
m=(starc*)malloc(sizeof (starc));
m->adivex=A[i][j];
if(p->node[i]->firstarc==NULL)
{
p->node[j]->firstarc=m;
}
else
{
n->nextarc=m;
}
n=m;
n->nextarc=NULL;
}
}
}
}



void output (hnarry *p)
{
int i;
starc *t;
for (i=0;i<6;i++)
{
printf ("%d",p->node[i]->vexdata);
if(p->node[i]->firstarc!=NULL)
t=p->node[i]->firstarc;
else
continue;
while (1)
if (t->nextarc!=NULL)
{
printf ("%d",t->adivex);
t=t->nextarc;
}
else
{
printf ("%d",t->adivex);
continue;
}
}
}
...全文
694 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2018-06-14
  • 打赏
  • 举报
回复
if(p->node[i]->firstarc==NULL)
                {
                    p->node[j]->firstarc=m;
                }
                else
                {
                    n->nextarc=m;
                }
注意p->node[j],node[j]是野指针,需要为其分配空间才能进行这句操作p->node[j]->firstarc=m;否则会出现段错误;
while (1)
            if (t->nextarc!=NULL)
            {
                printf ("%d\t",t->adivex);
                t=t->nextarc;
            }
            else
            {
                printf ("%d\n",t->adivex);
                //continue;
                break;
            }
    }
这里的continue应改成break,否则这是一个死循环。 以下是我做的稍微修改,参考一下吧:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct st_arc
{
    int adivex;
    struct st_arc *nextarc;
} starc;


typedef struct head_node
{
    int vexdata;
    starc *firstarc;
}hnode;


typedef struct  hnode_array
{
    int num;
    hnode node [6];
}hnarry;


void transfer (int (*A)[6],hnarry *p);
void output (hnarry *p);

int main ()
{
    int i,j;
    int A[6][6];
    hnarry *p;
    p=(hnarry *)malloc(sizeof (hnarry));
    printf ("Please input the adjacency list:\n");
    srand(time(NULL));
    for (i=0;i<6;i++)
    {
        for (j=0;j<6;j++)
        {
            //scanf ("%d",&A[i][j]);
            A[i][j] = rand() % 1000;
            printf("%d\t", A[i][j]);
        }
    }
    transfer (A,p);
    output (p);
    return 0;
}



void transfer (int (*A)[6],hnarry *p)
{
    int i,j;
    starc *m,*n;
    p->num=0;
    for (i=0;i<6;i++)
    {
        p->node[i].vexdata=i;
        p->node[i].firstarc=NULL;
        p->num++;
    }
    for (i=0;i<6;i++)
    {
        for (j=0;j<6;j++)
        {
            if(A[i][j]!=0)
            {
                m = (starc*)malloc(sizeof (starc));
                m->adivex = A[i][j];
                if(p->node[i].firstarc==NULL)
                {
                    p->node[j].firstarc = m;
                }
                else
                {
                    n->nextarc = m;
                }
                n=m;
                n->nextarc=NULL;
            }
        }
    }
}



void output (hnarry *p)
{
    int i;
    starc *t;
    for (i=0;i<6;i++)
    {
        printf ("%d\n",p->node[i].vexdata);
        if(p->node[i].firstarc!=NULL)
            t = p->node[i].firstarc;
        else
            continue;
        while (1)
            if (t->nextarc!=NULL)
            {
                printf ("%d\t",t->adivex);
                t=t->nextarc;
            }
            else
            {
                printf ("%d\n",t->adivex);
                //continue;
                break;
            }
    }
}
赵4老师 2018-06-14
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧