/*-----------------------------------
    Breadth_First_Search
-----------------------------------*/
void BFSTraverse(MGraph G)
{
	int i, j;
	LinkQueue Q;

	for(i=0; i<G.numVertexs; i++)
		visited[i] = FALSE;
	InitQueue(&Q);
	for(i=0; i<G.numVertexs; i++)
	{
		if(visited[i] == TRUE)				//顶点是否已经被访问过
			continue;
		visited[i] = TRUE;
		printf("visited %c\n", G.vex[i]);	//访问顶点,或者进行其他操作
		EnQueue(&Q, i);						//该顶点序号入队列

		//当前队列不为空时,一直遍历
		while(QueueLength(Q)>0)
		{
			DeQueue(&Q,&i);					//出队列,得到出来的顶点的序号
			for(j=0; j<G.numVertexs; j++)
			{
				if((G.edge[i][j]!=0 || G.edge[i][j]!=INFINITY) && (visited[j]==FALSE))
				{
					printf("visited %c\n", G.vex[j]);
					visited[j] = TRUE;
					EnQueue(&Q, j);			//与i顶点相连的顶点j入队列
				}
			}
		}
	}

}
int main()
{
	int i;
	QElemType d;
	LinkQueue q;
	i=InitQueue(&q);
	if(i)
		printf("成功地构造了一个空队列!\n");
	printf("是否空队列?%d(1:空 0:否)  ",QueueEmpty(q));
	printf("队列的长度为%d\n",QueueLength(q));
	EnQueue(&q,-5);
	EnQueue(&q,5);
	EnQueue(&q,10);
	printf("插入3个元素(-5,5,10)后,队列的长度为%d\n",QueueLength(q));
	printf("是否空队列?%d(1:空 0:否)  ",QueueEmpty(q));
	printf("队列的元素依次为:");
	QueueTraverse(q);
	i=GetHead(q,&d);
	if(i==OK)
	 printf("队头元素是:%d\n",d);
	DeQueue(&q,&d);
	printf("删除了队头元素%d\n",d);
	i=GetHead(q,&d);
	if(i==OK)
		printf("新的队头元素是:%d\n",d);
	ClearQueue(&q);
	printf("清空队列后,q.front=%u q.rear=%u q.front->next=%u\n",q.front,q.rear,q.front->next);
	DestroyQueue(&q);
	printf("销毁队列后,q.front=%u q.rear=%u\n",q.front, q.rear);
	
	return 0;
}
Beispiel #3
0
void LevelOrderTraversal(struct TNode* root,int n){

if(!root){
return;
}

QNode* queue=CreateQueue(n);
EnQueue(queue,root);

struct TNode* tmpNode;

while(!IsEmptyQueue(queue)){

tmpNode=DeQueue(queue);
if(tmpNode){
printf("%d   ",tmpNode->data);
}
if(tmpNode->left){
EnQueue(queue,tmpNode->left);
}
if(tmpNode->right){
EnQueue(queue,tmpNode->right);
}

}




}
TElemType Parent(CSTree T, TElemType cur_e)
{
	CSTree p, t;
	LinkQueue q;

	InitQueue(q);
	if (T) {
		if (Value(T) == cur_e)
			return Nil;
		EnQueue(q, T);
		while (!QueueEmpty(q)) {
			DeQueue(q, p);
			if (p->firstchild) {
				if (p->firstchild->data == cur_e)
					return Value(p);
				t = p;
				p = p->firstchild;
				EnQueue(q, p);
				while (p->nextsibling) {
					p = p->nextsibling;
					if (Value(p) == cur_e)
						return Value(t);
					EnQueue(q, p);
				}
			}
		}
	}
	return Nil;
}
/* 邻接矩阵的广度遍历算法 */
void BFSTraverse(MGraph G)
{
	int i, j;
	Queue Q;
	for(i = 0; i < G.numVertexes; i++)
       	visited[i] = FALSE;
    InitQueue(&Q);		/* 初始化一辅助用的队列 */
    for(i = 0; i < G.numVertexes; i++)  /* 对每一个顶点做循环 */
    {
		if (!visited[i])	/* 若是未访问过就处理 */
		{
			visited[i]=TRUE;		/* 设置当前顶点访问过 */
			printf("%c ", G.vexs[i]);/* 打印顶点,也可以其它操作 */
			EnQueue(&Q,i);		/* 将此顶点入队列 */
			while(!QueueEmpty(Q))	/* 若当前队列不为空 */
			{
				DeQueue(&Q,&i);	/* 将队对元素出队列,赋值给i */
				for(j=0;j<G.numVertexes;j++) 
				{ 
					/* 判断其它顶点若与当前顶点存在边且未访问过  */
					if(G.arc[i][j] == 1 && !visited[j]) 
					{ 
 						visited[j]=TRUE;			/* 将找到的此顶点标记为已访问 */
						printf("%c ", G.vexs[j]);	/* 打印顶点 */
						EnQueue(&Q,j);				/* 将找到的此顶点入队列  */
					} 
				} 
			}
		}
	}
}
Beispiel #6
0
int main()
{
	LinkQueue Q;
	if(InitQueue(&Q))
	{
		QElemType e;

		printf("initialize successful");
		if(IsEmpty(Q))
		{
			printf("queue is IsEmpty\n");
		}

		for (int i=0;i<10;i++)
		{
			EnQueue(&Q,i);
		}

		GetHead(Q,&e);
		printf("The head element is %d\n",e );
		printf("The length of the queue is %d\n",GetLength(Q));

		DeQueue(&Q,&e);

		printf("delete element is %d\n",e);

		TraverseQueue(Q,*visit);
		if (DestroyQueue(&Q))
		{
			printf("DestroyQueue successful\n");
		}
	}
	return 0;
}
/**
Insert an unlocked page into the last position of the locked queue, may squeeze the original last page into
the unlocked queue.
This function is used on last visited but 'unlocked' pages to avoid excessive lock/unlock calls to cache memory
manager as contiguous entry reading/writing often happens on the same page.
@param  aPage   the page to be inserted.
@pre    the page type of aPage should only be TDynamicDirCachePage::EUnknown
*/
void CDynamicDirCache::MakePageLastLocked(TDynamicDirCachePage* aPage)
    {
    // this function should not be called on active pages
    ASSERT(aPage->iType == TDynamicDirCachePage::EUnknown);

    if (iLockedQ.IsEmpty())
        {
        // if locked queue is empty, add it onto the locked queue directly
        AddFirstOntoQueue(aPage, TDynamicDirCachePage::ELocked);
        }
    else
        {
        // otherwise, we squeeze for the last position on locked queue
        while (iLockedQCount + 1 >= iMinSizeInPages)
            {
            TDynamicDirCachePage* last = iLockedQ.Last();
            DeQueue(last);
            UnlockPage(last);
            AddFirstOntoQueue(last, TDynamicDirCachePage::EUnlocked);
            }

        // iLockedQCount + 1 < iMinSizeInPages
        iLockedQ.AddLast(*aPage);
        aPage->SetPageType(TDynamicDirCachePage::ELocked);
        iLockedQCount++;
        }
    }
