#include "stdlib.h" #include "stdio.h" //#include "ERROR.h" //#include "Liyang.h" typedef struct { int amount; int *vex; int **matrix; } Graph; void Init(Graph &G); void Create(Graph &G); void Print(Graph G); void Destory(Graph &G); ///============================================================================= void DFS(Graph G,int index);///图的深度遍历DFS void WFS(Graph G,int index);///广度优先遍历WFS ///============================================================================= ///栈的准备操作 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef struct { int *base; int *top; int stacksize; } SqStack; void InitStack(SqStack &S) { ///初始化 S.base = (int*)calloc(STACK_INIT_SIZE,sizeof(int)); S.top = S.base; S.stacksize = STACK_INIT_SIZE; } void Push(SqStack &S,int e) { ///入栈 if(S.top - S.base >= S.stacksize) { S.base = (int*)realloc(S.base,(STACKINCREMENT + S.stacksize)*sizeof(int)); S.top = S.base + S.stacksize; S.stacksize += STACKINCREMENT; } *S.top++ = e; } bool Pop(SqStack &S,int &e) { ///出栈 if(S.top == S.base) { puts("This is an empty stack!"); return false; } else { e = *--S.top; return true; } } bool GetTopS(SqStack S,int &e) { ///得到栈顶元素 if(S.top == S.base) { return false; } else { e = *--S.top; return true; } } ///=============================================================================== ///循环队列的准备 #define MAXQSIZE 100 typedef struct { int *base; int front; int rear; } SqQueue; void InitQueue(SqQueue &Q) { ///初始化 Q.base = (int*)calloc(MAXQSIZE,sizeof(int)); Q.front = Q.rear = 0; } bool EnQueue(SqQueue &Q,int e) { ///入队 if((Q.rear + 1)%MAXQSIZE == Q.front) { puts("队已满."); return false; } else { Q.base[Q.rear] = e; Q.rear = (Q.rear + 1)%MAXQSIZE; return true; } } bool DeQueue(SqQueue &Q,int &e) { ///出队 if(Q.front == Q.rear) { puts("队为空."); return false; } else { e = Q.base[Q.front]; Q.front = (Q.front + 1)%MAXQSIZE; return true; } } bool GetTopQ(SqQueue &Q,int &e) { ///得到队首元素 if(Q.front == Q.rear) { // puts("队为空."); return false; } else { e = Q.base[Q.front]; //Q.front = (Q.front + 1)%MAXQSIZE; return true; } } int main(void) { Graph G; Init(G); Create(G); // Print(G); puts("DFS"); for(int i = 0; i < G.amount; ++i) DFS(G,i); puts("WFS"); for(int i = 0; i < G.amount; ++i) WFS(G,i); Destory(G); return 0; } void Init(Graph &G) { G.amount = 0; G.vex = NULL; G.matrix = NULL; } void Create(Graph &G) { scanf("%d",&G.amount); G.vex = (int*)calloc(G.amount,sizeof(int)); for(int i = 0; i < G.amount; ++i) G.vex[i] = i; G.matrix = (int**)calloc(G.amount,sizeof(int*)); for(int i = 0; i < G.amount; ++i) { G.matrix[i] = (int*)calloc(G.amount,sizeof(int)); for(int j = 0; j < G.amount; ++j) scanf("%d",&G.matrix[i][j]); } } void Print(Graph G) { for(int i = 0; i < G.amount; ++i) { int j = 0; for(; j < G.amount - 1; ++j) printf("%d ",G.matrix[i][j]); printf("%d/n",G.matrix[i][j]); } } void Destory(Graph &G) { for(int i = 0; i < G.amount; ++i) { free(G.matrix[i]); } G.amount = 0; free(G.vex); G.vex = NULL; free(G.matrix); G.matrix = NULL; } ///========================================================================= ///核心部分_非递归实现DFS_栈 void DFS(Graph G,int index) { if(index > G.amount || index < 0) { ///index的值不合法 puts("ERROR!"); exit(0); } int i; bool *IsVisited; IsVisited = (bool*)calloc(G.amount,sizeof(bool)); for(i = 0; i < G.amount; ++i) IsVisited[i] = false; SqStack S; InitStack(S); printf("%d ",G.vex[index]); IsVisited[index] = true; Push(S,index); while(S.base != S.top) { for(i = 0; i < G.amount; ++i) { if(G.matrix[i][index] == 1 && IsVisited[i] == false) { index = i; printf("%d ",G.vex[index]); IsVisited[index] = true; Push(S,index); break; } } if(i == G.amount) { Pop(S,index); GetTopS(S,index); } } puts(""); } ///非递归实现WFS_队列 void WFS(Graph G,int index) { if(index > G.amount || index < 0) { ///index的值不合法 puts("ERROR!"); exit(0); } int i; bool *IsVisited; IsVisited = (bool*)calloc(G.amount,sizeof(bool)); for(i = 0; i < G.amount; ++i) IsVisited[i] = false; SqQueue Q; InitQueue(Q); printf("%d ",G.vex[index]); IsVisited[index] = true; EnQueue(Q,index); while(Q.front != Q.rear) { for(i = 0; i < G.amount; ++i) { if(G.matrix[i][index] == 1 && IsVisited[i] == false) { printf("%d ",G.vex[i]); IsVisited[i] = true; EnQueue(Q,i); } } DeQueue(Q,index); GetTopQ(Q,index); } puts(""); } ///如有BUG,联系邮箱:dabbysunshine@qq.com