๊ฐœ์ธ ๊ณต๋ถ€ ๊ณผ์ •์„ ๊ธฐ๋กํ•ฉ๋‹ˆ๋‹ค.

๐Ÿš€ What I Will Learn

  • ํ(Queue) ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ฐœ๋…๊ณผ รฅ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ดํ•ดํ•˜๊ธฐ
  • C์–ธ์–ด๋ฅผ ์ด์šฉํ•ด ํ ์ž๋ฃŒ๊ตฌ์กฐ ๊ตฌํ˜„ํ•˜๊ธฐโ€ฆโ€ฆ.

ํ(Queue)๋ž€? ๋’ค์ชฝ์œผ๋กœ ๋“ค์–ด๊ฐ€์„œ ์•ž์ชฝ์œผ๋กœ ๋‚˜์˜ค๋Š”(์„ ์ž…์„ ์ถœ) ์ž๋ฃŒ ๊ตฌ์กฐ ํ˜•ํƒœ.


ํ(Queue)

1๏ธโƒฃ ํ(Queue)๋ž€?

  1. ๋’ค์ชฝ์œผ๋กœ ๋“ค์–ด๊ฐ€์„œ ์•ž์ชฝ์œผ๋กœ ๋‚˜์˜ค๋Š”(์„ ์ž…์„ ์ถœ) ์ž๋ฃŒ ๊ตฌ์กฐ ํ˜•ํƒœ.
  2. ์Šค์ผ€์ค„๋ง, ํƒ์ƒ‰ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋“ฑ์—์„œ ๋‹ค๋ฐฉ๋ฉด์œผ๋กœ ํ™œ์šฉ๋จ
  • PUSH: ํ์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
  • POP: ํ์—์„œ ๋ฐ์ดํ„ฐ ์‚ญ์ œ
  1. ์˜ˆ์‹œ: PUSH(7) โ€“ PUSH(5) โ€“ PUSH(4) โ€“ POP() โ€“ PUSH(6) โ€“ POP()



2๏ธโƒฃ ์Šคํƒ์˜ ๊ตฌํ˜„

  1. ํ(Queue)์˜ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์—๋Š”
  • ๋ฐฐ์—ด์„ ์ด์šฉํ•œ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•๊ณผ
  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ๊ตฌํ˜„ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค
  1. ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๊ตฌํ˜„ ๋‚œ์ด๋„๋Š” ๋‚ฎ์€ ํŽธ

โœ”๏ธ ๋ฐฐ์—ด์„ ์ด์šฉํ•œ ํ์˜ ๊ตฌํ˜„

1) ํ์˜ ์„ ์–ธ

#include <stdio.h>
#define SIZE 10000
#define INF 99999999

int queue[SIZE];
int front = 0;
int rear = 0;


2) ํ ์‚ฝ์ž… ํ•จ์ˆ˜

void push(int data) {
  if (rear >= SIZE) {
    printf("ํ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.\n");
    return; }
  queue[rear++] = data;
}


3) ํ ์ถ”์ถœ ํ•จ์ˆ˜

int pop() {
  if (front == rear) {
    printf("ํ ์–ธ๋”ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.\n");
    return -INF;
  }
  return queue[front++];
}


4) ํ ์ „์ฒด์ถœ๋ ฅ ํ•จ์ˆ˜

void show() {
  printf("--- ํ์˜ ์•ž ---\n");
  for (int i = front; i < rear; i++) {
      printf("%d\n", queue[i]);
  }
  printf("--- ํ์˜ ๋’ค ---\n");
}


5) ์™„์„ฑ๋œ ํ ์‚ฌ์šฉํ•˜๊ธฐ

int main(void) {
  push(7);
  push(5);
  push(4);
  pop();
  push(6);
  pop();
  show();
  system("pause");
  return 0;
}



โœ”๏ธ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ํ์˜ ๊ตฌํ˜„

1) ํ์˜ ์„ ์–ธ

#include <stdio.h>
#include <stdlib.h>
#define INF 99999999

typedef struct {
  int data;
  struct Node *next;
} Node;

typedef struct {
  Node *front;
  Node *rear;
  int count;
} Queue;


2) ํ ์‚ฝ์ž… ํ•จ์ˆ˜

void push(Queue *queue, int data) {
  Node *node = (Node*)malloc(sizeof(Node));
  node->data = data;
  node->next = NULL;
  if (queue->count == 0) {
      queue->front = node;
  } else {
  queue->rear->next = node;
  }
  queue->rear = node;
  queue->count++;
}


3) ํ ์ถ”์ถœ ํ•จ์ˆ˜

int pop(Queue *queue) {
  if (queue->count == 0) {
    printf("ํ ์–ธ๋”ํ”Œ๋กœ์šฐ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.\n");
    return -INF;
  }
  Node *node = queue->front;
  int data = node->data;
  queue->front = node->next;
  free(node);
  queue->count--;
  return data;
}


4) ํ ์ „์ฒด์ถœ๋ ฅ ํ•จ์ˆ˜

void show(Queue *queue) {
  Node *cur = queue->front;
  printf("--- ํ์˜ ์•ž ---\n");
  while (cur != NULL) {
      printf("%d\n", cur->data);
      cur = cur->next;
  }
  printf("--- ํ์˜ ๋’ค ---\n");
}


5) ์™„์„ฑ๋œ ํ ์‚ฌ์šฉํ•˜๊ธฐ

int main(void) {
  Queue queue;
  queue.front = queue.rear = NULL;
  queue.count = 0;
  push(&queue, 7);
  push(&queue, 5);
  push(&queue, 4);
  pop(&queue);
  push(&queue, 6);
  pop(&queue);
  show(&queue);
  system("pause");
  return 0;
}




โœจ tl;dr

  • ํ๋Š” ๋ฐฐ์—ด ํ˜น์€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค