linked_stack.c

Download
c 156 lines 3.5 KB
  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}