问题解决,这么长时间一条回复都没,太失败了。
#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;
}