此程序错在哪啊

昵称是可以中文吗 2010-04-17 07:25:52
广度排序 ,编译通过,运行错误
#include"stdio.h" /* EOF(=^Z或F6),NULL */
#include"string.h"
#include"stdlib.h" /* exit() */
#define MAX_NAME 3 /* 顶点字符串的最大长度+1 */
#define MAX_VERTEX_NUM 20
typedef int InfoType; /* 顶点权值类型 */
typedef int VertexType;
typedef int Status;
typedef int ElemType;
typedef struct QNode
{
ElemType data;
QNode* next;
}QueuePtr;

struct LinkQueue
{
QueuePtr *front;
QueuePtr *rear;
};

Status InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = new QNode;
if(!Q.front)
exit(0);
Q.front->next = NULL;
return true;
}

Status QueueEmpty(LinkQueue Q)
{
return (Q.front == Q.rear);
}

Status EnQueue(LinkQueue &Q, ElemType e)
{
QueuePtr *p = new QNode;
if(!p)
exit(0);
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
return true;
}

Status DeQueue(LinkQueue &Q, ElemType &e)
{
if(Q.front == Q.rear)
return false;
QueuePtr *p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if(Q.rear == p)
Q.rear = Q.front;
delete p;
return true;
}
Status DestroyQueue(LinkQueue &Q)
{
while(Q.front)
{
Q.rear = Q.front->next;
delete Q.front;
Q.front = Q.rear;
}
return true;
}

typedef enum{DG,DN,AG,AN}GraphKind; /* {有向图,有向网,无向图,无向网} */
typedef struct ArcNode
{
int adjvex; /* 该弧所指向的顶点的位置 */
struct ArcNode *nextarc; /* 指向下一条弧的指针 */
InfoType *info; /* 网的权值指针) */
}ArcNode; /* 表结点 */

typedef struct
{
VertexType data; /* 顶点信息 */
ArcNode *firstarc; /* 第一个表结点的地址,指向第一条依附该顶点的弧的指针 */
}VNode,AdjList[MAX_VERTEX_NUM]; /* 头结点 */

typedef struct
{
AdjList vertices;
int vexnum,arcnum; /* 图的当前顶点数和弧数 */
int kind; /* 图的种类标志 */
}ALGraph;

int LocateVex(ALGraph G,VertexType u)
{ /* 初始条件: 图G存在,u和G中顶点有相同特征 */
/* 操作结果: 若G中存在顶点u,则返回该顶点在图中位置;否则返回-1 */
int i;
for(i=0;i<G.vexnum;++i)
if(u == G.vertices[i].data)
return i;
return -1;
}

void CreateGraph(ALGraph *G)
{ /* 采用邻接表存储结构,构造没有相关信息的图G(用一个函数构造4种图) */
int i,j,k;
int w; /* 权值 */
VertexType va,vb;
ArcNode *p;
printf("Enter the type of map:(0~3): ");
scanf("%d",&(*G).kind);
printf("Enter Vertex number,Arc number: ");
scanf("%d%d",&(*G).vexnum,&(*G).arcnum);
printf("Enter %d Vertex :\n",(*G).vexnum);
for(i=0;i<(*G).vexnum;++i) /* 构造顶点向量 */
{
scanf("%d",(*G).vertices[i].data);
(*G).vertices[i].firstarc=NULL;
}
if((*G).kind==1||(*G).kind==3) /* 网 */
printf("Enter order every arc weight,head and tail (Takes the gap by the blank space ):\n");
else /* 图 */
printf("Enter order every arc head and tail (Takes the gap by the blank space ):\n");
for(k=0;k<(*G).arcnum;++k) /* 构造表结点链表 */
{
if((*G).kind==1||(*G).kind==3) /* 网 */
scanf("%d%s%s",&w,va,vb);
else /* 图 */
scanf("%s%s",va,vb);
i=LocateVex(*G,va); /* 弧尾 */
j=LocateVex(*G,vb); /* 弧头 */
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
if((*G).kind==1||(*G).kind==3) /* 网 */
{
p->info=(int *)malloc(sizeof(int));
*(p->info)=w;
}
else
p->info=NULL; /* 图 */
p->nextarc=(*G).vertices[i].firstarc; /* 插在表头 */
(*G).vertices[i].firstarc=p;
if((*G).kind>=2) /* 无向图或网,产生第二个表结点 */
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=i;
if((*G).kind==3) /* 无向网 */
{
p->info=(int*)malloc(sizeof(int));
*(p->info)=w;
}
else
p->info=NULL; /* 无向图 */
p->nextarc=(*G).vertices[j].firstarc; /* 插在表头 */
(*G).vertices[j].firstarc=p;
}
}
}

VertexType* GetVex(ALGraph G,int v)
{ /* 初始条件: 图G存在,v是G中某个顶点的序号。操作结果: 返回v的值 */
if(v>=G.vexnum||v<0)
exit(0);
return &G.vertices[v].data;
}

int FirstAdjVex(ALGraph G,VertexType v)
{
/* 初始条件: 图G存在,v是G中某个顶点 */
/* 操作结果: 返回v的第一个邻接顶点的序号。若顶点在G中没有邻接顶点,则返回-1 */
int v1 = LocateVex(G,v); /* v1为顶点v在图G中的序号 */
ArcNode* p=G.vertices[v1].firstarc;
if(p!=NULL)
return p->adjvex;
else
return -1;
}

