//二叉树广度遍历--即层次遍历
//使用队列来实现
int TraverseBiTree(BiTree T)
{
    LinkQueue Q;
    InitQueue(&Q);

    BiTree tmp=T;
    if(tmp==NULL)
    {
        exit(ERROR);
    }
    EnQueue(&Q,tmp);
    while(QisEmpty(Q)!=true)
    {
        DeQueue(&Q,&tmp);
        printf("%c",tmp->data);
        if(tmp->lchild!=NULL)
        {
            EnQueue(&Q,tmp->lchild);
        }
        if(tmp->rchild!=NULL)
        {
            EnQueue(&Q,tmp->rchild);
        }
    }
    return true;
}
Пример #2
0
/**
 * 算法7.6,按广度优先非递归遍历图G,使用辅助队列Q和访问标志数组visited
 */
void BFSTraverse(ALGraph G, Boolean visited[], Status (*Visit)(ALGraph G, int v))
{
	int u, v, w;
	LinkQueue Q;
	InitQueue(Q);
	
	for (v = 0; v < G.vexnum; v++) {
		visited[v] = false;
	}

	for (v = 0; v < G.vexnum; v++) {
		if (!visited[v]) {
			visited[v] = true;
			Visit(G, v);
			EnQueue(Q, v);
			while (!QueueEmpty(Q)) {
				DeQueue(Q, u);		//附着元素出列并置为u
				for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w)) {
					if (!visited[w]) {
						visited[w] = true;
						Visit(G, w);
						EnQueue(Q, w);
					}
				}
			}
		}
	}
}
Пример #3
0
/////////广度优化遍历/////////
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] != INFINITY && !visited[j]) {
            visited[j] = TRUE;
            printf("%c ", G.vexs[j]);
            EnQueue(&Q, j);
          }
        }
      }
    }
  }
}
Пример #4
0
void BFS(AdjGraph G,int i)
{
    int k;
    EdgeNode p;
    QUEUE Q;
    MakeNull(Q);
    EnQueue(k,Q);
    while(!Empty(Q));
    {
        i=DeQueue(Q);
        p=G.verlist[i].firstedge;
        while(p)
        {
            if(!visit[p->adjvex])
            {
                 LineList[++edgenum].head=G.verlist[i].vertex;
                 LineList[++edgenum].tail=G.verlist[p->adjvex].vertex;
                visit[p->adjvex]=1;
                EnQueue(p->adjvex,Q);
            }
            p=p->next;
        }
    }

}
Пример #5
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");
}
Пример #6
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入队列 */
            }
    }
}
int BFSTraversal(GNode* graphRoot,int u,int v){

QNode* queue=CreateQueue(graphRoot->count);

EnQueue(queue,u);

int data;
ALNode* tmpNode;
while(!IsEmptyQueue(queue)){

data=DeQueue(queue);
if(visited[data]==0){
//printf("%d  ",data);
tmpNode=graphRoot->GArray[data]->head;
while(tmpNode){
    if(tmpNode->destination==v){
    return 1;
    }
if(visited[tmpNode->destination]==0){
EnQueue(queue,tmpNode->destination);
}
tmpNode=tmpNode->next;
}
visited[data]=1;
}


}

return 0;
}
Пример #8
0
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=%uq.rear=%uq.front->next=%u\n",q.front,q.rear,q.front->next);
    DestroyQueue(&q);
    printf("销毁队列后,q.front=%uq.rear=%u\n",q.front,q.rear);
    
    return 0;
}
Пример #9
0
void *pthr_rev()
{
    while(p_n_over)
    {
        if((Q_cmd.rear+1)%MAXQSIZE!=Q_cmd.front && (Q_respond.rear+1)%MAXQSIZE!=Q_respond.front)
        {
            if(-1==recv_serpkg_struct())
            {
                DEBUG_printf("Bad Package REV!\n");
                sleep(1);
                continue;
            }
            if(package.pkg_cmd[0]!='f')
            {
                DEBUG_printf("this is command\n");
                EnQueue(&Q_cmd,package);
                //sem_post(&q1_sem);
            }
            else
            {
                DEBUG_printf("this is respond \n");
                EnQueue(&Q_respond,package);
                //sem_post(&q2_sem);
            }
        }
        else
        {
            DEBUG_printf("the queue is full\n");
            sleep(5);
        }
    }
}
Пример #10
0
int BFSTraverse(MGraph G,int(*Visit)(char v))
{
	LinkQueue Q;
	int v,w,u;
	for(v=0;v<G.vexnum;v++)
		visited[v]=0;
	InitQueue(Q);
	for(v=0;v<G.vexnum;v++)
	{
		if(!visited[v])
		{
			visited[v]=1;
			Visit(G.dingdian[v]);
			EnQueue(Q,v);
			while(!EmptyQueue(Q))
			{
				DeQueue(Q,u);
				w=FirstAdjVex(G,G.dingdian[u]);
				for(;w>=0;w=NextAdjVex(G,G.dingdian[u],G.dingdian[w]))
					if(!visited[w])
					{
						visited[w]=1;
						Visit(G.dingdian[w]);
						EnQueue(Q,w);
					}
			}
		}
	}
	return OK;
}
Пример #11
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;
}
Пример #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;
}
Пример #13
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);
}

}




}
Пример #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);
}
Пример #15
0
/*-----------------------------------
    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入队列
				}
			}
		}
	}

}
Пример #16
0
int main()
{
  Status j;
  int i=0, l;
  QElemType d;
  SqQueue Q;

  InitQueue(&Q);
  printf("初始化队列后 队列是否为空%d  1:空 0:非空 \n", QueueEmpty(Q));

  printf("请输入整型队列元素(不超过%d个), -1为提前结束标识符 \n", MAXSIZE-1);

  do
  {
    scanf("%d", &d);
    if (d == -1)
      break;
    EnQueue(&Q, d);
    i++;
  }while(i < MAXSIZE-1);

  printf("队列长度为: %d \n", QueueLength(Q));

  printf("队列是否为空%d  1:空 0:非空 \n", QueueEmpty(Q));
  printf("连续%d次从队首删除元素, 从队尾插入元素: \n", MAXSIZE);

  for(l = 1; l <= MAXSIZE; l++)
  {
    DeQueue(&Q, &d);
    printf("删除的元素是: %d 插入的元素是 %d \n", d, l+100);

    d = l + 100;
    EnQueue(&Q, d);
  }

  printf("现在队列中的元素为: \n");
  QueueTraverse(Q);

  printf("共向队尾插入了 %d 个元素 \n", i+MAXSIZE);
  l = QueueLength(Q);
  if (l-2 > 0)
    printf("现在由队头开始删除 %d 个元素\n", l-2);

  while(QueueLength(Q) > 2)
  {
    DeQueue(&Q, &d);
    printf("删除的元素为 %d \n", d);
  }

  j = GetHead(Q, &d);
  if (j)
    printf("现在队首的元素为 %d \n", d);

  ClearQueue(&Q);
  printf("清空队列后队列是否为空%d  1:空 0:非空 \n", QueueEmpty(Q));
  return 0;
}
Пример #17
0
   /* 操作结果: 插入c为T中p结点的第i棵子树 */
   int j,k,l,f=1,n=0; /* 设交换标志f的初值为1,p的孩子数n的初值为0 */
   PTNode t;
   if(!TreeEmpty(*T)) /* T不空 */
   {
     for(j=0;j<(*T).n;j++) /* 在T中找p的序号 */
       if((*T).nodes[j].data==p) /* p的序号为j */
         break;
     l=j+1; /* 如果c是p的第1棵子树,则插在j+1处 */
     if(i>1) /* c不是p的第1棵子树 */
     {
       for(k=j+1;k<(*T).n;k++) /* 从j+1开始找p的前i-1个孩子 */
         if((*T).nodes[k].parent==j) /* 当前结点是p的孩子 */
         {
           n++; /* 孩子数加1 */
           if(n==i-1) /* 找到p的第i-1个孩子,其序号为k1 */
             break;
         }
       l=k+1; /* c插在k+1处 */
     } /* p的序号为j,c插在l处 */
     if(l<(*T).n) /* 插入点l不在最后 */
       for(k=(*T).n-1;k>=l;k--) /* 依次将序号l以后的结点向后移c.n个位置 */
       {
         (*T).nodes[k+c.n]=(*T).nodes[k];
         if((*T).nodes[k].parent>=l)
           (*T).nodes[k+c.n].parent+=c.n;
       }
     for(k=0;k<c.n;k++)
     {
       (*T).nodes[l+k].data=c.nodes[k].data; /* 依次将树c的所有结点插于此处 */
       (*T).nodes[l+k].parent=c.nodes[k].parent+l;
     }
     (*T).nodes[l].parent=j; /* 树c的根结点的双亲为p */
     (*T).n+=c.n; /* 树T的结点数加c.n个 */
     while(f)
     { /* 从插入点之后,将结点仍按层序排列 */
       f=0; /* 交换标志置0 */
       for(j=l;j<(*T).n-1;j++)
         if((*T).nodes[j].parent>(*T).nodes[j+1].parent)
         {/* 如果结点j的双亲排在结点j+1的双亲之后(树没有按层序排列),交换两结点*/
           t=(*T).nodes[j];
           (*T).nodes[j]=(*T).nodes[j+1];
           (*T).nodes[j+1]=t;
           f=1; /* 交换标志置1 */
           for(k=j;k<(*T).n;k++) /* 改变双亲序号 */
             if((*T).nodes[k].parent==j)
               (*T).nodes[k].parent++; /* 双亲序号改为j+1 */
             else if((*T).nodes[k].parent==j+1)
               (*T).nodes[k].parent--; /* 双亲序号改为j */
         }
     }
     return OK;
   }
   else /* 树T不存在 */
     return ERROR;
 }

 Status deleted[MAX_TREE_SIZE+1]; /* 删除标志数组(全局量) */
 void DeleteChild(PTree *T,TElemType p,int i)
 { /* 初始条件: 树T存在,p是T中某个结点,1≤i≤p所指结点的度 */
   /* 操作结果: 删除T中结点p的第i棵子树 */
   int j,k,n=0;
   LinkQueue q;
   QElemType pq,qq;
   for(j=0;j<=(*T).n;j++)
     deleted[j]=0; /* 置初值为0(不删除标记) */
   pq.name='a'; /* 此成员不用 */
   InitQueue(&q); /* 初始化队列 */
   for(j=0;j<(*T).n;j++)
     if((*T).nodes[j].data==p)
       break; /* j为结点p的序号 */
   for(k=j+1;k<(*T).n;k++)
   {
     if((*T).nodes[k].parent==j)
       n++;
     if(n==i)
       break; /* k为p的第i棵子树结点的序号 */
   }
   if(k<(*T).n) /* p的第i棵子树结点存在 */
   {
     n=0;
     pq.num=k;
     deleted[k]=1; /* 置删除标记 */
     n++;
     EnQueue(&q,pq);
     while(!QueueEmpty(q))
     {
       DeQueue(&q,&qq);
       for(j=qq.num+1;j<(*T).n;j++)
         if((*T).nodes[j].parent==qq.num)
         {
           pq.num=j;
           deleted[j]=1; /* 置删除标记 */
           n++;
           EnQueue(&q,pq);
         }
     }
     for(j=0;j<(*T).n;j++)
       if(deleted[j]==1)
       {
         for(k=j+1;k<=(*T).n;k++)
         {
           deleted[k-1]=deleted[k];
           (*T).nodes[k-1]=(*T).nodes[k];
           if((*T).nodes[k].parent>j)
             (*T).nodes[k-1].parent--;
         }
         j--;
       }
     (*T).n-=n; /* n为待删除结点数 */
   }
 }
