C Programming Language/C

C์–ธ์–ด ๋™์  ํ• ๋‹น๊ณผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

567Rabbit 2024. 8. 13. 16:42

 

๋™์  ํ• ๋‹น ๋ฉ”๋ชจ๋ฆฌ์˜ ๊ฐœ๋…

 

ํ”„๋กœ๊ทธ๋žจ์ด ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›๋Š” ๋ฐฉ๋ฒ•

-์ •์ (static)

-๋™์ (dynamic)

 

 

์ •์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

 

- ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘๋˜๊ธฐ ์ „์— ๋ฏธ๋ฆฌ ์ •ํ•ด์ง„ ํฌ๊ธฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›๋Š” ๊ฒƒ- ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ๊ธฐ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ์‹œ์ž‘ํ•˜๊ธฐ ์ „์— ๊ฒฐ์ •ex) int score_s[100];- ์ฒ˜์Œ์— ๊ฒฐ์ •๋œ ํฌ๊ธฐ๋ณด๋‹ค ๋” ํฐ ์ž…๋ ฅ์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•จ- ๋” ์ž‘์€ ์ž…๋ ฅ์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด ๋‚จ์€ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์€ ๋‚ญ๋น„๋จ

 

 

๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น

 

- ์‹คํ–‰ ๋„์ค‘์— ๋™์ ์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›๋Š” ๊ฒƒ- ์‚ฌ์šฉ์ด ๋๋‚˜๋ฉด ์‹œ์Šคํ…œ์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฐ˜๋‚ฉ(full)

ex) score = (int *)               malloc(100*sizeof(int));

- ํ•„์š”ํ•œ ๋งŒํผ๋งŒ ํ• ๋‹น(malloc)์„ ๋ฐ›๊ณ  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋งค์šฐ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉ

 

 

 

์˜ˆ์ œ 1

#include <stdio.h>
#include <stdlib.h>  
// malloc, free, exit ๋“ฑ์˜ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

int main(void)
{
    int *list;
    list = (int*)malloc(3 * sizeof(int));   //๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น
    if (list == NULL) {  //๋ฐ˜ํ™˜๊ฐ’์ด NULL์ธ์ง€ ๊ฒ€์‚ฌ
    	printf("๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์˜ค๋ฅ˜\n");
    	exit(1);   //exit(1)์„ ํ˜ธ์ถœํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒ. 1์€ ์ผ๋ฐ˜์ ์œผ๋กœ ์˜ค๋ฅ˜๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ข…๋ฃŒ ์ƒํƒœ ์ฝ”๋“œ
    }
    list[0] = 10;
    list[1] = 20;
    list[2] = 30;
    free(list);   //๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ
    return 0;
}

 

ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ์‹œ์ž‘์ฃผ์†Œ : list

 

list = (int*)malloc(3 * sizeof(int));

 

: malloc ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋™์ ์œผ๋กœ ํ• ๋‹น

3 * sizeof(int)๋Š” intํ˜• ๋ณ€์ˆ˜ 3๊ฐœ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ํฌ๊ธฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์š”์ฒญํ•œ๋‹ค

sizeof(int)๋Š” intํ˜• ๋ณ€์ˆ˜์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ”์ดํŠธ ๋‹จ์œ„๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค

์ด ํฌ๊ธฐ๋งŒํผ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น๋ฐ›์•„ list ํฌ์ธํ„ฐ๊ฐ€ ์ด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋œ๋‹ค.

๋ฐ˜ํ™˜๊ฐ’์€ void*ํ˜•์ด๋ฏ€๋กœ (int*)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ int*ํ˜•์œผ๋กœ ํ˜• ๋ณ€ํ™˜

 

 

 

 

๋™์  ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ

 

 

๋™์  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ์ทจ๊ธ‰

 

int *score = (int *) malloc (sizeof(int) * 10)

score[0] = 100;

score[1] = 200;

score[2] = 300;

