c语言实现图的广度优先遍历
来源:互联网 发布:王者归来流量软件试用 编辑:程序博客网 时间:2024/04/29 03:05
#include <stdio.h>#include <stdlib.h>//定义图的最多有20个顶点 #define MAX_VERTEX_NUM 20/*******定义图的顶点**************/ struct node{ char data; int flag;//标志位(1:访问过 0:未访问 ) struct arcNode * firstArcNode;}; struct arcNode{ int no; struct arcNode * nextArcNode;};/*******定义队列的结构体**********/ struct qNode{ struct node * Node; struct qNode * nextQNode; };struct queue{ struct qNode * front; struct qNode * rear; };/**********方法声明*************///图 void createArcNode(struct node * Node);void rangeTraverse(struct node arrayList[],int position);//队列 struct queue * init();void inQueue(struct queue * Queue,struct node * Node);struct node * outQueue(struct queue * Queue);int isEmpty(struct queue * Queue);/**********Main函数***********/ int main(int argc, char *argv[]){ struct node arrayList[MAX_VERTEX_NUM]; char ch; int flag = 1;//标志位,0表示输入结束 int position =1; struct arcNode * testNode;//测试用弧点 printf("输入顶点信息(顶点信息用一个字符表示):\n"); printf("***输入0表示结束输入***\n"); while(1){ printf("第%d个顶点的信息为:",position); scanf(" %c",&arrayList[position].data);//在%c之前加空格解决把回车符当做输入字符的问题 if(arrayList[position].data=='0'){ break; } position +=1; } for(flag=1;flag<20;flag++){ if(arrayList[flag].data!='0'){ printf("\n"); createArcNode(&arrayList[flag]); } else{ break; } } //输出邻接表 for(flag=1;flag<20;flag++){ if(arrayList[flag].data!='0'){ printf("\n"); testNode = arrayList[flag].firstArcNode; printf("%d %c :",flag,arrayList[flag].data); while(1){ printf("%d ",testNode->no); if(testNode->nextArcNode==NULL){ break; } testNode = testNode->nextArcNode; } } else{ break; } } printf("\n"); printf("图建立完毕,按1进行广度优先遍历:"); scanf("%d",&position); if(position==1){ while(1){ printf("\n"); printf("输入开始节点在数组中的位置:"); scanf("%d",&position); rangeTraverse(arrayList,position); } } system("PAUSE"); return 0;}/*****创建顶点间连接关系 **参数:顶点结构体指针 **注:参数必须是指针,如果是形参并不能改变原来node中的firstArcNode的值 ***/ void createArcNode(struct node * Node){ int i = 1; int position =0; struct arcNode * arcNode,* temp,* temp2; printf("请输入与顶点%c相连的顶点在数组中的位置:\n",Node->data); printf("***输入0表示结束输入***\n"); while(1){ printf("第%d个与顶点%c相连的顶点在数组中的位置:",i++,Node->data); scanf("%d",&position); temp = (struct arcNode *)malloc(sizeof(struct arcNode)); if(temp==NULL){ printf("内存分配失败!"); } if(position==0){ arcNode->nextArcNode=NULL; break; } if(i==2){ temp->no=position; Node->firstArcNode = temp; arcNode = temp; } else{ temp->no=position; arcNode->nextArcNode = temp; arcNode=temp; } // temp2 = Node.firstArcNode; // printf("%d",temp2->no); } }/*****广度优先遍历 **参数:顶点数组,开始顶点在数组中的位置 ***/void rangeTraverse(struct node arrayList[],int position){ int i; struct queue * Queue=init(); struct node * Node; struct arcNode * arcNode; //把数组中的顶点全部标记为未访问:falg=0 for(i=1;i<20;i++){ if(arrayList[i].data!='0'){ arrayList[i].flag=0; } else{ break; } } inQueue(Queue,&arrayList[position]); arrayList[position].flag=1; while(isEmpty(Queue)){ Node = outQueue(Queue); printf("%c ",Node->data); arcNode = Node->firstArcNode; i=1; while(i){ Node = &arrayList[arcNode->no]; if(Node->flag==0){ Node->flag=1; inQueue(Queue,Node); } if(arcNode->nextArcNode==NULL){ i=0; } arcNode = arcNode->nextArcNode; } } } /*****队列的初始化,入队列,出队列 ***/struct queue * init(){ struct queue * Queue = (struct queue *)malloc(sizeof(struct queue)); struct qNode * QNode = (struct qNode *)malloc(sizeof(struct qNode)); if(QNode == NULL||Queue == NULL){ printf("内存分配失败"); } Queue->front = QNode; Queue->rear = QNode; return Queue;}void inQueue(struct queue * Queue,struct node * Node){ struct qNode * QNode = (struct qNode *)malloc(sizeof(struct qNode)); if(QNode == NULL){ printf("内存分配失败"); } QNode->Node = Node; QNode->nextQNode = NULL;//队尾元素,指针为空 Queue->rear->nextQNode = QNode; Queue->rear = QNode; } struct node * outQueue(struct queue * Queue){ struct node * Node; struct qNode * QNode; if(Queue->front==Queue->rear){ printf("队列已空!"); } QNode = Queue->front->nextQNode; Node = QNode->Node; Queue->front->nextQNode = QNode->nextQNode; //当队列中最后一个元素被删除后,队列尾指针也丢失了,因此需要对队尾指针重新赋值 if(Queue->rear==QNode){ Queue->rear = Queue->front; } return Node; }int isEmpty(struct queue * Queue){ if(Queue->front==Queue->rear){ return 0; } return 1;}