Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2019년 9월 19일 목요일

Doubly list _Alpha


"doubly list.h"
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
#ifndef _DOUBLY_LIST_
#define _DOUBLY_LIST_
 
struct Doubly_List_Node_Type {
 
    int data;
    struct Doubly_List_Node_Type* Pointer_Left_Link;
    struct Doubly_List_Node_Type* Pointer_Right_Link;
};
 
struct Doubly_List_Type {
 
    int Current_Element_Count;
    struct Doubly_List_Node_Type Pointer_Header_Node;
};
 
struct Doubly_List_Type* Create_Doubly_List(void);
void Delete_Doubly_List(struct Doubly_List_Type* Pointer_List);
void Delete_Doubly_List_Alpha(struct Doubly_List_Type* Pointer_List);
int Add_Doubly_List_Element(struct Doubly_List_Type* Pointer_List, int position, struct Doubly_List_Node_Type element);
int Add_Doubly_List_Element_Alpha(struct Doubly_List_Type* Pointer_List, int position, struct Doubly_List_Node_Type element);
int Remove_Doubly_List_Element(struct Doubly_List_Type* Pointer_List, int position);
int Remove_Doubly_List_Element_Alpha(struct Doubly_List_Type* Pointer_List, int position);
void Clear_Doubly_List(struct Doubly_List_Type* Pointer_List);
int Get_Doubly_List_Length(struct Doubly_List_Type* Pointer_List);
struct Doubly_List_Node_Type* Get_Doubly_List_Element(struct Doubly_List_Type* Pointer_List, int position);
void Display_Doubly_List(struct Doubly_List_Type* Pointer_List);
 
#endif // !_DOUBLY_LIST_
 
#ifndef _COMMON_LIST_DEFAULT_
#define _COMMON_LIST_DEFAULT_
 
#define TRUE    1
#define FALSE    0
 
#endif // !_COMMON_LIST_DEFAULT_
 
cs
"doubly list.c"
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "doubly list.h"
 
struct Doubly_List_Type* Create_Doubly_List(void) {
 
    struct Doubly_List_Type* Pointer_Return = (struct Doubly_List_Type*)malloc(1 * sizeof(struct Doubly_List_Type));
    if (!(struct Doubly_List_Type*)Pointer_Return) {
 
        printf("Error! The dynamic memory allocation failed. Create_Doubly_List()\n");
        return NULL;
    }
 
    (void*)memset((struct Doubly_List_Type*)Pointer_Return, 01 * sizeof(struct Doubly_List_Type));
 
    Pointer_Return->Pointer_Header_Node.Pointer_Left_Link = (struct Doubly_List_Node_Type*&Pointer_Return->Pointer_Header_Node;
    Pointer_Return->Pointer_Header_Node.Pointer_Right_Link = (struct Doubly_List_Node_Type*&Pointer_Return->Pointer_Header_Node;
 
    return (struct Doubly_List_Type*)Pointer_Return;
}
 
int Add_Doubly_List_Element_Alpha(struct Doubly_List_Type* Pointer_List, int position, struct Doubly_List_Node_Type element) {
 
    if (!(struct Doubly_List_Type*) Pointer_List) {
 
        printf("Error! The doubly list was empty. Add_Doubly_List_Element()\n");
        return FALSE;
    }
    if (!(position >= 0 && position <= Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List))) {
 
        printf("Error! The Position index was invalid. Add_Doubly_List_Element()\n");
        return FALSE;
    }
 
    struct Doubly_List_Node_Type* Pointer_New_Node = (struct Doubly_List_Node_Type*malloc(1 * sizeof(struct Doubly_List_Node_Type));
    if (!(struct Doubly_List_Node_Type*)Pointer_New_Node) {
 
        printf("Error! The dynamic memory allocation failed. Add_Doubly_List_Element()\n");
        return FALSE;
    }
    *Pointer_New_Node = *(struct Doubly_List_Node_Type*&element;
    Pointer_New_Node->Pointer_Left_Link = NULL;
    Pointer_New_Node->Pointer_Right_Link = NULL;
 
    struct Doubly_List_Type* Pointer_Header = (struct Doubly_List_Type*)Pointer_List;
    struct Doubly_List_Node_Type* Pointer_Previous_Node = (struct Doubly_List_Node_Type*&Pointer_List->Pointer_Header_Node;
 
    if (position == 0 && Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List) == 0) {
 
        Pointer_New_Node->Pointer_Left_Link = (struct Doubly_List_Node_Type*) Pointer_New_Node;
        Pointer_New_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*) Pointer_New_Node;
 
        Pointer_Header->Pointer_Header_Node.Pointer_Right_Link = (struct Doubly_List_Node_Type*) Pointer_New_Node;
 
        Pointer_List->Current_Element_Count++;
 
        return TRUE;
    }
    else if (position == 0 && Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List) != 0) {
 
        Pointer_New_Node->Pointer_Left_Link = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link->Pointer_Left_Link;
        Pointer_New_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*) Pointer_Previous_Node->Pointer_Right_Link;
        Pointer_Previous_Node->Pointer_Right_Link->Pointer_Left_Link = (struct Doubly_List_Node_Type*) Pointer_New_Node;
        Pointer_New_Node->Pointer_Left_Link->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_New_Node;
 
        Pointer_Header->Pointer_Header_Node.Pointer_Right_Link = (struct Doubly_List_Node_Type*) Pointer_New_Node;
 
        Pointer_List->Current_Element_Count++;
 
        return TRUE;
    }
 
