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}