数据结构C语言版之邻接表(各种遍历)

//邻接表:

#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define Stack_size 100
#define Stackincreament 10
#define MAX_VERTE_NUM 20
int visited[MAX_VERTE_NUM];
typedef struct ArcNode{
	int adjvex;
	struct ArcNode *nextArc;
}ArcNode,*N;
typedef struct VNode{
	char data;
	ArcNode *firstarc;
}VNode,AdjList[MAX_VERTE_NUM];
typedef struct{
	AdjList vertices;
	int vexnum,arcnum;
}ALGraph;
int Location(ALGraph ALG,char v)
{
	for(int i=0;i<ALG.vexnum;i++)
		if(ALG.vertices[i].data==v)
			return i;
	return -1;
}

void CreatALGraph(ALGraph &ALG)
{
	int i,m,n;
	char v1,v2;
	printf("请输入图的顶点数和边数:\n");
	scanf("%d",&ALG.vexnum);
	scanf("%d",&ALG.arcnum);
	getchar();
	printf("请输入各个顶点:\n");
	for(i=0;i<ALG.vexnum;i++)
	{
		scanf("%c",&ALG.vertices[i].data);
		ALG.vertices[i].firstarc=NULL;
	}
	printf("请输入图的各边:\n");
	getchar();
	for(i=0;i<ALG.arcnum;i++)
	{
		ArcNode *p,*s;
		scanf("%c%c",&v1,&v2);
		getchar();
		m=Location(ALG,v1);
		n=Location(ALG,v2);
		p=(ArcNode*)malloc(sizeof(ArcNode));
		p->adjvex=n;
		p->nextArc=NULL;
		if(!ALG.vertices[m].firstarc)
			ALG.vertices[m].firstarc=p;
		else
		{
			ArcNode *q;
			q=ALG.vertices[m].firstarc;
			while(q->nextArc)
				q=q->nextArc;
			
			q->nextArc=p;
		}	
		s=(ArcNode*)malloc(sizeof(ArcNode));
		s->adjvex=m;
		s->nextArc=NULL;
		if(!ALG.vertices[n].firstarc)
			ALG.vertices[n].firstarc=s;
		else
		{
			ArcNode *r;
			r=ALG.vertices[m].firstarc;
			while(r->nextArc)
				r=r->nextArc;
			
			r->nextArc=s;
		}		
	}
}
void DFS(ALGraph G,int v)
{
	ArcNode *w;
	visited[v]=1;
	printf("%c",G.vertices[v]);
	for(w=G.vertices[v].firstarc;w;w=w->nextArc)
		if(!visited[w->adjvex])
			DFS(G,w->adjvex);
}
void DFStraverse(ALGraph G)
{//深度优先遍历的递归算法
	int v;
	for(v=0;v<G.vexnum;v++)
		visited[v]=0;
	for(v=0;v<G.vexnum;v++)
		if(!visited[v])
			DFS(G,v);
}
/*typedef struct{
    ArcNode *base;
	int top;
	int stacksize;
}sqstack;
void initstack(sqstack &s)
{
	s.base=(N)malloc(Stack_size*sizeof(ArcNode));
	s.stacksize=Stack_size;
	if(!s.base)
		exit(0);
	s.top=0;
}
int stackempty(sqstack s)
{
	if(s.top==0)
		return 1;
	return 0;
}
void push(sqstack &s,N e)
{
	if(s.top>=s.stacksize)
	{
		s.base=(N)realloc(s.base,(Stack_size+Stackincreament)*sizeof(ArcNode));	
		if(!s.base)
		   exit(0);
		s.stacksize+=Stackincreament;
	}
	s.base[s.top++]=e;
}
void pop(sqstack &s,N e)
{
	if(s.top==0)
		return ;
	e=s.base[--s.top];
	
	
}
void DFS(ALGraph G,int v)
{
	ArcNode *p;
	sqstack s;
	visited[v]=1;
	printf("%c",G.vertices[v].data);
	p=G.vertices[v].firstarc;
	initstack(s);
	while(!stackempty(s) || p)
	{
		while(p)
		{
			if(visited[p->adjvex]==1)
			{
				p=p->nextArc;
			}
			else
			{
				printf("%c",G.vertices[p->adjvex].data);
				visited[p->adjvex]=1;
				push(s,p);
				p=G.vertices[p->adjvex].firstarc;
			}
		}
		if(!stackempty(s))
		{
			pop(s,p);	p=p->nextArc;
		}
	}
}
*/
typedef struct{
	int *data;
	int front;
	int rear;
}SqQueue;
void InitQueue(SqQueue &Q)
{
	Q.data=(int*)malloc(MAX*sizeof(int));
	Q.front=Q.rear=0;
}

int QueueEmpty(SqQueue Q)
{
	if(Q.rear==Q.front)
		return 1;
	return 0;
}
void EnQueue(SqQueue &Q,int e)
{
	if((Q.rear%MAX)>=Q.front)
		return;
	else
	{	
		Q.data[Q.rear]=e;
		Q.rear=(Q.rear+1)%MAX;
	
	}
}
void DeQueue(SqQueue &Q,int &e)
{
	if(QueueEmpty(Q))
		return;
	e=Q.data[Q.front];
	Q.front=(Q.front+1)%MAX;
}
void BFSTraverse(ALGraph G)
{
	int u,v;
	ArcNode *p;
	SqQueue Q;
	InitQueue(Q);
	for(v=0;v<G.vexnum;v++)
		visited[v]=0;
	for(v=0;v<G.vexnum;v++)
	{
		if(!visited[v])
		{
			printf("%c",G.vertices[v].data);	
			EnQueue(Q,v);
			visited[v]=1;
			while(!QueueEmpty(Q))
			{
				
				DeQueue(Q,u);
				for(p=G.vertices[u].firstarc;p;p=p->nextArc)
				{
					if(!visited[p->adjvex])
					{
						printf("%c",G.vertices[p->adjvex].data);
						visited[p->adjvex]=1;
						EnQueue(Q,p->adjvex);
					}
				}
			}
		
		}
	}
		printf("\n");
}
int main()
{
	ALGraph ALG;
	CreatALGraph(ALG);
	DFStraverse(ALG);
	printf("\n");
	BFSTraverse(ALG);

	return 0;
}

posted @ 2015-12-24 12:32  Joe.Smith  阅读(568)  评论(0编辑  收藏  举报