반응형
SMALL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int initQueue(int qSize, unsigned char* Queue); int getQueueLeftoverSize(void); int isQueueEmpty(void); int isQueueFull(int queueSize); void enqueue(unsigned char* buf, int len); int dequeue(unsigned char* buf, int len); int front; int rear; int queueSize; unsigned char* Q; | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | int initQueue(int qSize, unsigned char* Queue) { int ret = 1; if (Queue != NULL) // 등록하고자 하는 큐의 주소가 NULL이 아닌경우 초기화 수행 { Q = Queue; queueSize = qSize; front = 0; rear = 0; ret = 1; // 초기화 정상 처리 } else { ret = -1; // 초기화 실패 } return ret; } // 남아있는 데이터의 크기 int getQueueLeftoverSize(void) { int ret = 0; if (rear > front) ret = rear - front; else ret = queueSize - (front - rear); return ret; } // is queue empty int isQueueEmpty(void) { int ret = 0; if (front == rear) {//비어있기 때문에 // printf("queue is empty\n"); ret = 1; } else { ret = 0; } return ret; } // is queue full int isQueueFull(void) { int ret = 0; if ((rear + 1) % queueSize == front) { ret = 1; } else { ret = 0; } return ret; } // enqueue // buf(src) : Queue(Q)에 넣고자 하는 원본 데이터 배열 // len : Queue(Q)에 넣고자 하는 데이터 데이터 요소의 갯수 // return : void //void enqueue(unsigned char* Queue, unsigned char * buf, int len) // , int elementSize) void enqueue(unsigned char* buf, int len) // , int elementSize) { #if 0 printf("recv data : "); for (int j = 0; j < len; j++) { printf("%0.2X ", buf[j]); } printf("\n"); #endif // printf("enqueue data :"); for (int i = 0; i < len; i++) { if (isQueueFull() == 1) { printf("queue is full \n"); return; } else { Q[rear] = buf[i]; // printf("%0.2X ", Q[rear]); rear = rear + 1; rear = rear % queueSize; } } // printf(", front : %d, rear : %d\n", front, rear); } // dequeue // buf(target) : 추출된 데이터를 저장할 데이터 버퍼 // len : 추출하기 위한 데이터 갯수 // return : dequeue 요소 갯수 //int dequeue(unsigned char * Queue, unsigned char* buf, int len) int dequeue(unsigned char* buf, int len) { int ret = 0; // 1. 큐에 남아있는 데이터가 추출하려는 데이터의 크기보다 크다면 정상 수행 if (getQueueLeftoverSize() >= len) { // check if the queue is empty for (int i = 0; i < len; i++) { if (isQueueEmpty() != 1) { buf[i] = Q[front]; front = front + 1; front = front % queueSize; ret = i + 1; } else { ret = -1; } } } else { ret = -1; } return ret; } // 큐에서 데이터를 빼지 않고 front에 어떤 데이터가 있는지 확인 하는 함수 unsigned char peakFrontData(void) { return Q[front]; } | cs |
반응형
LIST
'혼자 코딩(C언어)' 카테고리의 다른 글
난... 아무것도 모르는 감자... (0) | 2022.09.02 |
---|---|
c언어 윈도우 기반 TCP/IP 서버 다중 접속 예제 - 002 (0) | 2022.08.08 |
c언어 TCP/IP 소켓 프로그램 예제 - 001 (0) | 2022.08.03 |
C언어 계산기 프로그램을 마치며 (0) | 2022.08.02 |
C언어 계산기 프로그램 5안(우선순위 괄호 계산기) (0) | 2022.07.26 |
댓글