...

 

ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ๋Š” ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

 

  • malloc ํ•จ์ˆ˜๋Š” sizeof(int) * 10 ๋ฐ”์ดํŠธ ๋งŒํผ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•œ๋‹ค
  • ์—ฌ๊ธฐ์„œ sizeof(int)๋Š” intํ˜• ๋ณ€์ˆ˜์˜ ํฌ๊ธฐ(๋ณดํ†ต 4๋ฐ”์ดํŠธ)๋ฅผ ๋ฐ˜ํ™˜. ๋”ฐ๋ผ์„œ 10 * 4 ๋ฐ”์ดํŠธ ํฌ๊ธฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋œ๋‹ค
  • malloc์˜ ๋ฐ˜ํ™˜๊ฐ’์€ void* ํ˜•์ด๋ฏ€๋กœ (int *)๋กœ ํ˜• ๋ณ€ํ™˜ํ•˜์—ฌ int*๋กœ ์‚ฌ์šฉ
  • score๋Š” ํฌ์ธํ„ฐ์ด์ง€๋งŒ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Š” C์—์„œ ํฌ์ธํ„ฐ์™€ ๋ฐฐ์—ด์ด ๋งค์šฐ ์œ ์‚ฌํ•˜๊ฒŒ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ. score[i]๋Š” *(score + i)์™€ ๊ฐ™๋‹ค.

 

 

ํฌ์ธํ„ฐ ์—ฐ์‚ฐ์œผ๋กœ๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

 

*score = 100;

*(score+1) = 200;

*(score+2)  = 300;

...

 

  • ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ๋Š” ํฌ์ธํ„ฐ ์—ฐ์‚ฐ์œผ๋กœ๋„ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋‹ค
  • *(score + i)๋Š” score[i]์™€ ๊ฐ™๋‹ค.
  • ํฌ์ธํ„ฐ ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐฐ์—ด์˜ ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ  ๊ฐ’์„ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค.
  • score + 3์€ score ํฌ์ธํ„ฐ๋ฅผ 3๋งŒํผ ์ด๋™์‹œํ‚จ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, ์—ฌ๊ธฐ์— ์—ญ์ฐธ์กฐ ์—ฐ์‚ฐ์ž *๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์„ค์ •

 

 

 

 

 

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ (linked list)

 

 

๋ฐฐ์—ด(array)

 

- ๊ตฌํ˜„์ด ๊ฐ„๋‹จํ•˜๊ณ  ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜๋ฉฐ ์ค‘๊ฐ„์—์„œ ์‚ฝ์ž…, ์‚ญ์ œ๊ฐ€ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค. (์œ ์—ฐ์„ฑ ๋–จ์–ด์ง)

- ์ค‘๊ฐ„์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค. list[3]...

 

 

 

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ(linked list)

 

- ๊ฐ๊ฐ์˜ ์›์†Œ(node)๊ฐ€ ํฌ์ธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค์Œ ์›์†Œ(node)๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค(pointํ•œ๋‹ค). 

- ์ค‘๊ฐ„์— ์›์†Œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•˜๊ธฐ ์šฉ์ดํ•˜๋‹ค (ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๋™์ ์œผ๋กœ ๊ณต๊ฐ„์„ ๋งŒ๋“ค์–ด์„œ ์ถ”๊ฐ€)

- ์ค‘๊ฐ„์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์—†๊ณ  ์•ž์—์„œ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๊ฒ€์ƒ‰ํ•ด์•ผ ํ•œ๋‹ค. ๊ตฌํ˜„์ด ์–ด๋ ต๊ณ  ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ธฐ ์‰ฝ๋‹ค.

 

 

 

๋…ธ๋“œ์™€ ํฌ์ธํ„ฐ ์ƒ์„ฑํ•˜๊ธฐ

 

#include <stdio.h>
#include <stdlib.h>

struct node{               //๋…ธ๋“œ ์ƒ์„ฑ
    int score;
    struct node* next;
};

void main()
{
    struct node* new_node;
    
    new_node = (struct node*) malloc (sizeof(struct node));   // ํฌ์ธํ„ฐ ์ƒ์„ฑ
    new_node -> score = 100;
    new_node -> next = NULL;
}

 

 

 

 

๋…ธ๋“œ ์ƒ์„ฑํ•˜๊ธฐ

ex) 5๋ช…์˜ ์„ฑ์ ์„ ์ €์žฅํ•˜๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

 



 

 

 

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ๊ณผ์ •

#include <stdio.h>
#include <stdlib.h>
#define MAX_COUNT 5

struct node{
    int score;
    struct node* next;   // ๋‹ค์Œ ๋…ธ๋“œ์˜ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ด
};

void print_list(struct node* list_head)
{
    // ๋ฆฌ์ŠคํŠธ์˜ ๋‚ด์šฉ์„ ๋ชจ๋‘ ์ถœ๋ ฅ
    while(list_head != NULL) {
    	printf("%d => ", list_head->score);
        list_head = list_head -> next;
    }
    printf("\n")
}

void main()
{
    int count = 0;
    int tmp_score = 0;
    struct node* new_node;     // ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์œ„์น˜ ์ €์žฅํ•  ํฌ์ธํ„ฐ
    struct node* list_head = NULL;   // ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘ ๋…ธ๋“œ
    
    while (count <  MAX_COUNT){
        // ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ
        printf("score %d: ", count);
        scanf("%d", &tmp_score);
        
        // 1. ๋…ธ๋“œ ์ƒ์„ฑ
        new_node =
        	(struct node*) malloc (sizeof(struct node));
        new_node->score = tmp_score;
        
        // 2. ๋…ธ๋“œ ์—ฐ๊ฒฐ
        new_node -> next = list_head;
        list_head = new_node;
        
        // 3. ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ
        print_list(list_head);
        count++;
    }
}

 

 

๊ฒฐ๊ณผ

 

score 0: 77

77 =>

score 1: 100

100 => 77  =>

score 2: 92

92 => 100 => 77 =>

score 3: 87

87 => 92 => 100 => 77 =>

score 4 : 95

95 => 87 => 92 => 100 => 77 =>

๊ณ„์†ํ•˜๋ ค๋ฉด ์•„๋ฌด ํ‚ค๋‚˜ ๋ˆ„๋ฅด์‹ญ์‹œ์˜ค . . .

 

 

 

 

 

์ž๋ฃŒํ˜•์„ ์žฌ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ• : typedef

 

- struct node๋ฅผ node_t๋ผ๋Š” ์ด๋ฆ„์˜ ์ž๋ฃŒํ˜•์œผ๋กœ ์ •์˜

- int, double์ฒ˜๋Ÿผ node_t๋กœ ์‚ฌ์šฉ

 

#include <stdio.h>
#include <stdlib.h>
#define MAX_COUNT 5

typedef struct node {
    int score;
    struct node* next;
} node_t;

void print_list(node_t* list_head) {
    // ๋ฆฌ์ŠคํŠธ์˜ ๋‚ด์šฉ์„ ๋ชจ๋‘ ์ถœ๋ ฅ
    while (list_head != NULL) {
        printf("%d => ", list_head->score);
        list_head = list_head->next;
    }
    printf("\n");  // ์„ธ๋ฏธ์ฝœ๋ก  ์ถ”๊ฐ€
}

int main() {
    int count = 0;
    int tmp_score = 0;
    node_t* new_node;  // ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์œ„์น˜ ์ €์žฅํ•  ํฌ์ธํ„ฐ
    node_t* list_head = NULL;  // ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘ ๋…ธ๋“œ
    node_t* temp;  // ์ž„์‹œ ๋…ธ๋“œ๋ฅผ ์œ„ํ•ด ์‚ฌ์šฉ

    while (count < MAX_COUNT) {
        // ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ
        printf("score %d: ", count);
        scanf("%d", &tmp_score);

        // 1. ๋…ธ๋“œ ์ƒ์„ฑ
        new_node = malloc(sizeof(node_t));
        if (new_node == NULL) {
            fprintf(stderr, "Memory allocation failed\n");
            return 1;  // ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ์‹คํŒจ ์‹œ ์ข…๋ฃŒ
        }
        new_node->score = tmp_score;

        // 2. ๋…ธ๋“œ ์—ฐ๊ฒฐ
        new_node->next = list_head;
        list_head = new_node;

        // 3. ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ
        print_list(list_head);
        count++;
    }

    // ๋™์ ์œผ๋กœ ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ ํ•ด์ œ
    while (list_head != NULL) {
        temp = list_head;
        list_head = list_head->next;
        free(temp);
    }

    return 0;  // ์ •์ƒ ์ข…๋ฃŒ
}

 

 

 

 

์„ฑ์ ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚ด๋ฆผ์ฐจ์ˆœ ์ถœ๋ ฅํ•˜๊ธฐ ์˜ˆ์ œ

 

- ํ•™์ƒ ์ •๋ณด(ํ•™๋ฒˆ, ์ด๋ฆ„, ์„ฑ์ )๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  ์„ฑ์ ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ์œ ์ง€ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ ์ž‘์„ฑ

- next_p๋ผ๋Š” ํฌ์ธํ„ฐ๋กœ, ์ƒˆ๋กœ์šด ๋…ธ๋“œ๊ฐ€ ๋“ค์–ด๊ฐˆ ์œ„์น˜(์ •๋ ฌ๋  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜)๋ฅผ ์ฐพ์•„์คŒ

 

struct node* list_head;
struct node* next_p;

newt_p = list_head;

while(next_p){
    if(next_p > score < new_node -> score)
    	break;
    next_p = newt_p -> next;
}

 

 

ํฌ์ธํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐˆ ์ „ ์ฃผ์†Œ(prev_p)์™€ ํ›„ ์ฃผ์†Œ(next_p)๊ฐ€ ๋‘ ๊ฐœ ๋‹ค ํ•„์š”ํ•˜๋ฉฐ ํฌ์ธํ„ฐ ๋˜ํ•œ ๋‘ ๊ฐœ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

 

void main() {
    int count = 0;
    int tmp_score = 0;
    node_t* new_node;  // ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์œ„์น˜ ์ €์žฅํ•  ํฌ์ธํ„ฐ
    node_t* list_head = NULL;  // ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘ ๋…ธ๋“œ
    node_t* next_p;
    node_t* prev_p;

    while (count < MAX_COUNT) {
        // ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ
        printf("score %d: ", count);
        scanf("%d", &tmp_score);

        // 1. ๋…ธ๋“œ ์ƒ์„ฑ
        new_node = (node_t*) malloc (sizeof(node_t));
        new_node->score = tmp_score;
        
        // 2. ๋…ธ๋“œ ์‚ฝ์ž… ์œ„์น˜ ๊ฒ€์ƒ‰
        next_p = list_head; // ๋ฆฌ์ŠคํŠธ ํ—ค๋“œ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰
        prev_p = NULL;  // prev_p๋Š” NULL๋กœ ์ดŸ๊ธฐํ™”
        
        while(next_p){
             // ํ˜„์žฌ ๋…ธ๋“œ์˜ score ๊ฐ’์ด ์ƒˆ ๋…ธ๋“œ์˜ score ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด
             // ์ƒˆ ๋…ธ๋“œ๋ฅผ ํ˜„์žฌ ๋…ธ๋“œ ์•ž์— ์‚ฝ์ž…ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฃจํ”„ ์ข…๋ฃŒ
            if(next_p -> score < new_node -> score)
            	break;
            // ํ˜„์žฌ ๋…ธ๋“œ์˜ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ
            prev_p = next_p;
            // ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์ด๋™
            next_p =  next_p -> next;
        }

        // 2. ๋…ธ๋“œ ์‚ฝ์ž…
        new_node.next = next_p;   // ์ƒˆ ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋Š” ํ˜„์žฌ ๋…ธ๋“œ(next_p)๋ฅผ ๊ฐ€๋ฆฌํ‚ด
        
        if(prev_p){  // ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ๋ฆฌ์ŠคํŠธ์˜ ์ค‘๊ฐ„์— ์‚ฝ์ž…๋˜์—ˆ์„ ๋•Œ
        	prev_p -> next = new_node;   // ํ˜„์žฌ ๋…ธ๋“œ(prev_p)์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ƒˆ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ
        } else {  //์ฒ˜์Œ์— ์‚ฝ์ž…ํ•  ๋•Œ prev_p๋Š” null๋กœ ์ดˆ๊ธฐํ™”๋˜๋ฏ€๋กœ
        	list_head = new_node;   // ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ์ƒˆ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ
        }
        
        // 3. ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ
        print_list(list_head);  // ๋ฆฌ์ŠคํŠธ์˜ ํ˜„์žฌ ์ƒํƒœ๋ฅผ ์ถœ๋ ฅ
        count++;                // ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์นด์šดํŠธ ์ฆ๊ฐ€
    }
}