Beispiel #8
0
Status BFSTraverse(ALGrapth G, Status(visit)(VertexType), int num = 0){
	int i;
	visitfunc = visit;
	for(i = 0; i < G.vexnum; i++){
		visited[i] = FALSE;
	}
	LinkQueue Q;
	InitQueue(&Q);
	int j;
	for(i = num, j = 0; j < G.vexnum; i++, j++){
		if(i == G.vexnum) i = 0;
		if(!visited[i]){
			visited[i] = TRUE;
			visitfunc(G.vertices[i].data);
			EnQueue(&Q, i);
			int u;
			while(!isEmpty(Q)){
				DeQueue(&Q, &u);
				int w;
				for(w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w)){
					// printf("w: %d\n", w);
					visited[w] = TRUE;
					visitfunc(G.vertices[w].data);
					EnQueue(&Q, w);
					// printf("step\n");
				}
				// printf("stop\n");
			}
		}
		// printf("end\n");
	}
}
int main()
{
	LinkQueue lq;
	InitQueue(&lq);
	int input, count = 5;
	while(count)
	{
		scanf("%d", &input);
		EnQueue(&lq, input);
		count --;
	}
	count = 7;
	int deQueueOut;
	while(count)
	{
		if(DeQueue(&lq,&deQueueOut)){
			printf("I'm empty!\n");
			break;
		}else{
			printf("%d\n", deQueueOut);
		}
	}

	return 0;
}
Beispiel #10
0
void *pthr_deal_cmd()
{
    while(p_n_over)
    {
        DEBUG_printf("next command!\n");
        if(1==SW)
        {
            DEBUG_printf("Web  Switch is OFF!\n");
            exit(1);
        }
        if(DeQueue(&Q_cmd,&deal_package_cmd))
        {
            DEBUG_printf("get a command head:%s!\n",deal_package_cmd.pkg_head);
            DEBUG_printf("get a command:%s!\n",deal_package_cmd.pkg_cmd);

            QueueTraverse(Q_cmd,visit);
            if(!deal_cmd(deal_package_cmd))
                continue;
            DEBUG_printf("after deal_cmd()\n");
            DEBUG_printf("get a command nonce:%s!\n",deal_package_cmd.pkg_nonce);

            NO_data=0;//data recived
            Insert(&deal_package_cmd,L_cmd,L_cmd);

            printf("new node:%s,%s==========\n",deal_package_cmd.pkg_cmd,deal_package_cmd.pkg_nonce);
            clear_recv(deal_package_cmd);
        }
        else
        {
            DEBUG_printf("queue empty1\n");
            sleep(1);
        }
    }
}
Beispiel #11
0
/*宽度优先*/ 
void BFS ( LGraph Graph, Vertex S)
{   
    Queue Q;     
    Vertex V;
    PtrToAdjVNode W; 
  
    InitializeQueue(&Q);
    /* 访问顶点S:此处可根据具体访问需要改写 */
    printf("% d",S);
    Printed[S] = true; /*本例中表示该节点已被输出*/
    Visited[S] = true; /* 标记S已访问 */
    EnQueue(S,&Q); /* S入队列 */
      
    while ( !QueueIsEmpty(&Q) ) {
        DeQueue(&V,&Q);  /* 弹出V */
        for( W=Graph->G[V].FirstEdge; W; W=W->Next ) /* 对图中的每个顶点W */
            if ( !Visited[W->AdjV] ) {
                /* 访问顶点W */
                printf("% d",W->AdjV);
                Printed[W->AdjV] = true; 
                Visited[W->AdjV] = true; 
                EnQueue(W,&Q); /* W入队列 */
            }
    }
}
Beispiel #12
0
/**
 * 习题4.49,非递归法判定给定二叉树是否为完全二叉树
 */
