linked_queue.c

Download
c 214 lines 5.0 KB
  1// linked_queue.c
  2// ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ ํ ๊ตฌํ˜„
  3// ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์œผ๋กœ ํฌ๊ธฐ ์ œํ•œ ์—†์Œ
  4
  5#include <stdio.h>
  6#include <stdlib.h>
  7#include <stdbool.h>
  8
  9// ๋…ธ๋“œ ๊ตฌ์กฐ์ฒด
 10typedef struct Node {
 11    int data;
 12    struct Node *next;
 13} Node;
 14
 15// ํ ๊ตฌ์กฐ์ฒด
 16typedef struct {
 17    Node *front;  // ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ (์ œ๊ฑฐ ์œ„์น˜)
 18    Node *rear;   // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ (์‚ฝ์ž… ์œ„์น˜)
 19    int size;
 20} LinkedQueue;
 21
 22// ํ ์ƒ์„ฑ
 23LinkedQueue* lqueue_create(void) {
 24    LinkedQueue *q = malloc(sizeof(LinkedQueue));
 25    if (q) {
 26        q->front = NULL;
 27        q->rear = NULL;
 28        q->size = 0;
 29    }
 30    return q;
 31}
 32
 33// ํ ํ•ด์ œ (๋ชจ๋“  ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ)
 34void lqueue_destroy(LinkedQueue *q) {
 35    Node *current = q->front;
 36    while (current) {
 37        Node *next = current->next;
 38        free(current);
 39        current = next;
 40    }
 41    free(q);
 42}
 43
 44// ๋น„์–ด์žˆ๋Š”์ง€ ํ™•์ธ
 45bool lqueue_isEmpty(LinkedQueue *q) {
 46    return q->front == NULL;
 47}
 48
 49// Enqueue - ๋’ค์— ์š”์†Œ ์ถ”๊ฐ€ (O(1))
 50bool lqueue_enqueue(LinkedQueue *q, int value) {
 51    Node *node = malloc(sizeof(Node));
 52    if (!node) {
 53        printf("๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์‹คํŒจ!\n");
 54        return false;
 55    }
 56
 57    node->data = value;
 58    node->next = NULL;
 59
 60    // ํ๊ฐ€ ๋น„์–ด์žˆ์œผ๋ฉด front์™€ rear ๋ชจ๋‘ ์ƒˆ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ด
 61    if (q->rear == NULL) {
 62        q->front = q->rear = node;
 63    } else {
 64        // ๊ธฐ์กด rear ๋’ค์— ์ƒˆ ๋…ธ๋“œ ์ถ”๊ฐ€
 65        q->rear->next = node;
 66        q->rear = node;
 67    }
 68    q->size++;
 69    return true;
 70}
 71
 72// Dequeue - ์•ž์—์„œ ์š”์†Œ ์ œ๊ฑฐ (O(1))
 73bool lqueue_dequeue(LinkedQueue *q, int *value) {
 74    if (lqueue_isEmpty(q)) {
 75        printf("Queue is empty!\n");
 76        return false;
 77    }
 78
 79    Node *node = q->front;
 80    *value = node->data;
 81    q->front = node->next;
 82
 83    // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•œ ๊ฒฝ์šฐ rear๋„ NULL๋กœ ์„ค์ •
 84    if (q->front == NULL) {
 85        q->rear = NULL;
 86    }
 87
 88    free(node);
 89    q->size--;
 90    return true;
 91}
 92
 93// Front - ์•ž์˜ ๊ฐ’ ํ™•์ธ (์ œ๊ฑฐ ์•ˆํ•จ)
 94bool lqueue_front(LinkedQueue *q, int *value) {
 95    if (lqueue_isEmpty(q)) {
 96        return false;
 97    }
 98    *value = q->front->data;
 99    return true;
100}
101
102// Rear - ๋’ค์˜ ๊ฐ’ ํ™•์ธ
103bool lqueue_rear(LinkedQueue *q, int *value) {
104    if (lqueue_isEmpty(q)) {
105        return false;
106    }
107    *value = q->rear->data;
108    return true;
109}
110
111// ํ ์ถœ๋ ฅ (front๋ถ€ํ„ฐ rear๊นŒ์ง€)
112void lqueue_print(LinkedQueue *q) {
113    printf("Queue (size=%d): front -> ", q->size);
114    Node *current = q->front;
115    while (current) {
116        printf("%d ", current->data);
117        current = current->next;
118    }
119    printf("<- rear\n");
120}
121
122// ํ ํฌ๊ธฐ ๋ฐ˜ํ™˜
123int lqueue_size(LinkedQueue *q) {
124    return q->size;
125}
126
127// ๋ชจ๋“  ์š”์†Œ ์ถœ๋ ฅ ํ›„ ํ ์ดˆ๊ธฐํ™”
128void lqueue_clear(LinkedQueue *q) {
129    int value;
130    while (lqueue_dequeue(q, &value)) {
131        // ๋‹จ์ˆœํžˆ ๋ชจ๋“  ์š”์†Œ ์ œ๊ฑฐ
132    }
133}
134
135// ํ…Œ์ŠคํŠธ ์ฝ”๋“œ
136int main(void) {
137    LinkedQueue *q = lqueue_create();
138    if (!q) {
139        printf("ํ ์ƒ์„ฑ ์‹คํŒจ!\n");
140        return 1;
141    }
142
143    printf("=== ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ ํ ํ…Œ์ŠคํŠธ ===\n\n");
144
145    // Enqueue ํ…Œ์ŠคํŠธ
146    printf("[ 1๋‹จ๊ณ„: Enqueue 5๊ฐœ ]\n");
147    for (int i = 1; i <= 5; i++) {
148        printf("Enqueue %d -> ", i * 10);
149        lqueue_enqueue(q, i * 10);
150        lqueue_print(q);
151    }
152
153    // Front/Rear ํ™•์ธ
154    printf("\n[ 2๋‹จ๊ณ„: Front/Rear ํ™•์ธ ]\n");
155    int front_val, rear_val;
156    if (lqueue_front(q, &front_val) && lqueue_rear(q, &rear_val)) {
157        printf("Front ๊ฐ’: %d (๋จผ์ € ๋“ค์–ด์˜จ ๊ฐ’)\n", front_val);
158        printf("Rear ๊ฐ’: %d (๋‚˜์ค‘์— ๋“ค์–ด์˜จ ๊ฐ’)\n", rear_val);
159        lqueue_print(q);
160    }
161
162    // Dequeue ํ…Œ์ŠคํŠธ
163    printf("\n[ 3๋‹จ๊ณ„: Dequeue 2๊ฐœ ]\n");
164    int value;
165    for (int i = 0; i < 2; i++) {
166        if (lqueue_dequeue(q, &value)) {
167            printf("Dequeued: %d -> ", value);
168            lqueue_print(q);
169        }
170    }
171
172    // ์ถ”๊ฐ€ Enqueue
173    printf("\n[ 4๋‹จ๊ณ„: Enqueue 2๊ฐœ ๋” ]\n");
174    for (int i = 6; i <= 7; i++) {
175        printf("Enqueue %d -> ", i * 10);
176        lqueue_enqueue(q, i * 10);
177        lqueue_print(q);
178    }
179
180    // ๋ชจ๋“  ์š”์†Œ Dequeue
181    printf("\n[ 5๋‹จ๊ณ„: ๋ชจ๋“  ์š”์†Œ Dequeue ]\n");
182    while (lqueue_dequeue(q, &value)) {
183        printf("Dequeued: %d -> ", value);
184        lqueue_print(q);
185    }
186
187    // Empty ์ƒํƒœ์—์„œ ์ œ๊ฑฐ ์‹œ๋„
188    printf("\n[ 6๋‹จ๊ณ„: Empty ์ƒํƒœ์—์„œ Dequeue ์‹œ๋„ ]\n");
189    printf("Dequeue -> ");
190    lqueue_dequeue(q, &value);
191
192    // ๋Œ€๋Ÿ‰ ์‚ฝ์ž… ํ…Œ์ŠคํŠธ
193    printf("\n[ 7๋‹จ๊ณ„: ๋Œ€๋Ÿ‰ ์‚ฝ์ž… ํ…Œ์ŠคํŠธ ]\n");
194    printf("10000๊ฐœ ์š”์†Œ ์‚ฝ์ž… ์ค‘...\n");
195    for (int i = 0; i < 10000; i++) {
196        lqueue_enqueue(q, i);
197    }
198    printf("์‚ฝ์ž… ์™„๋ฃŒ! ํ˜„์žฌ ํฌ๊ธฐ: %d\n", lqueue_size(q));
199
200    // ๋Œ€๋Ÿ‰ ์ œ๊ฑฐ ํ…Œ์ŠคํŠธ
201    printf("\n์ฒ˜์Œ 5๊ฐœ ์š”์†Œ ํ™•์ธ:\n");
202    for (int i = 0; i < 5; i++) {
203        lqueue_dequeue(q, &value);
204        printf("  Dequeued: %d\n", value);
205    }
206
207    // ๋ฉ”๋ชจ๋ฆฌ ์ •๋ฆฌ
208    printf("\n๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ ์ค‘...\n");
209    lqueue_destroy(q);
210    printf("ํ ํ•ด์ œ ์™„๋ฃŒ!\n");
211
212    return 0;
213}