今天看了下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;
}