bool isCompleteBiTree(BiTree T)
{
	LinkQueue Q;
	InitQueue(Q);
	BiTree p;
	int flag = 1;	//是否遍历到达最后一层的标志,到达最后一层则flag置0
	p = T;
	while (p || !QueueEmpty(Q)) {
		if (p) {
			if (p->lchild && p->rchild && 1 == flag) {	//左右孩子都存在
				EnQueue(Q, p->lchild);
				EnQueue(Q, p->rchild);
			} else if (p->lchild && !p->rchild && 1 == flag) {	//左孩子存在,右孩子不存在,到达最后一层
				flag = 0;
				EnQueue(Q, p->lchild);
			} else if (!p->lchild && p->rchild)	//左孩子不存在,右孩子存在,结束
				return false;
			else if (!p->lchild && !p->rchild)	//左右孩子都不存在
				flag = 0;
			else
				return false;

			if (!QueueEmpty(Q))
				DeQueue(Q, p);
			else
				return true;
		}
	}
	return true;
}
Beispiel #13
0
Status BFSTraverse(MGraph G,Status(*Visit)(int v))
{
    Queue q;
    QElemType e;
    int i,j;

    for(i=0; i<G.vexnum; i++)
        Visited[i]=FALSE;

    if(!InitQueue(q))return ERROR;
    EnQueue(q,G.vexs[0]);//将第一个顶点入队
    Visited[0]=TRUE;

    printf("广度优先遍历:");
    while(!QueueEmpty(q))
    {
        GetHead(q,e);
        i=LocateVex(G,e);
        for(j=0; j<G.vexnum; j++)
            if(G.arcs[i][j].adj!=0)
                if(!Visited[j])
                {
                    Visited[j]=TRUE;
                    EnQueue(q,G.vexs[j]);
                }
        DeQueue(q,e);
        Visit(e);
    }
    printf("\n");
    return OK;
}
Beispiel #14
0
/* 
** MapQueueMove
** 
** Applies a function to each element in a queue.  If the function returns
** moveVal, it enqueues the element to q2, otherwise it requeues it to 
** the original queue.
*/
int MapQueueMove(QUEUE q, int (*pFunc)(void *, void**), void **argv, 
		 int moveVal, QUEUE q2) 
{
void *item;
int count = QueueCount(q);
int status = SUCCESS;

TypeCheck(q,TYPE_QUEUE);
TypeCheck(q2,TYPE_QUEUE);
assert(! IS_ERROR(moveVal));
while (count--)
   {
   item = DeQueue(q);
   if (NULL == item)
      return(-1);
   status = (*pFunc)(item,argv);
   if (IS_ERROR(status))
      return(status);
   if (status == moveVal)
      status = EnQueue(q2,item);
   else
      status = EnQueue(q,item);
   if (IS_ERROR(status))
      return(status);
   }
return(status);
}
Beispiel #15
0
 void LevelOrderTraverse(CSTree T,void(*Visit)(TElemType))
 { /* 层序遍历孩子-兄弟二叉链表结构的树T */
   CSTree p;
   LinkQueue q;
   InitQueue(&q);
   if(T)
   {
     Visit(Value(T)); /* 先访问根结点 */
     EnQueue(&q,T); /* 入队根结点的指针 */
     while(!QueueEmpty(q)) /* 队不空 */
     {
       DeQueue(&q,&p); /* 出队一个结点的指针 */
       if(p->firstchild) /* 有长子 */
       {
         p=p->firstchild;
         Visit(Value(p)); /* 访问长子结点 */
         EnQueue(&q,p); /* 入队长子结点的指针 */
         while(p->nextsibling) /* 有下一个兄弟 */
         {
           p=p->nextsibling;
           Visit(Value(p)); /* 访问下一个兄弟 */
           EnQueue(&q,p); /* 入队兄弟结点的指针 */
         }
       }
     }
   }
 }
Beispiel #16
0
void BFSTraverse(MGraph G, Status(*Visit)(VertexType))
{/* 初始条件: 图G存在,Visit是顶点的应用函数。算法7.6 */
   /* 操作结果: 从第1个顶点起,按广度优先非递归遍历图G,并对每个顶点调用函数 */
   /*           Visit一次且仅一次。一旦Visit()失败,则操作失败。 */
   /*           使用辅助队列Q和访问标志数组visited */
	int u, v, w;
	VertexType w1, u1;
	SqQueue Q;
	for (v = 0; v < G.vexnum; v++)
		visited[v] = FALSE; //访问标志数组初始化(未被访问)
	InitQueue(Q);
	for (v = 0; v < G.vexnum; v++) {
		if (!visited[v]) {
			visited[v] = TRUE;
			Visit(G.vexs[v]);
			EnQueue(Q, v);
			while (!QueueEmpty(Q)) {
				DeQueue(Q, u); //队头元素出队并置为u
				strcpy(u1, GetVex(G, u));
				for (w = FirstAdjVex(G, u1); w != -1; w = NextAdjVex(G, u1, strcpy(w1, GetVex(G, w))))
					if (!visited[w]) { // w为u的尚未访问的邻接顶点的序号
						visited[w] = TRUE;
						Visit(G.vexs[w]);
						EnQueue(Q, w);
					}
			}
		}
	}
}
Beispiel #17
0
 TElemType Parent(BiTree T,TElemType e)
 { // 初始条件: 二叉树T存在,e是T中某个结点
   // 操作结果: 若e是T的非根结点,则返回它的双亲,否则返回"空"
   LinkQueue q;
   QElemType a;
   if(T) // 非空树
   {
     InitQueue(q); // 初始化队列
     EnQueue(q,T); // 树根入队
     while(!QueueEmpty(q)) // 队不空
     {
       DeQueue(q,a); // 出队,队列元素赋给a
       if(a->lchild&&a->lchild->data==e||a->rchild&&a->rchild->data==e) // 找到e(是其左或右孩子)
         return a->data; // 返回e的双亲的值
       else // 没找到e,则入队其左右孩子指针(如果非空)
       {
         if(a->lchild)
           EnQueue(q,a->lchild);
         if(a->rchild)
           EnQueue(q,a->rchild);
       }
     }
   }
   return Nil; // 树空或没找到e
 }
Beispiel #18
0
void LevelOrderTraverse(BinaryTree * binaryTree,void(*visit)()){
	if(binaryTree){
		
		NodeQueue * queue=NULL;
		queue=InitQueue();

		visit(binaryTree->data);
		if(binaryTree->lChild){
			EnQueue(queue,binaryTree->lChild);
		}

		if(binaryTree->rChild){
			EnQueue(queue,binaryTree->rChild);
		}

		while(IsQueueEmpty(queue)==1){
			Node * e=NULL;
			e=DeQueue(queue);
			visit(e->data);
			if(e->lChild){
				EnQueue(queue,e->lChild);
			}

			if(e->rChild){
				EnQueue(queue,e->rChild);
			}
		}

		DestroyQueue(queue);		
	}
}
Beispiel #19
0
int main() {
	SeQueue Q;
	int data = 3, value;

	//0. Init
	InitQueue(Q);
	PrintQueue(Q);

	//1. Enter Queue
	printf("\n");
	PrintQueue(Q);
	printf("EnQueue = %d\n", data);
	EnQueue(Q, data);
	PrintQueue(Q);

	//2. DeQueue
	printf("\n");
	PrintQueue(Q);
	value = Front(Q);
	DeQueue(Q);
	printf("DeQueue value = %d\n", value);
	PrintQueue(Q);

	//3. Clear
	printf("\n");
	PrintQueue(Q);
	printf("Clear Queue\n");
	ClearQueue(Q);
	PrintQueue(Q);

	return 0;
}
Beispiel #20
0
int main(){
printf("\nEnter the value of n : ");
int n;
scanf("%d",&n);
struct QUNode* queue=(struct QUNode*)malloc(sizeof(QUNode));
queue->front=-1;
queue->rear=-1;
queue->capacity=n;
queue->Array=(char**)malloc(sizeof(char*)*n);
int i;
for(i=0;i<n;i++){
queue->Array[i]=(char*)malloc(sizeof(char)*10);
}
EnQueue(queue,"1");
char* S1=(char*)malloc(sizeof(char)*10);
char* S2=(char*)malloc(sizeof(char)*10);;
char tmpZero[2]="0";
char tmpOne[2]="1";
while(n--){
S1=DeQueue(queue);
printf("\n%s",S1);
strcpy(S2,S1);
printf("\n\nS1 : %s  S2: %s",S1,S2);
strcat(S1,S2);

break;
}

return 0;
}
Beispiel #21
0
void bfs(Edge **C, int nNodes, int sink, int *sequenceList)
{
    int *colour;
    Item u, v, *p;
    Item i = 0; 
    int k = 0;
    p = (Item *)malloc(sizeof(Item));
    colour = (int *)calloc(nNodes, sizeof(int));
    colour[sink] = GRAY;
    Queue *queue = InitQueue();
    EnQueue(queue, sink);
    do {
        while (!IsEmpty(queue)) {
            DeQueue(queue, p);
            sequenceList[k++] = v = *p;
            for (u = 0; u < nNodes; u++) {
                if (colour[u] == WHITE && C[u][v].eContent > 0) {
                    EnQueue(queue, u);
                    colour[u] = GRAY;
                }
            }
            colour[v] = BLACK;
        }
        if (colour[i] == WHITE) {
            EnQueue(queue, i);
            colour[i] = GRAY;
        }
    } while(i++ < nNodes);

    free(p);
    free(colour);
    DestroyQueue(queue);
}
void BFSTraverse(GraphAdjList GL)
{
    int i;
    EdgeNode *p;
    Queue Q;
    for(i=0; i<GL->numVertexes; i++)
        visited[i]=FLASE;
    InitQueue(&Q);
    for(i=0; i<GL->numVertexes; i++)
    {
        if(!visited[i])
        {
            visited[i]=TRUE;
            printf("%c ",GL->adjList[i].data);
            EnQueue(&Q,i);
            while(!QueueEmpty(Q))
            {
                DeQueue(&Q,&i);
                p=GL->adjList[i].firstedge; //找到当前顶点边表链表头指针
                while(p)
                {
                    if(!visited[p->adjvex]) //若此顶点未被访问
                    {
                        visited[p->adjvex]=TRUE;
                        printf("%c ",GL->adjList[p->adjvex].data);
                        EnQueue(&Q,p->adjvex);
                    }
                    p=p->next;
                }
            }
        }
    }
}
void LevelOrderTraverse(CSTree T, void (*Visit)(TElemType))
{
	CSTree p;
	LinkQueue q;

	InitQueue(q);
	if (T) {
		Visit(Value(T));
		EnQueue(q, T);
		while (!QueueEmpty(q)) {
			DeQueue(q, p);
			if (p->firstchild) {
				p = p->firstchild;
				Visit(Value(p));
				EnQueue(q, p);
				while (p->nextsibling) {
					p = p->nextsibling;
					Visit(Value(p));
					EnQueue(q, p);
				}
			}
		}
	}
	printf("\n");
}
void BFSTraverse(MGraph G)
{
    int i,j;
    Queue Q;
    for(i=0; i<G.numVertexes; i++)
        visited[i]=FALSE;
    InitQueue(&Q);
    for(i=0; i<G.numVertexes; ++i)
    {
        if(!visited[i])
        {
            visited[i]=TRUE;
            printf("%c ",G.vexs[i]);
            EnQueue(&Q,i);
            while(!QueueEmpty(Q))
            {
                DeQueue(&Q,&i);
                for(j=0; j<G.numVertexes; j++)
                {
                    if(G.arc[i][j]==1 && !visited[j])
                    {
                        visited[j]=TRUE;
                        printf("%c ",G.vexs[j]);
                        EnQueue(&Q,j);
                    }
                }
            }
        }
    }
}
Beispiel #25
0
/**
 * @function BuildManifold
 */
