156 lines
3.5 KiB
C
156 lines
3.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
|
|
typedef struct C_Queue C_Queue;
|
|
|
|
void _enqueue(C_Queue *q, int value);
|
|
int _dequeue(C_Queue *q);
|
|
bool _isEmpty(C_Queue *q);
|
|
bool _isFull(C_Queue *q);
|
|
void _info(C_Queue *q);
|
|
void _destroyQ(C_Queue *q);
|
|
|
|
struct C_Queue{
|
|
int *data;
|
|
int front, rear, size;
|
|
|
|
bool (*isEmpty)(C_Queue *q);
|
|
bool (*isFull)(C_Queue *q);
|
|
void (*enqueue)(C_Queue *q, int value);
|
|
int (*dequeue)(C_Queue *q);
|
|
void (*info)(C_Queue *q);
|
|
void (*destroyQ)(C_Queue *q);
|
|
};
|
|
|
|
C_Queue creatQueue(int capacity){
|
|
C_Queue q;
|
|
q.front = -1;
|
|
q.rear = -1;
|
|
q.size = capacity;
|
|
q.data = (int*)malloc(sizeof(int)*capacity);
|
|
|
|
q.enqueue = _enqueue;
|
|
q.dequeue = _dequeue;
|
|
q.info = _info;
|
|
q.isEmpty = _isEmpty;
|
|
q.isFull = _isFull;
|
|
q.destroyQ = _destroyQ;
|
|
|
|
return q;
|
|
}
|
|
|
|
int opcode(char *input);
|
|
|
|
int main(){
|
|
printf("생성하려는 원형큐의 용량을 입력해주세요. : ");
|
|
int capacity;
|
|
scanf("%d",&capacity);
|
|
|
|
C_Queue q = creatQueue(capacity);
|
|
bool exit_flag = true;
|
|
while(exit_flag){
|
|
printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
|
|
printf("명령어 목록 : enqueue | dequeue | info | exit\n");
|
|
printf("명령어를 입력해주세요. : ");
|
|
char input[10];
|
|
scanf("%s",input);
|
|
switch (opcode(input))
|
|
{
|
|
case 1:
|
|
if(q.isFull(&q)) {
|
|
printf("큐가 꽉차있어서 입력할 수 없습니다.\n");
|
|
}
|
|
else{
|
|
printf("넣을 값을 입력해주세요. : ");
|
|
int num;
|
|
scanf("%d",&num);
|
|
q.enqueue(&q, num);
|
|
}
|
|
break;
|
|
case 2:
|
|
if(q.isEmpty(&q)){
|
|
printf("큐가 비어있어서 값을 꺼낼 수가 없습니다.\n");
|
|
}
|
|
else{
|
|
printf("추출값 : %d\n",q.dequeue(&q));
|
|
}
|
|
break;
|
|
case 3:
|
|
q.info(&q);
|
|
break;
|
|
case 4:
|
|
exit_flag = false;
|
|
printf("종료합니다\n");
|
|
break;
|
|
default:
|
|
printf("명령어를 잘못 입력했습니다. 다시 입력해주세요.\n");
|
|
break;
|
|
}
|
|
}
|
|
q.destroyQ(&q);
|
|
}
|
|
|
|
int opcode(char *input){
|
|
if(!strcmp(input, "enqueue")) return 1;
|
|
if(!strcmp(input, "dequeue")) return 2;
|
|
if(!strcmp(input, "info")) return 3;
|
|
if(!strcmp(input, "exit")) return 4;
|
|
return -1;
|
|
}
|
|
|
|
bool _isEmpty(C_Queue *q){
|
|
return q->front == -1;
|
|
}
|
|
|
|
bool _isFull(C_Queue *q){
|
|
return ((q->rear)+1)%(q->size) == q->front;
|
|
}
|
|
|
|
void _enqueue(C_Queue *q, int value){
|
|
if(q->isFull(q)){
|
|
return;
|
|
}
|
|
if(q->isEmpty(q)){
|
|
q->front = 0;
|
|
}
|
|
q->rear = ((q->rear)+1)%(q->size);
|
|
q->data[q->rear] = value;
|
|
}
|
|
|
|
int _dequeue(C_Queue *q){
|
|
if(q->isEmpty(q)){
|
|
return -1;
|
|
}
|
|
int value = q->data[q->front];
|
|
if(q->front == q->rear){
|
|
q->front = -1;
|
|
q->rear = -1;
|
|
}
|
|
else{
|
|
q->front = ((q->front)+1)%q->size;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
void _info(C_Queue *q){
|
|
if(q->isEmpty(q)){
|
|
printf("큐가 비어있습니다.\n");
|
|
}
|
|
else{
|
|
printf("큐 목록: ");
|
|
int i = q->front;
|
|
while(true){
|
|
printf("%d ",q->data[i]);
|
|
if(i==q->rear) break;
|
|
i = (i+1)%q->size;
|
|
}
|
|
printf("\n");
|
|
}
|
|
}
|
|
|
|
void _destroyQ(C_Queue *q){
|
|
free(q->data);
|
|
} |