    int loop = 0;
    for (loop = 0; loop < position; loop++) {
 
        Pointer_Previous_Node = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
    }
 
    Pointer_New_Node->Pointer_Left_Link = (struct Doubly_List_Node_Type*)Pointer_Previous_Node;
    Pointer_New_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
    Pointer_Previous_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_New_Node;
    Pointer_New_Node->Pointer_Right_Link->Pointer_Left_Link = (struct Doubly_List_Node_Type*) Pointer_New_Node;
 
    Pointer_List->Current_Element_Count++;
 
    return TRUE;
}
 
int Add_Doubly_List_Element(struct Doubly_List_Type* Pointer_List, int position, struct Doubly_List_Node_Type element) {
 
    if (!(struct Doubly_List_Type*) Pointer_List) {
 
        printf("Error! The doubly list was empty. Add_Doubly_List_Element()\n");
        return FALSE;
    }
    if (!(position >= 0 && position <= Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List))) {
 
        printf("Error! The Position index was invalid. Add_Doubly_List_Element()\n");
        return FALSE;
    }
 
    struct Doubly_List_Node_Type* Pointer_Previous_Node = (struct Doubly_List_Node_Type*&Pointer_List->Pointer_Header_Node;
    int loop = 0;
    for (loop = 0; loop < position; loop++) {
 
        Pointer_Previous_Node = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
    }
 
    struct Doubly_List_Node_Type* Pointer_New_Node = (struct Doubly_List_Node_Type*malloc(1 * sizeof(struct Doubly_List_Node_Type));
    if (!(struct Doubly_List_Node_Type*)Pointer_New_Node) {
 
        printf("Error! The dynamic memory allocation failed. Add_Doubly_List_Element()\n");
        return FALSE;
    }
    *Pointer_New_Node = *(struct Doubly_List_Node_Type*&element;
    Pointer_New_Node->Pointer_Left_Link = NULL;
    Pointer_New_Node->Pointer_Right_Link = NULL;
 
    Pointer_New_Node->Pointer_Left_Link = (struct Doubly_List_Node_Type*)Pointer_Previous_Node;
    Pointer_New_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
    Pointer_Previous_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_New_Node;
    Pointer_New_Node->Pointer_Right_Link->Pointer_Left_Link = (struct Doubly_List_Node_Type*) Pointer_New_Node;
 
    Pointer_List->Current_Element_Count++;
 
    return TRUE;
}
 
int Remove_Doubly_List_Element_Alpha(struct Doubly_List_Type* Pointer_List, int position) {
 
    if (!(struct Doubly_List_Type*)Pointer_List) {
 
        printf("Error! The doubly list was empty. Remove_Doubly_List_Element()\n");
        return FALSE;
    }
    if (!(position >= 0 && position < Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List))) {
 
        printf("Error! The position index was invalid. Remove_Doubly_List_Element()\n");
        return FALSE;
    }
 
    struct Doubly_List_Node_Type* Pointer_Previous_Node = (struct Doubly_List_Node_Type*&Pointer_List->Pointer_Header_Node;
    struct Doubly_List_Node_Type* Pointer_Delete_Node = NULL;
 
    int List_Length = Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List);
 
    if (position == 0 && List_Length == 1) {
 
        Pointer_Delete_Node = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
        Pointer_Previous_Node->Pointer_Right_Link = NULL;
        free((struct Doubly_List_Node_Type*)Pointer_Delete_Node);
 
        Pointer_List->Current_Element_Count = 0;
 
        return TRUE;
    }
    else if (position == 0 && List_Length != 1) {
 
        struct Doubly_List_Node_Type* Pointer_Next_Node = (struct Doubly_List_Node_Type*&Pointer_List->Pointer_Header_Node;
 
        Pointer_Next_Node = (struct Doubly_List_Node_Type*)Pointer_Next_Node->Pointer_Right_Link->Pointer_Right_Link;
        Pointer_Delete_Node = (struct Doubly_List_Node_Type*)Pointer_Next_Node->Pointer_Left_Link;
        Pointer_Next_Node->Pointer_Left_Link = (struct Doubly_List_Node_Type*)Pointer_Delete_Node->Pointer_Left_Link;
        Pointer_Delete_Node->Pointer_Left_Link->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_Next_Node;
        Pointer_Previous_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_Next_Node;
        free((struct Doubly_List_Node_Type*)Pointer_Delete_Node);
 
        Pointer_List->Current_Element_Count--;
 
        return TRUE;
    }
 
    int loop = 0;
    for (loop = 0; loop < position; loop++) {
 
        Pointer_Previous_Node = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
    }
 
    Pointer_Delete_Node = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
    Pointer_Previous_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_Delete_Node->Pointer_Right_Link;
    Pointer_Previous_Node->Pointer_Right_Link->Pointer_Left_Link = (struct Doubly_List_Node_Type*)Pointer_Previous_Node;
    free((struct Doubly_List_Node_Type*)Pointer_Delete_Node);
 
    Pointer_List->Current_Element_Count--;
 
    return TRUE;
}
 
int Remove_Doubly_List_Element(struct Doubly_List_Type* Pointer_List, int position) {
 
    if (!(struct Doubly_List_Type*)Pointer_List) {
 
        printf("Error! The doubly list was empty. Remove_Doubly_List_Element()\n");
        return FALSE;
    }
    if (!(position >= 0 && position < Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List))) {
 
        printf("Error! The position index was invalid. Remove_Doubly_List_Element()\n");
        return FALSE;
    }
 
    struct Doubly_List_Node_Type* Pointer_Previous_Node = (struct Doubly_List_Node_Type*&Pointer_List->Pointer_Header_Node;
 
    int loop = 0;
    for (loop = 0; loop < position; loop++) {
 
        Pointer_Previous_Node = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
    }
 
    struct Doubly_List_Node_Type* Pointer_Delete_Node = (struct Doubly_List_Node_Type*)Pointer_Previous_Node->Pointer_Right_Link;
    Pointer_Previous_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*)Pointer_Delete_Node->Pointer_Right_Link;
    Pointer_Previous_Node->Pointer_Right_Link->Pointer_Left_Link = (struct Doubly_List_Node_Type*)Pointer_Previous_Node;
    free((struct Doubly_List_Node_Type*)Pointer_Delete_Node);
 
    Pointer_List->Current_Element_Count--;
 
    return TRUE;
}
 
struct Doubly_List_Node_Type* Get_Doubly_List_Element(struct Doubly_List_Type* Pointer_List, int position) {
 
    if (!(struct Doubly_List_Type*)Pointer_List) {
 
        printf("Error! The doubly list was empty. Get_Doubly_List_Element()\n");
        return NULL;
    }
    if (!(position >= 0 && position < Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List))) {
 
        printf("Error! The position index was invalid. Get_Doubly_List_Element()\n");
        return NULL;
    }
 
    struct Doubly_List_Node_Type* Pointer_Node = (struct Doubly_List_Node_Type*)Pointer_List->Pointer_Header_Node.Pointer_Right_Link;
 
    int loop = 0;
    for (loop = 0; loop < position; loop++) {
 
        Pointer_Node = (struct Doubly_List_Node_Type*)Pointer_Node->Pointer_Right_Link;
    }
 
    return (struct Doubly_List_Node_Type*)Pointer_Node;
}
 