int main()
{
	Status j;
	int i=0,l;
	QElemType d;
	SqQueue Q;
	InitQueue(&Q);
	printf("初始化队列后,队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));

	printf("请输入整型队列元素(不超过%d个),-1为提前结束符: ",MAXSIZE-1);
	do
	{
		/* scanf("%d",&d); */
		d=i+100;
		if(d==-1)
			break;
		i++;
		EnQueue(&Q,d);
	}while(i<MAXSIZE-1);

	printf("队列长度为: %d\n",QueueLength(Q));
	printf("现在队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
	printf("连续%d次由队头删除元素,队尾插入元素:\n",MAXSIZE);
	for(l=1;l<=MAXSIZE;l++)
	{
		DeQueue(&Q,&d);
		printf("删除的元素是%d,插入的元素:%d \n",d,l+1000);
		/* scanf("%d",&d); */
		d=l+1000;
		EnQueue(&Q,d);
	}
	l=QueueLength(Q);

	printf("现在队列中的元素为: \n");
	QueueTraverse(Q);
	printf("共向队尾插入了%d个元素\n",i+MAXSIZE);
	if(l-2>0)
		printf("现在由队头删除%d个元素:\n",l-2);
	while(QueueLength(Q)>2)
	{
		DeQueue(&Q,&d);
		printf("删除的元素值为%d\n",d);
	}

	j=GetHead(Q,&d);
	if(j)
		printf("现在队头元素为: %d\n",d);
	ClearQueue(&Q);
	printf("清空队列后, 队列空否?%u(1:空 0:否)\n",QueueEmpty(Q));
	return 0;
}
Пример #19
0
void GListPrintLOrder(GList A){
	//按层输出广义表A中的所有元素
	InitQueue(Q);
	for(p=L;p;p=p->ptr.tp)
		EnQueue(Q,p);
	while(!QueueEmpty(Q)){
		DeQueue(Q,r);
		if(!r->tag)
			cout<<r->atom;
		else
			for(r=r->ptr.hp;r;r=r->ptr.tp)
				EnQueue(Q,r);
	}
}
Пример #20
0
/******************串口1中断服务函数*******************************/
void USART1_IRQHandler(void)
{
	if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
   {
		 EnQueue(&USART1Buffer,USART_ReceiveData(USART1));//新的数据进缓冲区
	 }
}
Пример #21
0
int main()
{
	FILE *ptr;
	ptr=fopen("file.txt","r");
	CheckFileForError(ptr);
	
  CreateQueue();

  char str[80];
  int i, pnum, atime,btime;
 	while(fgets(str,80,ptr)!=NULL)
 	{
    int *point;

    point = FindNuminString(str,0);
    pnum = *point;
    point = FindNuminString(str,*(point+1));
    atime = *point;
    point = FindNuminString(str,*(point+1));
    btime = *point;
   
   EnQueue(pnum,atime,btime);
  }
  int time_quantum;
  printf("\n Enter the time quantum: ");
  scanf("%d",&time_quantum);
  CalculateRR(time_quantum);


	fclose(ptr);
	DeleteQueue();
	return 0;
}
Пример #22
0
		void push(const Ty &t)
		{
			Ty *t_copy = new Ty(t);
			Node* p = new Node(NULL, (void*)t_copy);

			EnQueue(p);
		}
Пример #23
0
int main()
{
	FILE *ptr;
	ptr=fopen("file.txt","r");
	CheckFileForError(ptr);
	
  CreateQueue();

  char str[80];
  int i, pnum, atime,btime;
 	while(fgets(str,80,ptr)!=NULL)
 	{
    int *point;

    point = FindNuminString(str,0);
    pnum = *point;
    point = FindNuminString(str,*(point+1));
    atime = *point;
    point = FindNuminString(str,*(point+1));
    btime = *point;
   
   EnQueue(pnum,atime,btime);
  }
  
  CalculatePSJF();


	fclose(ptr);
	DeleteQueue();
	return 0;
}
Пример #24
0
void* customer(void* args)
{
	int id;
	
	sem_wait(&customer_mutex);
	id = (int )args;
	EnQueue(id);
	srand(id);
	Customer_Task[id] = rand()%3;
	printf("Customer Created %d\n",id);
	sem_post(&customer_mutex);
	
	sem_wait(&sem_max_cap);
	printf("Customer Entered into post office %d\n",id);
	
	sem_post(&worker_start_work);
	
	sem_wait(&customer_finished[id]);
	
	printf("Customer %d finished\n",id);
	
	printf("Customer %d exiting\n",id);
	
	sem_post(&customer_exit[id]);
	sem_post(&sem_max_cap);
	
	pthread_exit(NULL);
}
Пример #25
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;
}
Пример #26
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;
}
Пример #27
0
extern	void
TermEnqueue(
	TermNode	*term,
	SessionData	*data)
{
	EnQueue(term->que,data);
}
Пример #28
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;
}
/*Bianry Search Tree 层次遍历操作*/
void LevelOrder(BinaryTree btree) {
	Queue bQueue;
	if (btree) {
		InitalQueue(bQueue);
		EnQueue(bQueue, btree);
		//不需要再判断btree是否为空
		while (QueueLength(bQueue)!=0) {	
			DeQueue(bQueue, btree);
			printf("%d ", btree->key);
			if (btree->lchild!=NULL) EnQueue(bQueue, btree->lchild);
			if (btree->rchild!=NULL) EnQueue(bQueue, btree->rchild);
		}
		printf("\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;
}