//循环队列是一种高效的数据结构,适用于缓冲区和数据流应用,例如串口通信接收缓冲typedefstruct{
int buffer[SIZE];int head;int tail;int count;} CircularBuffer;voidpush(CircularBuffer *cb,int data){
if(cb->count < SIZE){
cb->buffer[cb->head]= data;
cb->head =(cb->head +1)% SIZE;
cb->count++;}}intpop(CircularBuffer *cb){
if(cb->count >0){
int data = cb->buffer[cb->tail];
cb->tail =(cb->tail +1)% SIZE;
cb->count--;return data;}return-1;// Buffer is empty}
断言(Assertion)
//断言用于在程序中检查特定条件是否满足,如果条件为假,会触发断言失败,并输出相关信息#defineassert(expression)((void)0)#ifndefNDEBUG#undefassert#defineassert(expression)((expression)?(void)0:assert_failed(__FILE__,__LINE__))#endifvoidassert_failed(constchar*file,int line){
printf("Assertion failed at %s:%d\n", file, line);// Additional error handling or logging can be added here}
//用于在已排序的数组中执行二进制查找的函数intbinary_search(int arr[],int size,int target){
int left =0, right = size -1;while(left <= right){
int mid = left +(right - left)/2;if(arr[mid]== target){
return mid;}elseif