void HP2D::BuildManifold( Vertex* _v0 ) {
	
	Vertex* va;
    std::vector<Vertex*> S;
	std::vector<Vertex*> B;

	printf("Build Manifold \n");
	AddVertex( _v0 );	
	EnQueue( _v0 );

	while( mQ.size() != 0 ) {
		va = DeQueue();
		S = Successors( va );

		for( unsigned int i = 0; i < S.size(); ++i ) {
			AddVertex( S[i] );
			AddEdge( va, S[i] );
			if( S[i]->GetDist() < DIST_MAX ) {		
				EnQueue( S[i] );
			}
			B = GetAdjacent2( va );
			for( unsigned j = 0; j < B.size(); ++j ) {
				if( CheckPosNeighbors( B[j], S[i] ) == true ) {
					AddEdge( S[i], B[j] );
				}
			}
		}		
	}
}
Beispiel #26
0
int main()
{
	LinkQueue Q;
	QElemType e = 5;

	InitQueue(&Q);

	EnQueue(&Q,e);
	TranverseQueue(Q,print);
	
	printf("\n------------\n");
	
	e = 6;
	EnQueue(&Q,e);
	TranverseQueue(Q,print);
	
	printf("\n------------\n");
	
	e = 7;
	EnQueue(&Q,e);
	TranverseQueue(Q,print);
	
	printf("\n------------\n");
	DeQueue(&Q,&e);
	TranverseQueue(Q,print);
	
	DestroyQueue(&Q);	
	
	return 0;
}
Beispiel #27
0
void BFSTraverse(AMLGraph G,Status(*Visit)(VertexType))
{ /* 初始条件: 图G存在,Visit是顶点的应用函数。*/
  /* 操作结果: 从第1个顶点起,按广度优先非递归遍历图G,并对每个顶点调用函数 */
  /*           Visit一次且仅一次。一旦Visit()失败,则操作失败。 */
  /*           使用辅助队列Q和访问标志数组visite */
  int v,u,w;
  VertexType w1,u1;
  LinkQueue Q;
  for(v=0;v<G.vexnum;v++)
    visite[v]=FALSE; /* 置初值 */
  InitQueue(&Q); /* 置空的辅助队列Q */
  for(v=0;v<G.vexnum;v++)
    if(!visite[v]) /* v尚未访问 */
    {
      visite[v]=TRUE; /* 设置访问标志为TRUE(已访问) */
      Visit(G.adjmulist[v].data);
      EnQueue(&Q,v); /* v入队列 */
      while(!QueueEmpty(Q)) /* 队列不空 */
      {
        DeQueue(&Q,&u); /* 队头元素出队并置为u */
        strcpy(u1,*GetVex(G,u));
        for(w=FirstAdjVex(G,u1);w>=0;w=NextAdjVex(G,u1,strcpy(w1,*GetVex(G,w))))
          if(!visite[w]) /* w为u的尚未访问的邻接顶点的序号 */
          {
            visite[w]=TRUE;
            Visit(G.adjmulist[w].data);
            EnQueue(&Q,w);
          }
      }
    }
  printf("\n");
}
Beispiel #28
0
 TElemType Parent(CSTree T,TElemType cur_e)
 { /* 初始条件: 树T存在,cur_e是T中某个结点 */
   /* 操作结果: 若cur_e是T的非根结点,则返回它的双亲,否则函数值为"空" */
   CSTree p,t;
   LinkQueue q;
   InitQueue(&q);
   if(T) /* 树非空 */
   {
     if(Value(T)==cur_e) /* 根结点值为cur_e */
       return Nil;
     EnQueue(&q,T); /* 根结点入队 */
     while(!QueueEmpty(q))
     {
       DeQueue(&q,&p);
       if(p->firstchild) /* p有长子 */
       {
         if(p->firstchild->data==cur_e) /* 长子为cur_e */
           return Value(p); /* 返回双亲 */
         t=p; /* 双亲指针赋给t */
         p=p->firstchild; /* p指向长子 */
         EnQueue(&q,p); /* 入队长子 */
         while(p->nextsibling) /* 有下一个兄弟 */
         {
           p=p->nextsibling; /* p指向下一个兄弟 */
	   if(Value(p)==cur_e) /* 下一个兄弟为cur_e */
	     return Value(t); /* 返回双亲 */
	   EnQueue(&q,p); /* 入队下一个兄弟 */
	 }
       }
     }
   }
   return Nil; /* 树空或没找到cur_e */
 }