int NextAdjVex(ALGraph G,int v,int w)
{ /* 初始条件: 图G存在,v是G中某个顶点,w是v的邻接顶点 */
/* 操作结果: 返回v的(相对于w的)下一个邻接顶点的序号。 */
/* 若w是v的最后一个邻接点,则返回-1 */
ArcNode *p;
// v1,w1;
//=LocateVex(G,v); /* v1为顶点v在图G中的序号 */
//=LocateVex(G,w); /* w1为顶点w在图G中的序号 */
p=G.vertices[v].firstarc;
while(p&&p->adjvex!=w) /* 指针p不空且所指表结点不是w */
p=p->nextarc;
if(!p||!p->nextarc) /* 没找到w或w是最后一个邻接点 */
return -1;
else /* p->adjvex==w */
return p->nextarc->adjvex; /* 返回v的(相对于w的)下一个邻接顶点的序号 */
}


int visited[MAX_VERTEX_NUM]; /* 访问标志数组(全局量),未访问标记0,访问标记1 */
void Visit(VertexType v)
{
printf("%c ",v);
}

void BFSTraverse(ALGraph G)
{ int v,u,w;
for(v=0; v < G.vexnum; ++v)
visited[v] = false;
LinkQueue Q;
InitQueue(Q);
for( v=0; v<G.vexnum; ++v)
{
if(!visited[v])
{
visited[v] = 1;
Visit(G.vertices[v].data);
EnQueue(Q,v);
while(!QueueEmpty(Q))
{

DeQueue(Q, u);
for(w = FirstAdjVex(G,u); w>=0; w = NextAdjVex(G, u, w))
{
if(!visited[w])
{
visited[w] = 1;
Visit(G.vertices[w].data);
EnQueue(Q,w);
}
}
}
}
}
DestroyQueue(Q);
}


int main()
{
ALGraph g;
CreateGraph(&g);
BFSTraverse(g);
return 1;
}









...全文
216 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
yekeyishuo 2010-05-01
  • 打赏
  • 举报
回复
帮你调试,成功改了
  • 打赏
  • 举报
回复
我看看先
matrix_01 2010-04-18
  • 打赏
  • 举报
回复
关于队列的结构体有问题吧
这样改吧:
typedef struct QNode/*队列存储结构*/
{
int data;
struct QNode *next;
}QNode,*QueuePtr;

typedef struct
{
QNode *front;/*队头指针*/
QNode *rear;/*队尾指针*/
}LinkQueue;
十八道胡同 2010-04-18
  • 打赏
  • 举报
回复
代码号长

调试也是程序员的必备能力之一
wade_2003 2010-04-18
  • 打赏
  • 举报
回复
好长啊,估计不会。
logiciel 2010-04-18
  • 打赏
  • 举报
回复
关于队列的代码有很多错误,需要大改。
顶点的值是字符型,长度3,现在LZ把它放在int中,由于长度够了,可以这样做,但scanf要修改如下:
scanf("%s",&(*G).vertices[i].data); //scanf("%d",(*G).vertices[i].data);
scanf("%d%s%s",&w,&va,&vb); //scanf("%d%s%s",&w,va,vb);
周靖峰 2010-04-18
  • 打赏
  • 举报
回复
说实话,我看不懂,只能顶了
喜-喜 2010-04-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 scauscnu 的回复:]
引用 2 楼 happycell188 的回复:

看傻了...

调试不出来!帮顶了...

谢谢你啦
[/Quote]

不客气...指针太复杂了!学过C#后就不再用指针了...呵呵...
wjz748305545 2010-04-18
  • 打赏
  • 举报
回复
顶顶顶顶顶
东大坡居士 2010-04-18
  • 打赏
  • 举报
回复
运行啥错误啊~
ForestDB 2010-04-18
  • 打赏
  • 举报
回复
帮顶。
supernoon 2010-04-18
  • 打赏
  • 举报
回复
没错,真的
  • 打赏
  • 举报
回复
先谢谢各位,我再看看
FDWolf 2010-04-18
  • 打赏
  • 举报
回复
真的好长。没看懂,呵呵,帮顶!
时间一粒 2010-04-18
  • 打赏
  • 举报
回复
感觉是内存分配空间有问题吧。但是怎么也改不正确......程序让我看得眼花缭乱~.~
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lcl_data 的回复:]

代码号长

调试也是程序员的必备能力之一
[/Quote]
前面那些是队列的基本定义,是后面错了,跳了好久啊
matrix_01 2010-04-18
  • 打赏
  • 举报
回复
void InitQueue(LinkQueue *p)
{
p->rear=(QueuePtr)malloc(sizeof(QNode));
p->front=p->rear;
if(!p->front)
exit(0);
p->front->next=NULL;
}
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 huanmie_09 的回复:]

自己单步跟踪一把看看吧。
好长的程序,感叹一下.
[/Quote]
调试很久都跳不出来啊,有谁能帮我啊
huanmie_09 2010-04-17
  • 打赏
  • 举报
回复
自己单步跟踪一把看看吧。
好长的程序,感叹一下.
  • 打赏
  • 举报
回复
DDDDDDDDDDDD
加载更多回复(3)

69,382

社区成员

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

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