我写的循环队列

 

今天看了下C和指针的 标准函数库那一张。复习了以前知道的一些函数。rand(); srand(); time(),等函数。学习到两个很有用的函数qsort, bsearch函数。这两个函数中也有函数指针的应用。。对于signal函数,还有一些疑惑.

另外,联想到之前自己写的协议栈代码中太多队列了,每一个队列都是自己维护。于是写了一个自己的循环 队列。文件如下 my_queue.h: 以后C语言用循环队列直接用这个

#ifndef MY_QUEUE_H
#define MY_QUEUE_H
typedef int type;
#define QUEUE_SIZE 5
typedef struct
{
 type content[QUEUE_SIZE];
 int in;
 int out;
 int count;
}MY_QUEUE;//我的循环队列

int in_queue(MY_QUEUE *temp,type t);
int out_queue(MY_QUEUE *temp,type *t);
int is_empty(MY_QUEUE *temp);
int is_full(MY_QUEUE *temp);

int in_queue(MY_QUEUE *temp,type t)
{
 if(temp->count==QUEUE_SIZE)
  return 0;
 temp->content[temp->in]=t;
 temp->in=(temp->in+1)%QUEUE_SIZE;
 temp->count++;
 return 1;
}

int out_queue(MY_QUEUE *temp,type *t)
{
 if(temp->count==0)
  return 0;
 *t=temp->content[temp->out];
 temp->out=(temp->out+1)%QUEUE_SIZE;
 temp->count--;
 return 1;
}

int is_empty(MY_QUEUE *temp)
{
 if(temp->count==0)
  return 1;
 else
  return 0;
}

int is_full(MY_QUEUE *temp)
{
 if(temp->count==QUEUE_SIZE)
  return 1;
 else
  return 0;
}

#endif

 

 

另外在main函数中。定义了一个

MY_QUEUE rev_buf;

然后有数据之后,测试了一下qsort()函数

qsort(rev_buf.content,rev_buf.count,sizeof(type),my_compare);

 

int my_compare(void const *a,void const *b)
{
 return *(type*)a>*(type*)b? 1:0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值