⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queue.c

📁 基于UNIX的中间件编程,只能运行于UNIX操作系统
💻 C
字号:
/***********************************************************	queue.cpp:the queue manage functions				******						zhoushijie						******						2000/3							***********************************************************/#include "queue.h"/*** -------------------------------------------------------** CreateQueue():Create a new queue** -------------------------------------------------------*/void CreateQueue(Queue *q){	q->count=0;		q->front=q->rear=NULL;}/***--------------------------------------------------------** AppendNode():append Node to the end of a queue**--------------------------------------------------------*/void AppendNode(QueueNode *p,Queue *q){	if(p==NULL)	{		Error("attempt to append the null node to the queue");		return ;	}		if(QueueEmpty(q))	{		q->front =q->rear =p;			}	else if (q->count==1)	{		q->rear=p ;		q->front->next =p ;					}	else	{		q->rear->next=p; 		q->rear =p;							}	q->count ++;}/***---------------------------------------------------------**ServerNode():pop a node from a queue**---------------------------------------------------------*/void ServeNode(QueueNode **p,Queue *q){	if(QueueEmpty(q))		Error("attempt to server the empty queue");	else	{		q->count--;		(*p)=q->front ;		if(QueueEmpty(q))			q->front=q->rear=NULL;				else		{			q->front=q->front->next;					}	}}/***-------------------------------------------------------** QueueEmpty():where a queue ois empty**-------------------------------------------------------*/int QueueEmpty(Queue *q){	return  q->count==0;		}/***------------------------------------------------------** ClearQueue():clear the  queue **------------------------------------------------------*/void ClearQueue(Queue *q){	if(QueueEmpty(q))		return;	q->count =0;	q->front =q->rear =NULL;	}/***------------------------------------------------------** DestroyQueue():free the  queue **------------------------------------------------------*/void DestroyQueue(Queue *q){	QueueNode *t;	if(QueueEmpty(q))		return;	t=q->front;		while(q->count)	{		t=q->front;		q->front=t->next ;		free(t);		q->count--;	}}/*-----------------------------------------QueueSize():return the size of a queue-----------------------------------------*/int QueueSize(Queue *q){	return q->count;}/*-------------------------------------QueueFull():where a queueu is full-------------------------------------*/int QueueFull(Queue *q,MAXQUEUE max){	return q->count>=max;}/*------------------------------------------QueueFront():return the pointer of the front of a queue------------------------------------------*/void QueueFront(QueueEntry *x,Queue *q){	if(QueueEmpty(q))		x=NULL;	else		x=&q->front->entry ;}/*------------------------------------------QueueFrontnode():return the node of the front of a queue------------------------------------------*/void QueueFrontNode(QueueNode *x,Queue *q){	if(QueueEmpty(q))		x->next =NULL;	else	{		x->next =q->front->next ;		x->entry =q->front->entry ;	}}/*---------------------------------------------------TraverseQueue():Traverse a queue and take a actionofevery node in a queue---------------------------------------------------*/void TraverseQueue(Queue *q,int(*Visit)(QueueEntry x)){	int count=0;	int i;	QueueNode *p;	if(QueueEmpty(q))		return;	count=q->count;	/************************************************************** 	**	the Queue size will increase dynamic ,so define a const to	**	restore its size	**	2000/3/29	**  for(int i=0;i<q->count;i++)	***************************************************************		***************************************************************	** Loop the queue to send the queuenode.If the visit fanction	** return 1,then some node be delete from queue.avoid resend	** the queuenode,break the loop if visit return 1.Otherwise 	** continue loop the queue	***************************************************************/	p=q->front;		while((count--)>0)	{		if(Visit(p->entry)==1)			break;		else 			p=p->next ;					}}/*-------------------------------------------------------------DeleteQueueNode():delete a queue node from a queue-------------------------------------------------------------*/int  DeleteQueueNode(QueueNode *node,Queue *q,QueueNode **ReturnNode){	int flag;	QueueNode *p;	QueueNode *t;	if(QueueEmpty(q))		return 1;	flag=0;		p=q->front;	t=p;	while(p!=NULL)	{		if(!strcmp(node->entry.MessageQueue.source,p->entry.MessageQueue.source) && \			!strcmp(node->entry.MessageQueue.sn,p->entry.MessageQueue.sn))		   		{			q->count--;			if(QueueEmpty(q))			{				q->front =q->rear =NULL;							}			else if(flag==0)			{				q->front =p->next;							}			else			{				t->next =p->next;							}						*ReturnNode=p;			(*ReturnNode)->next =NULL;						return 0;		}				t=p;		p=p->next ;				flag++;	}return 1;}/*------------------------------------------------------MakeQueueNode():form  a queue node------------------------------------------------------*/QueueNode *MakeQueueNode(QueueEntry x){	QueueNode *p=(QueueNode *)malloc(sizeof(QueueNode));	if(p)	{		p->entry =x;		p->next =NULL;	}	else		Error("no memery to  make the QueueNode");	return p;}/*------------------------------------------------------------AllocateNode():allocate a blank queue node------------------------------------------------------------*/int AllocateNode(Queue *Source,Queue *Destination,MESSAGESTRUCTURE m_struct){	QueueNode *newnode;		if(QueueEmpty(Source))		Error("There is no room for allocate");	else 	{		ServeNode(&newnode,Source);				SetNodeEntryContent(newnode,m_struct);		AppendNode(newnode,Destination);	}	return 0;		}/*-----------------------------------------------------SetNodeEntryContent():set the content of a queue node-----------------------------------------------------*/void SetNodeEntryContent(QueueNode *node,MESSAGESTRUCTURE m_struct){	node->entry.flag =1;	node->entry.MessageQueue=m_struct;}/*---------------------------------------------------------CopyQueue():copy the content of a queu to another queue---------------------------------------------------------*/Queue *CopyQueue(Queue *destination,Queue *source){	if (QueueEmpty(source))		return destination;	destination->count=source->count ;	destination->front =source->front ;	destination->rear =source->rear ;	destination->rear->next= NULL;	return destination;}int  Error(char *message){	vfprintf(stdout,"error:%s",message);	return 1;}void Warning(char *message){	vfprintf(stdout,"error:%s",message);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -