队列挂载和卸载操作

最近使用了消息队列,进行发送数据的重发和超时机制处理,其中用到了挂载和卸载的操作。具体编码以及测试代码如下,仅供参考(测试环境为win764位 visual studio 2010):

#include<stdio.h>
#include<stdint.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>

#define MOUNT( __CHAIN , __P )                  mount( (void **)&(__CHAIN) , __P )
#define UNMOUNT( __CHAIN , __P )                unmount( (void **)&(__CHAIN) , __P)


struct data_t{
	uint8_t num;
    /* 可以根据自己的需要填充更多字段 */
};


struct quene_t{
	struct quene_t *next;
	struct data_t data;
};

static struct quene_t* quene = NULL;

void mount(void **__chain , void *__p)
{
	if(NULL == __p){printf("__p is NULL.\r\n");return;}
	if(NULL == *__chain){
		printf("Empty Quene.\r\n ");
		
		*__chain = __p;
		*((void**)__p) = NULL;
		printf("Mount successed.\r\n");
	}
	else
	{
		void * _p = *__chain;
		*__chain = __p;
		*((void**)__p) = _p;
		printf("Mount successed.\r\n");
	}
}

void unmount(void **__chain , void *__p)
{
	void **current = NULL;
	if(NULL == __p || NULL == *__chain){ printf("error --- 01.\r\n");return;}
	current = __chain;
	while(__p != *current && NULL != *current)
	{
		current = (void**)(*current);
	}
	if(NULL == *current)
	{
		printf("__p not in the quene.\r\n");
		return;
	}
	else{
		if(__p != *current){ printf("error --- 02.\r\n"); return;}
		*current = (void**)*(void**)*current;
	}
	printf("unmount successed.\r\n");
}


int main()
{
	
	struct quene_t *p1 = (struct quene_t *)malloc(sizeof(struct quene_t));
	struct quene_t *p2 = (struct quene_t *)malloc(sizeof(struct quene_t));
	struct quene_t *p3 = (struct quene_t *)malloc(sizeof(struct quene_t));
	struct quene_t *p4 = (struct quene_t *)malloc(sizeof(struct quene_t));

	MOUNT(quene,NULL);          //测试挂载空元素
	UNMOUNT(quene,NULL);        //测试卸载空元素

	p1->data.num = 0x01;
	MOUNT(quene,p1);	
	p2->data.num = 0x02;
	MOUNT(quene,p2);
	p3->data.num = 0x03;
	MOUNT(quene,p3);	
	p4->data.num = 0x04;
	MOUNT(quene,p4);

	UNMOUNT(quene,p1);
	free(p1);
	UNMOUNT(quene,p2);
	free(p2);
	UNMOUNT(quene,p4);
	UNMOUNT(quene,p4);
	free(p4);
	UNMOUNT(quene,p3);
	free(p3);
	system("pause");
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值