본문 바로가기
알고 있으면 유용한 것들

c언어 - 구조체를 포함하는 간단한 원형큐 소스코드

by 긍정왕수전노 2021. 9. 24.
반응형

/* 연습용 코드 */
#define MAX_QUEUE_SIZE       100
struct{
        uint16_t head;
        uint16_t tail;
        int8_t storedQueueDataCount;
        uint32_t queue[MAX_QUEUE_SIZE];
} myCircularQueue;
 
 
 
void putQueue(uint32_t data)
{
        if((myCircularQueue.tail + 1) % MAX_QUEUE_SIZE == myCircularQueue.head)
               printf("Queue is full");
        else
        {
               myCircularQueue.queue[myCircularQueue.tail++ % MAX_QUEUE_SIZE] = data;
               myCircularQueue.storedQueueDataCount++;
        }
}
 
void getQueue(uint32_t *data)
{
        if(myCircularQueue.head == myCircularQueue.tail)
               printf("Queue is empty");
        else
        {
               *data = myCircularQueue.queue[myCircularQueue.head++ % MAX_QUEUE_SIZE];
               myCircularQueue.storedQueueDataCount--;
        }
}
 
void QueueTest()
{
        uint32_t testArray[30] = {0, };
        uint32_t getArray[30] = {0, };
        uint32_t tempSize;
        tempSize = sizeof(myCircularQueue);
        memset(&myCircularQueue, 0, tempSize);
 
        for(int i = 0; i < sizeof(testArray) / sizeof(uint32_t); i++)
        {
               testArray[i] = i*i;
               putQueue(testArray[i]);
        }
 
        for(int i = 0; i < sizeof(getArray) / sizeof(uint32_t); i++)
        {
               getQueue(&getArray[i]);
        }
}
/* 연습용코드 끝 */

반응형