void Display_Doubly_List(struct Doubly_List_Type* Pointer_List) {
 
    if (!(struct Doubly_List_Type*)Pointer_List) {
 
        printf("Error! The doubly list was empty. Display_Doubly_List()\n");
        return;
    }
 
    printf("Number of current node : %d\n", Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List));
 
    int index = 0;
    for (index = 0; index < Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List); index++) {
 
        printf("[%d], %d\n", index, Get_Doubly_List_Element((struct Doubly_List_Type*)Pointer_List, index)->data);
    }
 
    return;
}
 
void Delete_Doubly_List_Alpha(struct Doubly_List_Type* Pointer_List) {
 
    if (!(struct Doubly_List_Type*)Pointer_List) {
 
        printf("Error! The doubly list was empty. Delete_Doubly_List()\n");
        return;
    }
 
    struct Doubly_List_Node_Type* Pointer_Last_Node = (struct Doubly_List_Node_Type*) Pointer_List->Pointer_Header_Node.Pointer_Right_Link->Pointer_Left_Link;
 
    int loop = 0, List_Length = Get_Doubly_List_Length((struct Doubly_List_Type*)Pointer_List);
    for (loop = 0; loop < List_Length; loop++) {
 
        struct Doubly_List_Node_Type* Pointer_Delete_Node = (struct Doubly_List_Node_Type*) Pointer_Last_Node->Pointer_Right_Link;
        Pointer_Delete_Node->Pointer_Right_Link->Pointer_Left_Link = (struct Doubly_List_Node_Type*) Pointer_Last_Node;
        Pointer_Last_Node->Pointer_Right_Link = (struct Doubly_List_Node_Type*) Pointer_Delete_Node->Pointer_Right_Link;
        Pointer_List->Pointer_Header_Node.Pointer_Right_Link = (struct Doubly_List_Node_Type*) Pointer_Last_Node->Pointer_Right_Link;
        free((struct Doubly_List_Node_Type*)Pointer_Delete_Node);
 
        Pointer_List->Current_Element_Count--;
    }
    Pointer_List->Pointer_Header_Node.Pointer_Right_Link = NULL;
 
    free((struct Doubly_List_Type*)Pointer_List);
    Pointer_List = NULL;
 
    return;
}
 
