Exemple #1
0
/**
 * 广度优先搜索for图
 * @param {Graph}
 * @param {int} 
 */
void BFS2 (Graph G, int pos) {
  int i = 0, temp;

  Queue Q;
  InitQueue(&Q);

  for (i = 0; i < G -> vexnum; i++) {  //清零
    IsRead[i] = 0;
  }

  if(IsRead[pos] == 0) {
    IsRead[pos] = 1;
    printf("遍历顶点:%c\n",G->vertex[pos]);
  }

  EnterQueue(Q, pos);

  // 当队列不为空
  while (!isEmpty(Q)) {
    OutQueue(Q, &temp);
    for(i = 0; i< G->vexnum; i ++) {
      if(G->Arc[temp][i] != INFINITY && IsRead[i] == 0){
        IsRead[i] = 1;
        printf("遍历顶点:%c\n",G->vertex[i]);
        EnterQueue(Q,i);
      }
    }
  }

  free(Q);
}
Exemple #2
0
int LayerOrder(BiTree bt)
{
	LinkQueue Q;
	BiTree p;
	InitQueue(&Q);
	if(bt==NULL)return(FALSE);
	EnterQueue(&Q,bt);
	while(IsEmpty(Q)==0)
	{
		DeleteQueue(&Q,&p);
		printf("%c ",p->data);
		if(p->LChild)EnterQueue(&Q,p->LChild);
		if(p->RChild)EnterQueue(&Q,p->RChild);
	}
	printf("\n");
	return(TRUE);
}
Exemple #3
0
void main(){
	SeqQueue Q;
	char str[]="ABCDEFGH";
	int i,length=8;
	char x;
	InitQueue(&Q);
	for(i=0;i<length;i++){
		EnterQueue(&Q,str[i]);
	}
	DeleteQueue(&Q,&x);
	printf("出队列的元素为:%c\n",x);	//显示输出出队列的元素
	printf("顺序队列中的元素为:");
	if(!QueueEmpty(Q)){
		for(i=Q.front;i<Q.rear;i++)
			printf("%c",Q.queue[i]);
	}
	printf("\n");
	system("pause");
}