1// linked_stack.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 *top;
18 int size;
19} LinkedStack;
20
21// ์คํ ์์ฑ
22LinkedStack* lstack_create(void) {
23 LinkedStack *s = malloc(sizeof(LinkedStack));
24 if (s) {
25 s->top = NULL;
26 s->size = 0;
27 }
28 return s;
29}
30
31// ์คํ ํด์ (๋ชจ๋ ๋ฉ๋ชจ๋ฆฌ ํด์ )
32void lstack_destroy(LinkedStack *s) {
33 Node *current = s->top;
34 while (current) {
35 Node *next = current->next;
36 free(current);
37 current = next;
38 }
39 free(s);
40}
41
42// ๋น์ด์๋์ง ํ์ธ
43bool lstack_isEmpty(LinkedStack *s) {
44 return s->top == NULL;
45}
46
47// Push - ๋งจ ์์ ์์ ์ถ๊ฐ (O(1))
48bool lstack_push(LinkedStack *s, int value) {
49 Node *node = malloc(sizeof(Node));
50 if (!node) {
51 printf("๋ฉ๋ชจ๋ฆฌ ํ ๋น ์คํจ!\n");
52 return false;
53 }
54
55 node->data = value;
56 node->next = s->top; // ์ ๋
ธ๋๊ฐ ํ์ฌ top์ ๊ฐ๋ฆฌํด
57 s->top = node; // top์ ์ ๋
ธ๋๋ก ๊ฐฑ์
58 s->size++;
59 return true;
60}
61
62// Pop - ๋งจ ์ ์์ ์ ๊ฑฐ ํ ๋ฐํ (O(1))
63bool lstack_pop(LinkedStack *s, int *value) {
64 if (lstack_isEmpty(s)) {
65 printf("Stack Underflow!\n");
66 return false;
67 }
68
69 Node *node = s->top;
70 *value = node->data;
71 s->top = node->next; // top์ ๋ค์ ๋
ธ๋๋ก ์ด๋
72 free(node); // ์ ๊ฑฐ๋ ๋
ธ๋ ๋ฉ๋ชจ๋ฆฌ ํด์
73 s->size--;
74 return true;
75}
76
77// Peek - ๋งจ ์ ์์ ํ์ธ (์ ๊ฑฐ ์ํจ) (O(1))
78bool lstack_peek(LinkedStack *s, int *value) {
79 if (lstack_isEmpty(s)) {
80 return false;
81 }
82 *value = s->top->data;
83 return true;
84}
85
86// ์คํ ์ถ๋ ฅ (top๋ถํฐ bottom๊น์ง)
87void lstack_print(LinkedStack *s) {
88 printf("Stack (size=%d): ", s->size);
89 Node *current = s->top;
90 while (current) {
91 printf("%d ", current->data);
92 current = current->next;
93 }
94 printf("(top โ bottom)\n");
95}
96
97// ์คํ ํฌ๊ธฐ ๋ฐํ
98int lstack_size(LinkedStack *s) {
99 return s->size;
100}
101
102// ํ
์คํธ ์ฝ๋
103int main(void) {
104 LinkedStack *s = lstack_create();
105 if (!s) {
106 printf("์คํ ์์ฑ ์คํจ!\n");
107 return 1;
108 }
109
110 printf("=== ์ฐ๊ฒฐ ๋ฆฌ์คํธ ๊ธฐ๋ฐ ์คํ ํ
์คํธ ===\n\n");
111
112 // Push ํ
์คํธ
113 printf("[ Push ์ฐ์ฐ ]\n");
114 for (int i = 1; i <= 5; i++) {
115 printf("Push %d -> ", i * 10);
116 lstack_push(s, i * 10);
117 lstack_print(s);
118 }
119
120 // Peek ํ
์คํธ
121 printf("\n[ Peek ์ฐ์ฐ ]\n");
122 int top;
123 if (lstack_peek(s, &top)) {
124 printf("Top ๊ฐ: %d (์คํ์ ๋ณ๊ฒฝ ์๋จ)\n", top);
125 lstack_print(s);
126 }
127
128 // Pop ํ
์คํธ
129 printf("\n[ Pop ์ฐ์ฐ ]\n");
130 int value;
131 while (lstack_pop(s, &value)) {
132 printf("Popped: %d, ", value);
133 lstack_print(s);
134 }
135
136 // Underflow ํ
์คํธ
137 printf("\n[ Underflow ํ
์คํธ ]\n");
138 printf("๋น ์คํ์์ Pop ์๋: ");
139 lstack_pop(s, &value);
140
141 // ๋๋ ์ฝ์
ํ
์คํธ (๋์ ํ ๋น์ ์ฅ์ )
142 printf("\n[ ๋๋ ์ฝ์
ํ
์คํธ ]\n");
143 printf("10000๊ฐ ์์ ์ฝ์
์ค...\n");
144 for (int i = 0; i < 10000; i++) {
145 lstack_push(s, i);
146 }
147 printf("์ฝ์
์๋ฃ! ํ์ฌ ํฌ๊ธฐ: %d\n", lstack_size(s));
148
149 // ๋ฉ๋ชจ๋ฆฌ ์ ๋ฆฌ
150 printf("\n๋ฉ๋ชจ๋ฆฌ ํด์ ์ค...\n");
151 lstack_destroy(s);
152 printf("์คํ ํด์ ์๋ฃ!\n");
153
154 return 0;
155}