[c] Queue 구현 (non-Circular)
-Code
#include<stdio.h>
#define MAX_SIZE 10
void inqueue(int data);
int dequeue();
int queue_full();
int queue_empty();
int queue[MAX_SIZE] = {};
int front = -1;
int rear = -1;
int main() {
int want = 0; //operation number
int input = 0; //input data
while (1) {
printf("What do you want? 1.inqueue 2.dequeue 3.end :");
scanf_s("%d", &want);
switch (want) {
case 1:
if (queue_full())
printf("Now queue is full... you can't inqueue.\n\n");
else {
printf("Input inqueue number: ");
scanf_s("%d", &input);
inqueue(input);
printf("Done.\n");
}
break;
case 2:
if (queue_empty())
printf("Now queue is empty... you can't dequeue.\n\n");
else {
printf("dequeue is %d \n", dequeue());
}
break;
case 3:
return 0;
}
}
}
void inqueue(int data)
{
if (rear == -1)
front == 0;
rear++;
queue[rear] = data;
}
int dequeue()
{
front++;
return queue[front];
}
int queue_full()
{
if (rear == MAX_SIZE - 1)
return 1;
else
return 0;
}
int queue_empty()
{
if (front == rear)
return 1;
else
return 0;
}
'공부 > C, C++' 카테고리의 다른 글
[C/C++] Class (1) - 접근 제한자 (private, protected, public) (0) | 2022.03.22 |
---|---|
[C/C++] C 에서 C++로의 변화 - 구조체 (0) | 2022.03.21 |
[C] Stack 구현 (non -Circular) (0) | 2022.03.14 |
[C] 링크드 리스트 LinkedList (0) | 2022.03.13 |
[C] 함수 포인터 (0) | 2022.03.12 |
댓글