| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 751 人关注过本帖
标题:建图结构编译通过,运行出错
只看楼主 加入收藏
diaoxue
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-6-1
收藏
 问题点数:0 回复次数:1 
建图结构编译通过,运行出错
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define MAX_VERTEX_NUM 20
typedef struct ArcCell
{
   int adj;
  // InfoType *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct
{
   char vexs[MAX_VERTEX_NUM];
   AdjMatrix arcs;
   int weight[MAX_VERTEX_NUM];
   int vexnum,arcnum;
}MGraph;

void CreateGraph(MGraph *G)
{
   printf("input vexnum & arcnum:\n");
//   scanf("%d %d",&G->vexnum,&G->arcnum);
   scanf("%d",&(G->vexnum));//============error
   scanf("%d",&(G->arcnum));
   int i,j;
   for(i=0;i<G->vexnum;i++)
   {
      scanf("\n%c",&(G->vexs[i]));
      printf("%c ",G->vexs[i]);
   }
   printf("\n");
   for(i=0;i<G->vexnum;i++)
   {
      for(j=0;j<G->vexnum;j++)
      {
         G->arcs[i][j].adj=0;
      }
   }
   int k;
   for(k=0;k<G->arcnum;k++)
   {
      int i=0,j=0,w=0;
      printf("input the vertexs and weight:");
      scanf("%d,%d,%d",&i,&j,&w);
      G->arcs[i][j].adj=1;
      G->arcs[j][i].adj=1;
      G->weight[k]=w;
   }
   for(i=0;i<G->vexnum;i++)
   {
      for(j=0;j<G->vexnum;i++)
      {
         printf("%d",G->arcs[i][j].adj);
      }
      printf("\n");
   }
}
int main()
{
   MGraph *G;
   CreateGraph(G);
   system("pause");
   return 0;
}
搜索更多相关主题的帖子: 结构 vexnum int 编译 
2008-04-27 11:45
diaoxue
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-6-1
收藏
得分:0 
问题解决,这么长时间一条回复都没,太失败了。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

//================Queue DS===========================
typedef int QElemType;

typedef struct QNode{
    QElemType data;
    struct QNode *next;
}Queue,*QueuePtr;

typedef struct
{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;
//==================初始化队列=======================
void InitQueue(LinkQueue &Q)
{
   Q.front=Q.rear = (QueuePtr)malloc(sizeof(QNode));
   if(!Q.front)//////
       exit(1);
   Q.front->next=NULL;
 // Q.front->next =Q.rear;
 // Q.rear=NULL;
   //Q.rear=Q.front->next;
}
//===================入队列=========================
void EnQueue(LinkQueue &Q,QElemType e)
{
    QNode *p;
    p=(QueuePtr) malloc (sizeof(QNode));//==========error
    if(!p)
    {
        //cout<<"内存不足!\n";   
        exit(1);
    }
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;   
}
//====================出队列========================
void DeQueue(LinkQueue &Q,QElemType &e)
{

    if(Q.front==Q.rear)
        exit(1);
    QNode *p;
    p=Q.front->next;
    e=p->data;
    Q.front->next=p->next;
    if(Q.rear==p)
        Q.rear=Q.front;
    free(p);
}
//====================队列判空========================
bool QueueEmpty(LinkQueue &Q)
{
    if(Q.front==Q.rear)
        return true;
    else
        return false;

}
//===================Queue End========================
#define MAX 20
bool visited[MAX];
#define MAX_VERTEX_NUM 20
typedef struct ArcCell
{
   int adj;
  // InfoType *info;
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];

typedef struct
{
   char vexs[MAX_VERTEX_NUM];
   AdjMatrix arcs;
   int weight[MAX_VERTEX_NUM];
   int vexnum,arcnum;
}MGraph;
//===================Create Graph=========================
void CreateGraph(MGraph *G)
{
   printf("==============Now Create a ND_Graph!=============\n");
   printf("input vexnum:\n");
//   scanf("%d %d",&G->vexnum,&G->arcnum);
   scanf("%d",&(G->vexnum));
   printf("input arcnum:\n");
   scanf("%d",&(G->arcnum));
   int i,j;
   printf("Input the vexs:");
   for(i=0;i<G->vexnum;i++)
   {
      scanf("\n%c",&(G->vexs[i]));
      //printf("%c ",G->vexs[i]);
   }
   printf("\nThe vexs ares:\n");
   for(i=0;i<G->vexnum;i++)
   {
      printf("%c ",G->vexs[i]);
   }
   printf("\n");
   for(i=0;i<G->vexnum;i++)
   {
      for(j=0;j<G->vexnum;j++)
      {
         G->arcs[i][j].adj=0;
      }
   }
   int k;
   printf("input the vertexs and weight:\n");
   for(k=0;k<G->arcnum;k++)
   {
      int i=0,j=0,w=0;
      printf("input the %d vertexs and weight:\n",k+1);
      scanf("%d %d %d",&i,&j,&w);
     // i--;
     // j--;
      G->arcs[i][j].adj=1;
      G->arcs[j][i].adj=1;
      G->weight[k]=w;
   }
   printf("==============Now  the ND_Graph was created!=============\n");
   for(i=0;i<G->vexnum;i++)
   {
      for(j=0;j<G->vexnum;j++)
      {
         printf(" %d ",G->arcs[i][j].adj);
      }
      printf("\n");
   }

}
//================访问函数==================
int Visit(MGraph *G,int v)
{
    printf("%c",(G->vexs[v]));
//    visited[v]=true;
    return 0;
}
//==================DFS======================
void DFS(MGraph *G,int v)
{
    visited[v]=true;
    Visit(G,v);
    int i;
    for(i=0;i<G->vexnum;i++)
    {
        if(G->arcs[v][i].adj==1 && !visited[i])
            DFS(G,i);
    }
}

void dfsTraverse(MGraph *G)
{
    int i=0;
//    Visit(G,i);
    for(i=0;i<G->vexnum;i++)
        visited[i]=false;
    for(i=0;i<G->vexnum;i++)
    {
        if(!visited[i])
            DFS(G,i);
    }
}
//==================BFS======================
void BfsTraverse(MGraph *G)
{
    int i,
        j;
    for(i=0;i<G->vexnum;i++)
        visited[i]=false;
    LinkQueue Q;
    InitQueue(Q);
    for(i=0;i<G->vexnum ;i++)
    {
        if(!visited[i])
        {
            Visit(G,i);
            visited[i]=true;
            EnQueue(Q,i);
            while(!QueueEmpty(Q))
            {
                int k;
                DeQueue(Q,j);
                for(k=0;k<G->vexnum;k++)
                {
                    if(G->arcs[j][k].adj==1 && !visited[k])
                    {
                        Visit(G,k);
                        visited[k]=true;
                        EnQueue(Q,k);
                    }
                }
            }
        }
    }
}

int main()
{
   MGraph *G;
   G=(MGraph *)malloc(sizeof(MGraph));
   CreateGraph(G);
   printf("\n==========================\n");
   printf("\n============DFS=========\n");
   dfsTraverse(G);
   printf("\n==========================\n");
   printf("\n============BFS=========\n");
   BfsTraverse(G);
   printf("\n==========================\n");
   system("pause");
   return 0;
}

上善若水,水善利万物而不争,处众人之所恶
2008-04-29 22:48
快速回复:建图结构编译通过,运行出错
数据加载中...
 
   



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

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