: μ μ μ μΆ (First-in First-out), rear(νλ¨-μ½μ ), front(μ λ¨-μμ )
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 5
typedef int element;
typedef struct {
int front;
int rear;
element data[MAX_QUEUE_SIZE];
}QueueType;
//μ€λ₯ν¨μ
void error(char* message) {
fprintf(stderr, "%s\n", message);
}
//μ΄κΈ°ν ν¨μ
void init_queue(QueueType* q) {
q->rear = -1;
q->front = -1;
}
//ν μΆλ ₯ ν¨μ
void queue_print(QueueType* q) {
for (int i = 0;i < MAX_QUEUE_SIZE;i++) {
if (i <= q->front || i > q->rear)
printf(" | ");
else
printf("%d | ", q->data[i]);
}
}
//ν¬ν ν¨μ
int is_full(QueueType *q) {
if (q->rear == (MAX_QUEUE_SIZE - 1))
return 1;
else
return 0;
}
//곡백 ν¨μ
int is_empty(QueueType* q) {
if (q->front == q->rear)
return 1;
else
return 0;
}
//μ½μ
ν¨μ
void enqueue(QueueType* q, int item) {
if (is_full(q)) {
error("νκ° ν¬νμνμ
λλ€\n");
return;
}
q->data[++(q->rear)] = item;
}
//μμ ν¨μ
int dequeue(QueueType* q) {
if (is_empty(q)) {
error("νκ° κ³΅λ°±μνμ
λλ€.\n");
return -1;
}
int item = q->data[++(q->front)];
return item;
}
int main(void)
{
int item = 0;
QueueType q;
init_queue(&q);
enqueue(&q, 10);queue_print(&q);
enqueue(&q, 20);queue_print(&q);
enqueue(&q, 30);queue_print(&q);
item = dequeue(&q); queue_print(&q);
item = dequeue(&q); queue_print(&q);
item = dequeue(&q); queue_print(&q);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 5
typedef int element;
typedef struct {
int front;
int rear;
element data[MAX_QUEUE_SIZE];
}QueueType;
//μ€λ₯ν¨μ
void error(char* message) {
fprintf(stderr, "%s\n", message);
exit(1);
}
//μ΄κΈ°ν ν¨μ
void init_queue(QueueType* q) {
q->rear = q->front = 0;
}
//ν μΆλ ₯ ν¨μ
void queue_print(QueueType* q) {
printf("QUEUE(front=%d rear=%d) = ", q->front, q->rear);
if (!is_empty(q)) {
int i = q->front;
do {
i = (i + 1) % MAX_QUEUE_SIZE;
printf("%d | ", q->data[i]);
if (i == q->rear) { //곡백μνμΌ λ
break;
}
} while (i != q->front);
}
printf("\n");
}
//ν¬ν ν¨μ
int is_full(QueueType *q) {
if (q->front == (q->rear+1) % MAX_QUEUE_SIZE)
return 1;
else
return 0;
}
//곡백 ν¨μ
int is_empty(QueueType* q) {
if (q->front == q->rear)
return 1;
else
return 0;
}
//μ½μ
ν¨μ
void enqueue(QueueType* q, int item) {
if (is_full(q)) {
error("νκ° ν¬νμνμ
λλ€\n");
return;
}
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
q->data[q->rear] = item;
}
//μμ ν¨μ
int dequeue(QueueType* q) {
if (is_empty(q)) {
error("νκ° κ³΅λ°±μνμ
λλ€.\n");
return -1;
}
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
return q->data[q->front];
}
int main(void)
{
QueueType q;
int element;
init_queue(&q);
printf("--λ°μ΄ν° μΆκ° λ¨κ³--\n");
while (!is_full(&q)) {
printf("μ μλ₯Ό μ
λ ₯νμμ€: ");
scanf_s("%d", &element);
enqueue(&q, element);
queue_print(&q);
}
printf("νλ ν¬νμνμ
λλ€.\n");
printf("--λ°μ΄ν° μμ λ¨κ³--\n");
while (!is_empty(&q)) {
element = dequeue(&q);
printf("κΊΌλ΄μ§ μ μ: %d\n", element);
queue_print(&q);
}
printf("νλ 곡백μνμ
λλ€.\n");
return 0;
}
: μ λ¨κ³Ό νλ¨μμ λͺ¨λ μ½μ , μμ κ° κ°λ₯ν ν
& μ€νμμ pushλ μ¦κ° ν μ½μ
(μ¦κ° ν μ²λ¦¬), popμ λ°ν ν κ°μ (μ²λ¦¬ ν κ°μ)
μ νν, μννμμλ νμ μ¦κ° ν μ½μ
, μμ !! (μ¦κ° ν μ²λ¦¬)
λ±μ add_frontμ delete_rearμμλ μ½μ
ν κ°μ, λ°ν ν κ°μ!!! (μ²λ¦¬ ν κ°μ)
-> μννμ λλ¨Έμ§λ‘ λλλ κ²μ΄ μ μ΄ν΄κ° λμ§ μμμ. μ§μ μμνλ©° λμ ν΄λ³΄λ©° ν΄κ²°ν¨. λ¨Έλ¦ΏμμΌλ‘ μμν΄κ°λ©΄μ μ½λ 보기!! νμμ frontλ μ΅μ΄κ°μ μμ΄λΌλ κ² κΈ°μ΅νκΈ°!!! (μ¦κ°νκΈ° μ μλ frontμ μ무 κ°λ μλ€λ κ²)