void Delete_Doubly_List(struct Doubly_List_Type* Pointer_List) {
 
    if (!(struct Doubly_List_Type*)Pointer_List) {
 
        printf("Error! The doubly list was empty. Delete_Doubly_List()\n");
        return;
    }
 
    Clear_Doubly_List((struct Doubly_List_Type*)Pointer_List);
    free((struct Doubly_List_Type*)Pointer_List);
 
    return;
}
 
void Clear_Doubly_List(struct Doubly_List_Type* Pointer_List) {
 
    if (!(struct Doubly_List_Type*)Pointer_List) {
 
        printf("Error! The doubly list was empty. Delete_Doubly_List()\n");
        return;
    }
 
    while (Pointer_List->Current_Element_Count) {
 
        Remove_Doubly_List_Element((struct Doubly_List_Type*)Pointer_List, 0);
    }
 
    return;
}
 
int Get_Doubly_List_Length(struct Doubly_List_Type* Pointer_List) {
 
    if (!(struct Doubly_List_Type*)Pointer_List) {
 
        printf("Error! The doubly list was empty. Get_Doubly_List_Length()\n");
        return 0;
    }
    return (int)Pointer_List->Current_Element_Count;
}
cs
"main.c"
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
#include <stdio.h>
#include <stdlib.h>
#include "doubly list.h"
 
int main(int argc, char* argv[]) {
 
    typedef struct Doubly_List_Type Doubly_List;
    typedef struct Doubly_List_Node_Type Doubly_List_Node;
 
    Doubly_List* Pointer_List = (Doubly_List*)Create_Doubly_List();
    if (!(Doubly_List*)Pointer_List) return 0;
 
    Doubly_List_Node node;
 
    int index = 0, increase = 1 - 2;
    for (index = 0; index < 10; index++) {
 
        node.data = increase += 2;
        Add_Doubly_List_Element_Alpha((Doubly_List*)Pointer_List, index, node);
    }
 
    Display_Doubly_List((Doubly_List*)Pointer_List);
 
    Remove_Doubly_List_Element_Alpha((Doubly_List*)Pointer_List, 0);
    Display_Doubly_List((Doubly_List*)Pointer_List);
    
    Delete_Doubly_List_Alpha((Doubly_List*)Pointer_List);
 
    system("pause");
 
    return 0;
}
cs

0 개의 댓글:

댓글 쓰기