Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2019년 9월 29일 일요일

Linked deque _Alpha


"linked deque.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
39
40
41
#ifndef _LINKED_DEQUE_
#define _LINKED_DEQUE_
 
struct Deque_Node_Type {
 
    char data;
 
    struct Deque_Node_Type* Pointer_Above;
    struct Deque_Node_Type* Pointer_Below;
};
 
struct Linked_Deque_Type {
 
    int Number_Of_Current_Element;
 
    struct Deque_Node_Type Top_Header;
    struct Deque_Node_Type Bottom_Header;
};
 
struct Linked_Deque_Type* Create_Linked_Deque(void);
int Push_In_The_Top_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque, struct Deque_Node_Type element);
int Push_In_The_Bottom_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque, struct Deque_Node_Type element);
struct Deque_Node_Type* Pop_In_The_Top_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque);
struct Deque_Node_Type* Pop_In_The_Bottom_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque);
struct Deque_Node_Type* Peek_In_The_Top_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque);
struct Deque_Node_Type* Peek_In_The_Bottom_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque);
void Delete_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque);
 
int Is_Linked_Deque_Empty(struct Linked_Deque_Type* Pointer_Deque);
int Get_Number_Of_Elements_In_The_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque);
void Display_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque);
 
#endif // !_LINKED_DEQUE_
 
#ifndef _COMMON_DEFINITION_
#define _COMMON_DEFINITION_
 
#define TRUE    1
#define FALSE    0
 
#endif // !_COMMON_DEFINITION_
cs
"linked deque.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linked deque.h"
 
struct Linked_Deque_Type* Create_Linked_Deque(void) {
 
    struct Linked_Deque_Type* Pointer_Return = (struct Linked_Deque_Type*)malloc(1 * sizeof(struct Linked_Deque_Type));
    if ((struct Linked_Deque_Type*)Pointer_Return == NULL) {
 
        printf("Error! The dynamic memory allocation failed. Create_Linked_Deque()\n");
        return NULL;
    }
    (void*)memset((struct Linked_Deque_Type*)Pointer_Return, 01 * sizeof(struct Linked_Deque_Type));
 
    return (struct Linked_Deque_Type*)Pointer_Return;
}
 
int Push_In_The_Top_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque, struct Deque_Node_Type element) {
 
    if ((struct Linked_Deque_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Push_In_The_Top_Linked_Deque()\n");
        return FALSE;
    }
 
    struct Deque_Node_Type* Pointer_New_Node = (struct Deque_Node_Type*malloc(1 * sizeof(struct Deque_Node_Type));
    if ((struct Deque_Node_Type*)Pointer_New_Node == NULL) {
 
        printf("Error! The Dynamic memory allocation failed. Push_In_The_Top_Linked_Deque()\n");
        return FALSE;
    }
    (void*)memset((struct Deque_Node_Type*)Pointer_New_Node, 01 * sizeof(struct Deque_Node_Type));
 
    *Pointer_New_Node = *(struct Deque_Node_Type*&element;
 
    struct Deque_Node_Type* Pointer_Top_Header = (struct Deque_Node_Type*&Pointer_Deque->Top_Header;
    struct Deque_Node_Type* Pointer_Bottom_Header = (struct Deque_Node_Type*&Pointer_Deque->Bottom_Header;
 
    if (Is_Linked_Deque_Empty((struct Linked_Deque_Type*)Pointer_Deque)) {
 
        Pointer_New_Node->Pointer_Above = (struct Deque_Node_Type*)Pointer_Top_Header;
        Pointer_New_Node->Pointer_Below = (struct Deque_Node_Type*)Pointer_Bottom_Header;
        Pointer_Top_Header->Pointer_Below = (struct Deque_Node_Type*)Pointer_New_Node;
        Pointer_Bottom_Header->Pointer_Above = (struct Deque_Node_Type*)Pointer_New_Node;
 
        Pointer_Deque->Number_Of_Current_Element++;
        return TRUE;
    }
 
    Pointer_New_Node->Pointer_Below = (struct Deque_Node_Type*)Pointer_Top_Header->Pointer_Below;
    Pointer_New_Node->Pointer_Above = (struct Deque_Node_Type*)Pointer_Top_Header;
    Pointer_Top_Header->Pointer_Below = (struct Deque_Node_Type*)Pointer_New_Node;
    Pointer_New_Node->Pointer_Below->Pointer_Above = (struct Deque_Node_Type*)Pointer_New_Node;
 
    Pointer_Deque->Number_Of_Current_Element++;
    return TRUE;
}
 
int Push_In_The_Bottom_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque, struct Deque_Node_Type element) {
 
    if ((struct Linked_Deque_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Push_In_The_Bottom_Linked_Deque()\n");
        return FALSE;
    }
 
    struct Deque_Node_Type* Pointer_New_Node = (struct Deque_Node_Type*malloc(1 * sizeof(struct Deque_Node_Type));
    if ((struct Deque_Node_Type*)Pointer_New_Node == NULL) {
 
        printf("Error! The dynamic memory allocation failed. Push_In_The_Bottom_Linked_Deque()\n");
        return FALSE;
    }
    (void*)memset((struct Deque_Node_Type*)Pointer_New_Node, 01 * sizeof(struct Deque_Node_Type));
 
    *Pointer_New_Node = *(struct Deque_Node_Type*&element;
 
    struct Deque_Node_Type* Pointer_Top_Header = (struct Deque_Node_Type*&Pointer_Deque->Top_Header;
    struct Deque_Node_Type* Pointer_Bottom_Header = (struct Deque_Node_Type*&Pointer_Deque->Bottom_Header;
 
    if (Is_Linked_Deque_Empty((struct Linked_Deque_Type*)Pointer_Deque)) {
 
        Pointer_New_Node->Pointer_Above = (struct Deque_Node_Type*)Pointer_Top_Header;
        Pointer_New_Node->Pointer_Below = (struct Deque_Node_Type*)Pointer_Bottom_Header;
        Pointer_Top_Header->Pointer_Below = (struct Deque_Node_Type*)Pointer_New_Node;
        Pointer_Bottom_Header->Pointer_Above = (struct Deque_Node_Type*)Pointer_New_Node;
 
        Pointer_Deque->Number_Of_Current_Element++;
        return TRUE;
    }
 
    Pointer_New_Node->Pointer_Above = (struct Deque_Node_Type*)Pointer_Bottom_Header->Pointer_Above;
    Pointer_New_Node->Pointer_Below = (struct Deque_Node_Type*)Pointer_Bottom_Header;
    Pointer_Bottom_Header->Pointer_Above = (struct Deque_Node_Type*)Pointer_New_Node;
    Pointer_New_Node->Pointer_Above->Pointer_Below = (struct Deque_Node_Type*)Pointer_New_Node;
 
    Pointer_Deque->Number_Of_Current_Element++;
    return TRUE;
}
 
struct Deque_Node_Type* Pop_In_The_Top_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque) {
 
    if ((struct Deque_Node_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Pop_In_The_Top_Linked_Deque()\n");
        return NULL;
    }
    if (Is_Linked_Deque_Empty ((struct Linked_Deque_Type*)Pointer_Deque)) {
 
        printf("Error! The linked deque was empty. Pop_In_The_Top_Linked_Deque()\n");
        return NULL;
    }
 
    struct Deque_Node_Type* Pointer_Top_Header = (struct Deque_Node_Type*&Pointer_Deque->Top_Header;
    struct Deque_Node_Type* Pointer_Bottom_Header = (struct Deque_Node_Type*&Pointer_Deque->Bottom_Header;
    struct Deque_Node_Type* Pointer_Return = (struct Deque_Node_Type*)Pointer_Top_Header->Pointer_Below;
    
    if ((struct Deque_Node_Type*)Pointer_Top_Header->Pointer_Below == (struct Deque_Node_Type*)Pointer_Bottom_Header->Pointer_Above) {
 
        Pointer_Top_Header->Pointer_Below = NULL;
        Pointer_Bottom_Header->Pointer_Above = NULL;
 
        Pointer_Deque->Number_Of_Current_Element--;
        return (struct Deque_Node_Type*) Pointer_Return;
    }
 
    Pointer_Top_Header->Pointer_Below = (struct Deque_Node_Type*)Pointer_Return->Pointer_Below;
    Pointer_Return->Pointer_Below->Pointer_Above = (struct Deque_Node_Type*)Pointer_Top_Header;
 
    Pointer_Deque->Number_Of_Current_Element--;
    return (struct Deque_Node_Type*) Pointer_Return;
}
 
struct Deque_Node_Type* Pop_In_The_Bottom_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque) {
 
    if ((struct Deque_Node_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Pop_In_The_Bottom_Linked_Deque()\n");
        return NULL;
    }
    if (Is_Linked_Deque_Empty((struct Linked_Deque_Type*)Pointer_Deque)) {
 
        printf("Error! The linked deque was empty. Pop_In_The_Bottom_Linked_Deque()\n");
        return NULL;
    }
 
    struct Deque_Node_Type* Pointer_Top_Header = (struct Deque_Node_Type*&Pointer_Deque->Top_Header;
    struct Deque_Node_Type* Pointer_Bottom_Header = (struct Deque_Node_Type*&Pointer_Deque->Bottom_Header;
    struct Deque_Node_Type* Pointer_Return = (struct Deque_Node_Type*)Pointer_Bottom_Header->Pointer_Above;
 
    if ((struct Deque_Node_Type*)Pointer_Top_Header->Pointer_Below == (struct Deque_Node_Type*)Pointer_Bottom_Header->Pointer_Above) {
 
        Pointer_Top_Header->Pointer_Below = NULL;
        Pointer_Bottom_Header->Pointer_Above = NULL;
 
        Pointer_Deque->Number_Of_Current_Element--;
        return (struct Deque_Node_Type*) Pointer_Return;
    }
 
    Pointer_Bottom_Header->Pointer_Above = (struct Deque_Node_Type*)Pointer_Return->Pointer_Above;
    Pointer_Return->Pointer_Above->Pointer_Below = (struct Deque_Node_Type*)Pointer_Bottom_Header;
 
    Pointer_Deque->Number_Of_Current_Element--;
    return (struct Deque_Node_Type*) Pointer_Return;
}
 
struct Deque_Node_Type* Peek_In_The_Top_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque) {
 
    if ((struct Deque_Node_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Peek_In_The_Top_Linked_Deque()\n");
        return NULL;
    }
    if (Is_Linked_Deque_Empty((struct Linked_Deque_Type*)Pointer_Deque)) {
 
        printf("Error! The linked deque was empty. Peek_In_The_Top_Linked_Deque()\n");
        return NULL;
    }
 
    struct Deque_Node_Type* Pointer_Return = (struct Deque_Node_Type*)Pointer_Deque->Top_Header.Pointer_Below;
 
    return (struct Deque_Node_Type*) Pointer_Return;
}
 
struct Deque_Node_Type* Peek_In_The_Bottom_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque) {
 
    if ((struct Deque_Node_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Peek_In_The_Bottom_Linked_Deque()\n");
        return NULL;
    }
    if (Is_Linked_Deque_Empty((struct Linked_Deque_Type*)Pointer_Deque)) {
 
        printf("Error! The linked deque was empty. Peek_In_The_Bottom_Linked_Deque()\n");
        return NULL;
    }
 
    struct Deque_Node_Type* Pointer_Return = (struct Deque_Node_Type*)Pointer_Deque->Bottom_Header.Pointer_Above;
 
    return (struct Deque_Node_Type*) Pointer_Return;
}
 
void Delete_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque) {
 
    if ((struct Deque_Node_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Delete_Linked_Deque()\n");
        return;
    }
 
    int loop = Get_Number_Of_Elements_In_The_Linked_Deque((struct Linked_Deque_Type*)Pointer_Deque);
    for (loop; loop; loop--) {
 
        struct Deque_Node_Type* Pointer_Delete_Node = (struct Deque_Node_Type*)Pointer_Deque->Top_Header.Pointer_Below;
        Pointer_Delete_Node->Pointer_Above->Pointer_Below = (struct Deque_Node_Type*)Pointer_Delete_Node->Pointer_Below;
        Pointer_Delete_Node->Pointer_Below->Pointer_Above = (struct Deque_Node_Type*)Pointer_Delete_Node->Pointer_Above;
        free((struct Deque_Node_Type*)Pointer_Delete_Node);
    }
    free((struct Deque_Node_Type*)Pointer_Deque);
 
    return;
}
 
void Display_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque) {
 
    if ((struct Deque_Node_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Display_Linked_Deque()\n");
        return;
    }
    if (Is_Linked_Deque_Empty((struct Linked_Deque_Type*)Pointer_Deque)) {
 
        printf("Error! The linked deque was empty. Display_Linked_Deque()\n");
        return;
    }
 
    printf("Number of elements : %d\n", Get_Number_Of_Elements_In_The_Linked_Deque((struct Linked_Deque_Type*)Pointer_Deque));
 
    struct Deque_Node_Type* Pointer_Current_Node = (struct Deque_Node_Type*)Pointer_Deque->Top_Header.Pointer_Below;
    int loop = Get_Number_Of_Elements_In_The_Linked_Deque((struct Linked_Deque_Type*)Pointer_Deque);
    for (loop; loop; loop--) {
 
        printf("[%c]\n", Pointer_Current_Node->data);
        Pointer_Current_Node = (struct Deque_Node_Type*)Pointer_Current_Node->Pointer_Below;
    }
    printf("\n\n");
 
    return;
}
 
int Get_Number_Of_Elements_In_The_Linked_Deque(struct Linked_Deque_Type* Pointer_Deque) {
 
    if ((struct Deque_Node_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Get_Number_Of_Elements_In_The_Linked_Deque()\n");
        return FALSE;
    }
    if (Is_Linked_Deque_Empty((struct Linked_Deque_Type*)Pointer_Deque)) {
 
        printf("Error! The linked deque was empty. Get_Number_Of_Elements_In_The_Linked_Deque()\n");
        return FALSE;
    }
 
    return Pointer_Deque->Number_Of_Current_Element;
}
 
int Is_Linked_Deque_Empty(struct Linked_Deque_Type* Pointer_Deque) {
 
    if ((struct Deque_Node_Type*)Pointer_Deque == NULL) {
 
        printf("Error! The linked deque was null. Is_Linked_Deque_Empty()\n");
        return FALSE;
    }
 
    if ((struct Deque_Node_Type*) Pointer_Deque->Top_Header.Pointer_Below == NULL
        && (struct Deque_Node_Type*) Pointer_Deque->Bottom_Header.Pointer_Above == NULLreturn TRUE;
    else return FALSE;
}
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
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
#include <stdio.h>
#include <stdlib.h>
#include "linked deque.h"
 
int main(int argc, char* argv[]) {
 
    typedef struct Linked_Deque_Type Linked_Deque;
    typedef struct Deque_Node_Type Deque_Node;
 
    Linked_Deque* Pointer_Deque = (Linked_Deque*)Create_Linked_Deque();
 
    Deque_Node node = { 0, };
    int loop = 0;
    for (loop = 0; loop < 10; loop++) {
 
        node.data = 'A' + loop;
        Push_In_The_Top_Linked_Deque((Linked_Deque*)Pointer_Deque, node);
    }
    for (loop = 0; loop < 9; loop++) {
 
        node.data = 'A' + loop;
        Push_In_The_Bottom_Linked_Deque((Linked_Deque*)Pointer_Deque, node);
    }
    Display_Linked_Deque((Linked_Deque*)Pointer_Deque);
 
    Deque_Node* Pointer_Node = NULL;
    for (loop = 0; loop < 5; loop++) {
 
        Pointer_Node = (Deque_Node*)Pop_In_The_Top_Linked_Deque((Linked_Deque*)Pointer_Deque);
        if ((Deque_Node*)Pointer_Node == NULLreturn -1;
        printf("[%c] ", Pointer_Node->data);
        free((Deque_Node*)Pointer_Node);
    }
    printf("\n");
    for (loop = 0; loop < 5; loop++) {
 
        Pointer_Node = (Deque_Node*)Pop_In_The_Bottom_Linked_Deque((Linked_Deque*)Pointer_Deque);
        if ((Deque_Node*)Pointer_Node == NULLreturn -1;
        printf("[%c] ", Pointer_Node->data);
        free((Deque_Node*)Pointer_Node);
    }
    printf("\n\n");
 
    Display_Linked_Deque((Linked_Deque*)Pointer_Deque);
 
    Pointer_Node = (Deque_Node*)Peek_In_The_Top_Linked_Deque((Linked_Deque*)Pointer_Deque);
    printf("[%c]\n", Pointer_Node->data);
    Pointer_Node = (Deque_Node*)Peek_In_The_Bottom_Linked_Deque((Linked_Deque*)Pointer_Deque);
    printf("[%c]\n\n", Pointer_Node->data);
 
    Display_Linked_Deque((Linked_Deque*)Pointer_Deque);
 
    Delete_Linked_Deque((Linked_Deque*)Pointer_Deque);
    system("pause");
 
    return 0;
}
cs

0 개의 댓글:

댓글 쓰기