Beispiel #29
0
static void* pthreadWriteSerial (void* arg)
{
	PQueueNode pnode = {0};
	while (1)
	{
		pnode = GetFront(g_WriteQueue);
		if (pnode)
		{
			if (g_hSerialDevFd > 0)
			{
				TRACE("write serial####%d\n", pnode->nlen);
				if (g_PrintSirial)
				{
					PrintDataBy16((unsigned char *)pnode->data, pnode->nlen);
				}
				write(g_hSerialDevFd, pnode->data, pnode->nlen);
                //ar9331 /dev/ttyATH0不能使用该函数,否则数据发送不完整
				//tcflush(g_hSerialDevFd, TCOFLUSH);
				/*出队列,入队列,不能同时执行*/
				pthread_mutex_lock(&g_hRdWrMutex);
				DeQueue(g_WriteQueue);
				pthread_mutex_unlock(&g_hRdWrMutex);
				TRACE("DeQueue !!!:%d\n", GetSize(g_WriteQueue));
			}
		}
		/*30ms内不允许再次write,cc2530处理能力有限*/
		usleep(50000);
	}
	return NULL;
}
Beispiel #30
0
void BFSTraverse(ALGraph G,void(*Visit)(char*))
{/*按广度优先非递归遍历图G。使用辅助队列Q和访问标志数组visited。*/
  int v,u,w;
  VertexType u1,w1;
  LinkQueue Q;
  for(v=0;v<G.vexnum;++v)
    visited[v]=FALSE; /* 置初值 */
  InitQueue(&Q); /* 置空的辅助队列Q */
  for(v=0;v<G.vexnum;v++) /* 如果是连通图,只v=0就遍历全图 */
    if(!visited[v]) /* v尚未访问 */
    {
      visited[v]=TRUE;
      Visit(G.vertices[v].data);
      EnQueue(&Q,v); /* v入队列 */
      while(!QueueEmpty(Q)) /* 队列不空 */
      {
        DeQueue(&Q,&u); /* 队头元素出队并置为u */
        strcpy(u1,*GetVex(G,u));
        for(w=FirstAdjVex(G,u1);w>=0;w=NextAdjVex(G,u1,strcpy(w1,*GetVex(G,w))))
          if(!visited[w]) /* w为u的尚未访问的邻接顶点 */
          {
            visited[w]=TRUE;
            Visit(G.vertices[w].data);
            EnQueue(&Q,w); /* w入队 */
          }
      }
    }
  printf("\n");
}