注册 登录
编程论坛 数据结构与算法

求这道题的逻辑错误

ldj34089850 发布于 2013-11-28 23:22, 708 次点击
#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);
}
3 回复
#2
yuccn2013-11-29 09:57
某人去看医生,只说:病了。
就没有进一步说明了
#3
何时是月明2013-11-29 17:17
书上的代码吧?
#4
ldj340898502013-11-29 18:30
回复 3楼 何时是月明
这不是书上的代码,当我运行这个代码时会发生这个程序不能正常运行,这怎么破
1