| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 708 人关注过本帖
标题:求这道题的逻辑错误
只看楼主 加入收藏
ldj34089850
Rank: 2
等 级:论坛游民
帖 子:22
专家分:12
注 册:2013-11-10
结帖率:60%
收藏
已结贴  问题点数:20 回复次数:3 
求这道题的逻辑错误
#include<stdio.h>
#include<stdlib.h>
#define MAXV 100
#define INF 32767
int visited[MAXV];
typedef int InfoType;
typedef struct{
    int no;
    InfoType info;
}VertexType;
typedef struct{
    int edges[MAXV][MAXV];
    int n,e;
    VertexType vexs[MAXV];
}MGraph;
typedef struct ANode{
    int adjvex;
    struct ANode *nextarc;
    InfoType info;
}ArcNode;
typedef struct Vnode{
    VertexType data;
    ArcNode *firstarc;
}VNode;
typedef VNode AdjList[MAXV];
typedef struct{
    AdjList adjlist;
    int n,e;
}ALGraph;

void DFS(ALGraph *G,int v)                    //深度遍历递归算法
{
    ArcNode *p;
    visited[v]=1;
    printf("%d",v);
    p=G->adjlist[v].firstarc;
    while(p!=NULL)
    {
        if(visited[p->adjvex]==0)
            DFS(G,p->adjvex);
        p=p->nextarc;
    }
}

void MatToList(MGraph g,ALGraph *&G)         //将邻接矩阵g转换成邻接表G
{
    int i,j;
    ArcNode *p;
    G=(ALGraph*)malloc(sizeof(ALGraph));
    for(i=0;i<g.n;i++)
        for(j=g.n-1;j>=0;j--)
            if(g.edges[i][j]!=0&&g.edges[i][j]!=INF)
            {
                p=(ArcNode*)malloc(sizeof(ArcNode));
                p->adjvex=j;
                p->info=g.edges[i][j];
                p->nextarc=G->adjlist[i].firstarc;
                G->adjlist[i].firstarc=p;
            }
            G->n=g.n;
            G->e=g.e;
}

void BFS(ALGraph *G,int v)
{
    ArcNode *p;
    int queue[MAXV],front=0,rear=0;
    int visited[MAXV];
    int w,i;
    for(i=0;i<G->n;i++)
        visited[i]=0;
    printf("%2d",v);
    visited[v]=1;
    rear=(rear+1)%MAXV;
    queue[rear]=v;
    while(front!=rear)
    {
        front=(front+1)%MAXV;
        w=queue[front];
        p=G->adjlist[w].firstarc;
        while(p!=NULL)
        {
            if(visited[p->adjvex]==0)
            {
                printf("%2d",p->adjvex);
                visited[p->adjvex]=1;
                rear=(rear+1)%MAXV;
                queue[rear]=p->adjvex;
            }
            p=p->nextarc;
        }
    }
    printf("\n");
}

void DFS1(ALGraph *G,int v)
{
    int i,w;
    int top=-1;
    ArcNode *p,*St[MAXV];
    for(i=0;i<G->n;i++)
        visited[i]=0;
    visited[v]=1;
    top++;
    St[top]=G->adjlist[v].firstarc;
    while(top>-1)
    {
        p=St[MAXV];
        top--;
        w=p->adjvex;
        if(visited[w]==0)
        {
            printf("%d",w);
            visited[w]=1;
            top++;
            St[top]=G->adjlist[w].firstarc;
        }
        p=p->nextarc;
    }
}

void main()
{
    int i,j;
    MGraph g;
    ALGraph *G;
    int A[6][6]={
        {0,5,INF,7,INF,INF},
        {INF,0,2,INF,INF,INF},
        {8,INF,0,INF,INF,9},
        {INF,INF,5,0,INF,6},
        {INF,INF,INF,5,0,INF},
        {3,INF,INF,INF,1,0}};
        g.n=6;g.e=10;
        for(i=0;i<g.n;i++)
            for(j=0;j<g.n;j++)
                g.edges[i][j]=A[i][j];
            G=(ALGraph *)malloc(sizeof(ALGraph));
            MatToList(g,G);
            printf("有向图G从顶点0开始的深度优先遍历序列递归算法:\n");
            DFS(G,0);
            printf("有向图G从顶点0开始的深度优先遍历序列非递归算法:\n");
        //    DFS1(G,0);
            printf("有向图G从顶点0开始的广度优先遍历:\n");
        //    BFS(G,0);
}
搜索更多相关主题的帖子: include 
2013-11-28 23:22
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:7 
某人去看医生,只说:病了。
就没有进一步说明了

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-11-29 09:57
何时是月明
Rank: 2
等 级:论坛游民
帖 子:30
专家分:76
注 册:2011-9-27
收藏
得分:7 
书上的代码吧?
2013-11-29 17:17
ldj34089850
Rank: 2
等 级:论坛游民
帖 子:22
专家分:12
注 册:2013-11-10
收藏
得分:0 
回复 3楼 何时是月明
这不是书上的代码,当我运行这个代码时会发生这个程序不能正常运行,这怎么破
2013-11-29 18:30
快速回复:求这道题的逻辑错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014747 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved