본문 바로가기

공부/자료구조

링크드 리스트 소스코드

반응형

test.cpp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <stdlib.h>
 
typedef struct link
{
    int num;
    struct link* next;
}link;
 
void insert(link** list, int number); //마지막 요소 뒤에 추가
void print(link* list); //링크드리스트 모든 요소 출력
void del(link* list); //마지막의 요소 삭제
void select(link* list, int number); //number에 해당하는 링크드리스트의 요소가 있는지 검사
 
int main()
{
    int number = 3;
    link* head = NULL;
    link* tail = NULL;
 
    insert(&head, 4);
    insert(&head, 4);
    insert(&head, 4);
    insert(&head, 4);
    insert(&head, 4);
    insert(&head, 5);
    insert(&head, 4);
    insert(&head, 4);
    insert(&head, 4);
 
    del(head);
    print(head);
    select(head, 4);
    return 0;
}
 
void insert(link** list, int number)
{
    link* cur = *list;
    
    if(cur == NULL)
    {
        *list = (link*)malloc(sizeof(link));
        (*list)->num = number;
        (*list)->next = NULL;
        return ;
    }
 
    else
    {
        while(cur->next != NULL)
        {
            cur = cur->next;
        }
        cur->next = (link*)malloc(sizeof(link));
        cur->next->num = number;
        cur->next->next = NULL;
        return ;
    }
}
 
void print(link* list)
{
    link* cur = list;
    if(cur == NULL)
    {
        printf("list는 아무 요소도 존재하지 않습니다.\n");
        return ;
    }
 
    else
    {
        printf("   위치 0\n");
        printf("list => ");
        while(cur->next != NULL)
        {
            printf("%d ", cur->num);
            cur = cur->next;
        }
        printf("%d\n", cur->num);
    }
    return ;
}
 
void del(link* list)
{
    link* temp;
    while(list->next != NULL)
    {
        temp = list;
        list = list->next;
    }
    
    free(list);
    temp->next = NULL;
    return ;
}
 
void select(link* list, int number)
{
    int count = 0;
    int position = 0;
    int* counter = (int*)malloc(sizeof(int));
    if(list == NULL)
    {
        printf("링크에 아무 요소도 저장되어 있지 않습니다.\n");
        return ;
    }
    
    while(list->next != NULL)
    {
        if(list->num == number)
        {
            position++; //counter(number의 값을 가진 link의 위치를 저장하는  배열)의 위치
            counter = (int*)realloc(counter, sizeof(int)*position);
            counter[position-1] = count; //link의 위치(count)
        }
        list = list->next;
        count++;
    }
 
    if(list->num == number) //link의 마지막 구조체의 num을 검사
    {
        position++; //counter(number의 값을 가진 link의 위치를 저장하는  배열)의 위치
        counter = (int*)realloc(counter, sizeof(int)*position);
        counter[position-1] = count; //link의 위치(count)
    }
 
    printf("\n%d가 저장된 링크의 정보 출력 : \n", number);
    printf("counter : \n", number);
    printf("[배열의 위치] = 값이 저장된 위치 \n", number);
    for(int i = 0; i < position; i++)
    {
        printf("[%d] = %d\n", i, counter[i]);
    }
    return ;
}
반응형