Files
MicrochipFor32/bsp_Device/bsp_Queue.c

94 lines
3.0 KiB
C
Raw Normal View History

/*----------------------------------------------------------------------------------------------------
# THIS FILE IS A PART OF XerolySkinner's PROJECT
#
# THIS PROGRAM IS FREE SOFTWARE
#
# E-mail:ZABBCCCBBAZ@163.com
# QQ:2715099320
#
# Copyright (c) 2022 XerolySkinner
# All rights reserved.
*/
/*----------------------------------------------------------------------------------------------------
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>:bsp_Queue
ʱ<EFBFBD><EFBFBD>:20220806-0226
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա:<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD>
˵<EFBFBD><EFBFBD>:<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FIFO<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
////////////////////////////////////////////////////////////////////////////////////////////////////
// <09><><EFBFBD><EFBFBD>
struct _Queue {
struct _Queue* front;
void* member;
struct _Queue* rear;
};
////////////////////////////////////////////////////////////////////////////////////////////////////
// <09><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct _Queue* bsp_QueueInit(void) {
struct _Queue* list = malloc(sizeof(struct _Queue));
if (list == NULL)return NULL;
list->front = NULL;
list->rear = NULL;
list->member = NULL;
return list;}
////////////////////////////////////////////////////////////////////////////////////////////////////
// <09><><EFBFBD>пռ<D0BF><D5BC><EFBFBD>
char bsp_QueueEmpty(struct _Queue *list) {
return (list->front == NULL && list->rear == NULL);}
////////////////////////////////////////////////////////////////////////////////////////////////////
// <09><><EFBFBD><EFBFBD>β<EFBFBD>ʹ<EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD>Ա
char bsp_QueuePush(struct _Queue *list,void* member) {
struct _Queue* listAdd = malloc(sizeof(struct _Queue));
struct _Queue* listFront = list->front;
struct _Queue* listRear = list->rear;
if (list == NULL)return 1; //<2F><>ֹ<EFBFBD><D6B9>ը
if (listAdd == NULL)return 1; //<2F><>ֹ<EFBFBD><D6B9>ը
listAdd->member = member; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <09>յ<EFBFBD>λ<EFBFBD><CEBB>ʼ<EFBFBD><CABC>
if (bsp_QueueEmpty(list)) {
list->front = list->rear = listAdd;
listAdd->front = listAdd->rear = listAdd;}
// <09><><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
else {
listAdd->front = listRear; //<2F>½ڵ<C2BD><DAB5><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>
listRear->rear = listAdd; //β<>ͽ<EFBFBD><CDBD><EFBFBD><EFBFBD>½ڵ<C2BD>
listAdd->rear = listAdd; //β<><CEB2><EFBFBD>Խ<EFBFBD>
list->rear = listAdd; //<2F><>β<EFBFBD><CEB2>
}
return 0;}
//--------------------------------------------------------------------------------------------------
char bsp_QueuePushCharPtr(struct _Queue* list, char* member, unsigned int len) {
char* enter = malloc(len+1);
if (enter == NULL)return 1;
enter[len] = '\0';
strcpy_s(enter,len+1, member);
bsp_QueuePush(list,enter);}
void bsp_QueuePushCharPtrFree(char* member) {
free(member);}
////////////////////////////////////////////////////////////////////////////////////////////////////
// <09><><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա
void* bsp_QueuePop(struct _Queue* list) {
struct _Queue* listFront = list->front;
struct _Queue* listRear = list->rear;
void* result=NULL;
if (list == NULL)return NULL; //<2F><>ֹ<EFBFBD><D6B9>ը
if (bsp_QueueEmpty(list))return NULL; //<2F>Ѹ<EFBFBD><D1B8><EFBFBD><EFBFBD><EFBFBD>
// <09>Ѹ<EFBFBD><D1B8>ڵ<EFBFBD>
if (listFront== listRear) {
list->front = list->rear = NULL;
result= listFront->member;}
else {
list->front = listFront->rear;
result= listFront->member;}
free(listFront); //Ĩ<><C4A8><EFBFBD><EFBFBD>
return result;}
////////////////////////////////////////////////////////////////////////////////////////////////////