鄰接表的深度優先遍歷以及廣度優先遍歷
阿新 • • 發佈:2019-02-02
#include <stdio.h> #include <stdlib.h> #define MAX 20 int visited[20]; typedef struct ArcNode{ int adjvex; struct ArcNode *nextarc; }ArcNode,*LinkNode; typedef struct{ char data; ArcNode *firstarc; }VNode,AdjList[MAX]; typedef struct{ AdjList vexs; int vexnum,arcnum; }ALGraph; typedef struct{ LinkNode *base; int top; int stacksize; }SqStack; typedef struct{ LinkNode *Qbase; int front,rear; }SqQueue; int FirstAdj(ALGraph G, char v){ int i; for(i = 0; i < G.vexnum; i ++){ if(G.vexs[i].data == v) return i; } return -1; } void CreateALGraphList(ALGraph &G){ int i,j,k; char v1,v2; ArcNode *p,*s; printf("請輸入節點數目和邊的數目:\n"); scanf("%d%d",&G.vexnum,&G.arcnum); getchar(); printf("請輸入各個頂點:\n"); for(i = 0; i < G.vexnum; i ++){ scanf("%c",&G.vexs[i].data); G.vexs[i].firstarc = NULL; } printf("請輸入各個頂點連成的邊:\n"); for(i = 0; i < G.arcnum; i ++){ getchar(); scanf("%c%c",&v1,&v2); j = FirstAdj(G,v1); k = FirstAdj(G,v2); s = (ArcNode *)malloc(sizeof(ArcNode)); s->adjvex = k; s->nextarc = NULL; if(!G.vexs[j].firstarc){ G.vexs[j].firstarc = s; } else{ p = G.vexs[j].firstarc; while(p->nextarc) p = p->nextarc; p->nextarc = s; } } } void InitStack(SqStack &S){ S.base = (LinkNode *)malloc(sizeof(LinkNode) * MAX); if(!S.base) return ; S.top = 0; S.stacksize = MAX; } int StackEmpty(SqStack S){ if(!S.top) return 1; return 0; } void Push(SqStack &S, LinkNode p){ if(S.top >= S.stacksize){ S.base = (LinkNode *)realloc(S.base,(S.top + MAX) * sizeof(LinkNode)); if(!S.base) return ; S.stacksize += MAX; } S.base[S.top ++] = p; } void Pop(SqStack &S, LinkNode &p){ if(!S.top) return ; p = S.base[-- S.top]; } void DFS(ALGraph G, int i){ LinkNode p; printf("%3c",G.vexs[i].data); visited[i] = 1; for(p = G.vexs[i].firstarc; p; p = p->nextarc){ if(!visited[p->adjvex]) DFS(G,p->adjvex); } } void DFSTraverse(ALGraph G){ int i; for(i = 0; i < G.vexnum; i ++){ if(!visited[i]) DFS(G,i); } } void DFS1(ALGraph G, int i){ SqStack S; ArcNode *p; int k; InitStack(S); printf("%3c",G.vexs[i].data); visited[i] = 1; p = G.vexs[i].firstarc; while(p || !StackEmpty(S)){ while(p){ if(visited[p->adjvex]) p = p->nextarc; else{ printf("%3c",G.vexs[p->adjvex].data); visited[p->adjvex] = 1; Push(S,p); p = G.vexs[p->adjvex].firstarc; } } if(!StackEmpty(S)){ Pop(S,p); p = p->nextarc; } } } void DFSTraverse1(ALGraph G){ int i; for(i = 0; i < G.vexnum; i ++) visited[i] = 0; for(i = 0; i < G.vexnum; i ++){ if(!visited[i]) DFS1(G,i); } } void InitQueue(SqQueue &Q){ Q.Qbase = (LinkNode *)malloc(sizeof(LinkNode) * MAX); if(!Q.Qbase) return ; Q.front = Q.rear = 0; } void EnQueue(SqQueue &Q, LinkNode p){ if((Q.rear + 1) % MAX == Q.front) return ; Q.Qbase[Q.rear] = p; Q.rear = (Q.rear + 1) % MAX; } void DeQueue(SqQueue &Q, LinkNode &p){ if(Q.front == Q.rear ) return ; p = Q.Qbase[Q.front]; Q.front = (Q.front + 1) % MAX; } int QueueEmpty(SqQueue Q){ if(Q.front == Q.rear) return 1; return 0; } void BFS(ALGraph G, int i){ LinkNode p; SqQueue Q; printf("%3c",G.vexs[i].data); visited[i] = 1; InitQueue(Q); p = G.vexs[i].firstarc; while(p || !QueueEmpty(Q)){ while(p){ if(visited[p->adjvex]) p = p->nextarc; else{ printf("%3c",G.vexs[p->adjvex].data); visited[p->adjvex] = 1; EnQueue(Q,p); p = p->nextarc; } } if(!QueueEmpty(Q)){ DeQueue(Q,p); p = G.vexs[p->adjvex].firstarc; } } } void BFSTraverse(ALGraph G){ int i; for(i = 0; i < G.vexnum; i ++) visited[i] = 0; for(i = 0; i < G.vexnum; i ++) if(!visited[i]) BFS(G,i); } int main(){ ALGraph G; CreateALGraphList(G); DFSTraverse(G); putchar('\n'); DFSTraverse1(G); putchar('\n'); BFSTraverse(G); return 0; }