voidprintAdjacentList(AdjacencyListPtr paraPtr){int i;
AdjacentNodePtr p;int tempNum = paraPtr->numNodes;printf("This is the graph:\r\n");for(i =0; i < tempNum; i ++){
p = paraPtr->headers[i].next;while(p !=NULL){printf("%d, ", p->column);
p = p->next;}printf("\r\n");}}
广度遍历
voidprintAdjacentList(AdjacencyListPtr paraPtr){int i;
AdjacentNodePtr p;int tempNum = paraPtr->numNodes;printf("This is the graph:\r\n");for(i =0; i < tempNum; i ++){
p = paraPtr->headers[i].next;while(p !=NULL){printf("%d, ", p->column);
p = p->next;}printf("\r\n");}}
完整代码
#include<stdio.h>#include<malloc.h>#defineQUEUE_SIZE10int* visitedPtr;//创建图 typedefstructGraph{int** connections;int numNodes;}*GraphPtr;//初始化图
GraphPtr initGraph(int paraSize,int** paraData){int i, j;
GraphPtr resultPtr =(GraphPtr)malloc(sizeof(structGraph));
resultPtr -> numNodes = paraSize;
resultPtr -> connections =(int**)malloc(paraSize *sizeof(int*));for(i =0; i < paraSize; i ++){
resultPtr -> connections[i]=(int*)malloc(paraSize *sizeof(int));for(j =0; j < paraSize; j ++){
resultPtr -> connections[i][j]= paraData[i][j];}}return resultPtr;}//创建链队列 typedefstructGraphNodeQueue{int* nodes;int front;int rear;}GraphNodeQueue,*QueuePtr;//初始化队列
QueuePtr initQueue(){
QueuePtr resultQueuePtr =(QueuePtr)malloc(sizeof(structGraphNodeQueue));
resultQueuePtr->nodes =(int*)malloc(QUEUE_SIZE *sizeof(int));
resultQueuePtr->front =0;
resultQueuePtr->rear =1;return resultQueuePtr;}//判断队列是否为空
bool isQueueEmpty(QueuePtr paraQueuePtr){if((paraQueuePtr->front +1)% QUEUE_SIZE == paraQueuePtr->rear){return true;}return false;}//入队 voidenqueue(QueuePtr paraQueuePtr,int paraNode){if((paraQueuePtr->rear +1)% QUEUE_SIZE == paraQueuePtr->front % QUEUE_SIZE){printf("Error, trying to enqueue %d. queue full.\r\n", paraNode);return;}
paraQueuePtr->nodes[paraQueuePtr->rear]= paraNode;
paraQueuePtr->rear =(paraQueuePtr->rear +1)% QUEUE_SIZE;}//出队 intdequeue(QueuePtr paraQueuePtr){if(isQueueEmpty(paraQueuePtr)){printf("Error, empty queue\r\n");returnNULL;}
paraQueuePtr->front =(paraQueuePtr->front +1)% QUEUE_SIZE;return paraQueuePtr->nodes[paraQueuePtr->front];}//邻接表的结构体定义 typedefstructAdjacencyNode{int column;
AdjacencyNode* next;}AdjacencyNode,*AdjacentNodePtr;typedefstructAdjacencyList{int numNodes;
AdjacencyNode* headers;}AdjacencyList,*AdjacencyListPtr;//将图转为邻接表
AdjacencyListPtr graphToAdjacentList(GraphPtr paraPtr){int i, j, tempNum;
AdjacentNodePtr p, q;
tempNum = paraPtr->numNodes;
AdjacencyListPtr resultPtr =(AdjacencyListPtr)malloc(sizeof(structAdjacencyList));
resultPtr->numNodes = tempNum;
resultPtr->headers =(AdjacencyNode*)malloc(tempNum *sizeof(structAdjacencyNode));for(i =0; i < tempNum; i ++){
p =&(resultPtr->headers[i]);
p->column =-1;
p->next =NULL;for(j =0; j < tempNum; j ++){if(paraPtr->connections[i][j]>0){
q =(AdjacentNodePtr)malloc(sizeof(structAdjacencyNode));
q->column = j;
q->next =NULL;
p->next = q;
p = q;}}}return resultPtr;}//打印邻接表 voidprintAdjacentList(AdjacencyListPtr paraPtr){int i;
AdjacentNodePtr p;int tempNum = paraPtr->numNodes;printf("This is the graph:\r\n");for(i =0; i < tempNum; i ++){
p = paraPtr->headers[i].next;while(p !=NULL){printf("%d, ", p->column);
p = p->next;}printf("\r\n");}}//广度遍历 voidwidthFirstTranverse(AdjacencyListPtr paraListPtr,int paraStart){printf("width first \r\n");int i, j, tempNode;
AdjacentNodePtr p;
i =0;
visitedPtr =(int*)malloc(paraListPtr->numNodes *sizeof(int));for(i =0; i < paraListPtr->numNodes; i ++){
visitedPtr[i]=0;}
QueuePtr tempQueuePtr =initQueue();printf("%d\t", paraStart);
visitedPtr[paraStart]=1;enqueue(tempQueuePtr, paraStart);while(!isQueueEmpty(tempQueuePtr)){
tempNode =dequeue(tempQueuePtr);for(p =&(paraListPtr->headers[tempNode]); p !=NULL; p = p->next){
j = p->column;if(visitedPtr[j])continue;printf("%d\t", j);
visitedPtr[j]=1;enqueue(tempQueuePtr, j);}}printf("\r\n");}//测试 voidtestGraphTranverse(){int i, j;int myGraph[5][5]={{0,1,0,1,0},{1,0,1,0,1},{0,1,0,1,1},{1,0,1,0,0},{0,1,1,0,0}};int** tempPtr;printf("Preparing data\r\n");
tempPtr =(int**)malloc(5*sizeof(int*));for(i =0; i <5; i ++){
tempPtr[i]=(int*)malloc(5*sizeof(int));}for(i =0; i <5; i ++){for(j =0; j <5; j ++){
tempPtr[i][j]= myGraph[i][j];}}printf("Data ready\r\n");
GraphPtr tempGraphPtr =initGraph(5, tempPtr);
AdjacencyListPtr tempListPtr =graphToAdjacentList(tempGraphPtr);printAdjacentList(tempListPtr);widthFirstTranverse(tempListPtr,4);}intmain(){testGraphTranverse();return1;}
运行结果
Preparing data
Data ready
This is the graph:1,3,0,2,4,1,3,4,0,2,1,2,
width first
41203--------------------------------
Process exited after 0.02892 seconds with return value 1
请按任意键继续...