본문 바로가기

잡다한 IT/자료구조

링크드 리스트 구현

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include<stdio.h>
#include<stdlib.h>
 
typedef struct Node {
    int data;
    Node* next;
}Node;
 
typedef struct List {
    int count;
    Node* front;
    Node* back;
}List;
 
List list;
 
void init(Node* node,int data,Node* next)
{
    node->data = data;
    node->next = next;
}
void push_front(int data)
{
    Node* now = (Node*)malloc(sizeof(Node));
    init(now, data, NULL);
 
    if (list.count == 0)
    {
        list.front = now;
        list.back = now;
    }
    else
    {
        now->next = list.front;
        list.front = now;
    }
 
    list.count++;
}
void push_back(int data)
{
    Node* now = (Node*)malloc(sizeof(Node));
    init(now, data, NULL);
 
    if (list.count == 0)
    {
        list.front = now;
        list.back = now;
    }
    else
    {
        list.back->next = now;
        list.back = now;
    }
 
    list.count++;
}
void pop_front()
{
    Node* now = (Node*)malloc(sizeof(Node));
    init(now, NULLNULL);
 
    if (list.count == 0)
    {
        printf("list is empty\n");
        return;
    }
    else if (list.count == 1)
    {
        list.front = NULL;
        list.back = NULL;
        free(list.front);
    }
    else
    {
        now = list.front;
        list.front = list.front->next;
        free(now);
    }
 
    list.count--;
 
}
void pop_back()
{
    Node* now = (Node*)malloc(sizeof(Node));
    init(now, NULLNULL);
 
    if (list.count == 0)
    {
        printf("list is empty\n");
        return;
    }
    else if (list.count == 1)
    {
        list.front = NULL;
        list.back = NULL;
        free(list.back);
    }
    else
    {
        for (now = list.front; now->next != list.back;)
        {
            now = now->next;
        }
 
        now->next = list.back->next;
        free(now->next);
        list.back = now;
    }
 
    list.count--;
}
 
void clear()
{
    Node* now = (Node*)malloc(sizeof(Node));
    init(now, NULLNULL);
 
    while (list.count)
        pop_front();
}
void insert(int data,int goal)
{
    Node* now = (Node*)malloc(sizeof(Node));
    init(now, NULLNULL);
    Node* new_node = (Node*)malloc(sizeof(Node));
    init(new_node, data, NULL);
 
    for (now = list.front; now != NULL;)
    {
        if (now->data == goal)
        {
            break;
        }
        now = now->next;
    }
 
    if (now == NULL)
    {
        printf("찾는 데이터가 없습니다\n");
        return;
    }
 
    new_node->next = now->next;
    now->next = new_node;
    list.count++;
}
void del(int data)
{
    Node* now = (Node*)malloc(sizeof(Node));
    init(now, NULLNULL);
    Node* pre_node = NULL;
 
 
    for (now = list.front; now != NULL;)
    {
        if (now->data == data)
        {
            break;
        }
        pre_node = now;
        now = now->next;
    }
 
    if (now == NULL)
    {
        printf("찾는 데이터가 없습니다\n");
        return;
    }
 
    if (pre_node == NULL)
    {
        list.front = list.front->next;
        free(now);
    }
    else
    {
        pre_node->next = now->next;
        free(now);
    }
 
    list.count--;
    
}
 
void printList()
{
    Node* now = (Node*)malloc(sizeof(Node));
    init(now, NULLNULL);
 
    printf("NULL -> ");
    for (now = list.front; now != NULL;)
    {
        printf("%d -> ", now->data);
        now = now->next;
    }
    printf("NULL\n");
}
 
int main(void)
{
    list.front = NULL;
    list.back = NULL;
    list.count = 0;
 
    int ch = -1;
 
    while (ch != 0)
    {
        printf("\n\n");
        printf("하고싶은 명령을 입력하세요:\n");
        printf("0번:종료 1번: push_front 2번:push_back 3번:pop_front 4번:pop_back 5번:insert 6번:delete 7번:clear 8번:print \n");
        scanf("%d"&ch);
        switch (ch)
        {
        case 1:
            int d;
            printf("삽입할 데이터를 입력하세요\n");
            scanf("%d"&d);
            push_front(d);
            break;
        case 2:
            printf("삽입할 데이터를 입력하세요\n");
            scanf("%d"&d);
            push_back(d);
            break;
        case 3:
            pop_front();
            break;
        case 4:
            pop_back();
            break;
        case 5:
            int k;
            printf("어떤 데이터를 가진 노드 뒤에 어떤 값을 추가할건가요?\n");
            scanf("%d %d"&d,&k);
            insert(k, d);
            break;
        case 6:
            printf("어떤 데이터를 가진 노드를 삭제할건가요?\n");
            scanf("%d"&d);
            del(d);
            break;
        case 7:
            clear();
            break;
        case 8:
            printList();
            break;
        default:
            break;
        }
    }
    
 
    return 0;
}
cs


반응형