Step by step

My diary

...

Search

breakinformation. Powered by Blogger.

2019년 1월 11일 금요일

linked double ended queue


"linkeddoublequeue.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
42
#ifndef _LINKED_DOUBLE_QUEUE_
#define _LINKED_DOUBLE_QUEUE_
 
struct Double_Ended_Queue_Node_Type {
 
    char data;
    struct Double_Ended_Queue_Node_Type* Pointer_Right_Link;
    struct Double_Ended_Queue_Node_Type* Pointer_Left_Link;
};
 
struct Linked_Double_Ended_Queue_Type {
 
    int Current_Element_Count;
    struct Double_Ended_Queue_Node_Type* Pointer_Front_Node;
    struct Double_Ended_Queue_Node_Type* Pointer_Rear_Node;
};
 
typedef struct Double_Ended_Queue_Node_Type        Double_Ended_Queue_Node;
typedef struct Linked_Double_Ended_Queue_Type    Linked_Double_Ended_Queue;
 
Linked_Double_Ended_Queue* Create_Linked_Double_Ended_Queue(void);
int Insert_Front_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue, Double_Ended_Queue_Node element);
int Insert_Rear_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue, Double_Ended_Queue_Node element);
Double_Ended_Queue_Node* Delete_Front_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue);
Double_Ended_Queue_Node* Delete_Rear_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue);
Double_Ended_Queue_Node* Peek_Front_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue);
Double_Ended_Queue_Node* Peek_Rear_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue);
void Delete_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue);
int Is_Linked_Double_Ended_Queue_Full(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue);
int Is_Linked_Double_Ended_Queue_Empty(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue);
 
#endif // !_LINKED_DOUBLE_QUEUE_
 
#ifndef _COMMON_QUEUE_DEFAULT_
#define _COMMON_QUEUE_DEFAULT_
 
#define TRUE    1
#define FALSE    0
 
#endif // !_COMMON_QUEUE_DEFAULT_
 
 
cs
"linkeddoublequeue.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
#include <stdio.h>
#include <stdlib.h>
#include "linkeddoublequeue.h"
 
Linked_Double_Ended_Queue* Create_Linked_Double_Ended_Queue(void) {
 
    Linked_Double_Ended_Queue* Pointer_Return = (Linked_Double_Ended_Queue*)malloc(1 * sizeof(Linked_Double_Ended_Queue));
    if ((Linked_Double_Ended_Queue*)Pointer_Return == NULL) {
 
        printf("Error! Dynamic memory allocation. Create_Linked_Double_Queue()\n");
        return NULL;
    }
    (void*)memset((Linked_Double_Ended_Queue*)Pointer_Return, 0sizeof(Linked_Double_Ended_Queue));
 
    return (Linked_Double_Ended_Queue*)Pointer_Return;
}
 
int Insert_Front_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue, Double_Ended_Queue_Node element) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULL) {
 
        printf("Error! The Double Queue is null. Insert_Front_Linked_Double_Queue()\n");
        return FALSE;
    }
 
    Double_Ended_Queue_Node* Pointer_Node = NULL;
    (Double_Ended_Queue_Node*)Pointer_Node = (Double_Ended_Queue_Node*)malloc(1 * sizeof(Double_Ended_Queue_Node));
    if ((Double_Ended_Queue_Node*)Pointer_Node == NULL) {
 
        printf("Error! Dynamic memory allocation. Insert_Front_Linked_Double_Queue()\n");
        return FALSE;
    }
 
    *(Double_Ended_Queue_Node*)Pointer_Node = *(Double_Ended_Queue_Node*)&element;
    (struct Double_Ended_Queue_Node_Type*)Pointer_Node->Pointer_Right_Link = NULL;
    (struct Double_Ended_Queue_Node_Type*)Pointer_Node->Pointer_Left_Link = NULL;
 
    if (Is_Linked_Double_Ended_Queue_Empty((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue) == TRUE) {
 
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Front_Node = (struct Double_Ended_Queue_Node_Type*)Pointer_Node;
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Rear_Node = (struct Double_Ended_Queue_Node_Type*)Pointer_Node;
    }
    else {
 
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Front_Node->Pointer_Left_Link = (struct Double_Ended_Queue_Node_Type*)Pointer_Node;
        (struct Double_Ended_Queue_Node_Type*)Pointer_Node->Pointer_Right_Link = (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Front_Node;
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Front_Node = (struct Double_Ended_Queue_Node_Type*)Pointer_Node;
    }
    Pointer_Double_Ended_Queue->Current_Element_Count++;
 
    return TRUE;
}
 
int Insert_Rear_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue, Double_Ended_Queue_Node element) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULL) {
 
        printf("Error! The linked double queue is null. Insert_Rear_Linked_Double_Queue()\n");
        return FALSE;
    }
 
    Double_Ended_Queue_Node* Pointer_Node = (Double_Ended_Queue_Node*)malloc(1 * sizeof(Double_Ended_Queue_Node));
    if ((Double_Ended_Queue_Node*)Pointer_Node == NULL) {
 
        printf("Error! Dynamic memory allocation. Insert_Rear_Linked_Doiuble_Queue()\n");
        return FALSE;
    }
 
    *(Double_Ended_Queue_Node*)Pointer_Node = *(Double_Ended_Queue_Node*)&element;
    (struct Double_Ended_Queue_Node_Type*)Pointer_Node->Pointer_Right_Link = NULL;
    (struct Double_Ended_Queue_Node_Type*)Pointer_Node->Pointer_Left_Link = NULL;
 
    if (Is_Linked_Double_Ended_Queue_Empty((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue) == TRUE) {
 
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Front_Node = (struct Double_Ended_Queue_Node_Type*)Pointer_Node;
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Rear_Node = (struct Double_Ended_Queue_Node_Type*)Pointer_Node;
    }
    else {
 
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Rear_Node->Pointer_Right_Link = (struct Double_Ended_Queue_Node_Type*)Pointer_Node;
        (struct Double_Ended_Queue_Node_Type*)Pointer_Node->Pointer_Left_Link = (struct Double_Ended_Queue_Node_Type*) Pointer_Double_Ended_Queue->Pointer_Rear_Node;
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Rear_Node = (struct Double_Ended_Queue_Node_Type*)Pointer_Node;
    }
 
    Pointer_Double_Ended_Queue->Current_Element_Count++;
 
    return TRUE;
}
 
Double_Ended_Queue_Node* Delete_Front_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULL) {
 
        printf("Error! The linked double queue is null. Delete_Front_Linked_Double_Queue()\n");
        return FALSE;
    }
    else if (Is_Linked_Double_Ended_Queue_Empty((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue) == TRUE) {
 
        printf("Error! The linked double queue is zero. Delete_Front_Linked_Double_Queue()\n");
        return FALSE;
    }
 
    Double_Ended_Queue_Node* Pointer_Return = NULL;
    (Double_Ended_Queue_Node*)Pointer_Return = (Double_Ended_Queue_Node*) Pointer_Double_Ended_Queue->Pointer_Front_Node;
    (struct Double_Ended_Queue_Node_Type*) Pointer_Double_Ended_Queue->Pointer_Front_Node = (struct Double_Ended_Queue_Node_Type*) Pointer_Double_Ended_Queue->Pointer_Front_Node->Pointer_Right_Link;
    (struct Double_Ended_Queue_Node_Type*)Pointer_Return->Pointer_Right_Link = NULL;
 
    Pointer_Double_Ended_Queue->Current_Element_Count--;
    if (Is_Linked_Double_Ended_Queue_Empty((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue) == TRUE) {
 
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Rear_Node = NULL;
    }
    else (struct Double_Ended_Queue_Node_Type*) Pointer_Double_Ended_Queue->Pointer_Front_Node->Pointer_Left_Link = NULL;
 
    return (Double_Ended_Queue_Node*)Pointer_Return;
}
 
Double_Ended_Queue_Node* Delete_Rear_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULL) {
 
        printf("Error! The linked double queue is null. Delete_Rear_Linked_Double_Queue()\n");
        return FALSE;
    }
    else if (Is_Linked_Double_Ended_Queue_Empty((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue) == TRUE) {
 
        printf("Error! The linked double queue is zero. Delete_Rear_Linked_Double_Queue()\n");
        return FALSE;
    }
 
    Double_Ended_Queue_Node* Pointer_Return = NULL;
    (Double_Ended_Queue_Node*)Pointer_Return = (Double_Ended_Queue_Node*) Pointer_Double_Ended_Queue->Pointer_Rear_Node;
    (struct Double_Ended_Queue_Node_Type*) Pointer_Double_Ended_Queue->Pointer_Rear_Node = (struct Double_Ended_Queue_Node_Type*) Pointer_Double_Ended_Queue->Pointer_Rear_Node->Pointer_Left_Link;
    (struct Double_Ended_Queue_Node_Type*)Pointer_Return->Pointer_Left_Link = NULL;
 
    Pointer_Double_Ended_Queue->Current_Element_Count--;
    if (Is_Linked_Double_Ended_Queue_Empty((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue) == TRUE) {
 
        (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Front_Node = NULL;
    }
    else (struct Double_Ended_Queue_Node_Type*) Pointer_Double_Ended_Queue->Pointer_Rear_Node->Pointer_Right_Link = NULL;
 
    return (Double_Ended_Queue_Node*)Pointer_Return;
}
 
Double_Ended_Queue_Node* Peek_Front_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULL) {
 
        printf("Error! The linked double queue is null. Peek_Front_Linked_Double_Queue()\n");
        return NULL;
    }
    else if (Is_Linked_Double_Ended_Queue_Empty((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue) == TRUE) return NULL;
 
    return (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Front_Node;
}
 
Double_Ended_Queue_Node* Peek_Rear_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULL) {
 
        printf("Error! The linked double queue is null. Peek_Rear_Linked_Double_Queue()\n");
        return NULL;
    }
    else if (Is_Linked_Double_Ended_Queue_Empty((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue) == TRUE) return NULL;
 
    return (struct Double_Ended_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Rear_Node;
}
 
void Delete_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULLreturn;
 
    Double_Ended_Queue_Node* Pointer_Node = (Double_Ended_Queue_Node*)Pointer_Double_Ended_Queue->Pointer_Front_Node;
    while (Pointer_Node != NULL) {
 
        Double_Ended_Queue_Node* Pointer_Delete = (Double_Ended_Queue_Node*)Pointer_Node;
        (Double_Ended_Queue_Node*)Pointer_Node = (Double_Ended_Queue_Node*)Pointer_Node->Pointer_Right_Link;
        free((Double_Ended_Queue_Node*)Pointer_Delete);
    }
    free((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
 
    return;
}
 
int Is_Linked_Double_Ended_Queue_Full(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue) {
 
    return FALSE;
}
 
int Is_Linked_Double_Ended_Queue_Empty(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULLreturn FALSE;
    else if (Pointer_Double_Ended_Queue->Current_Element_Count != 0return FALSE;
    return TRUE;
}
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include "linkeddoublequeue.h"
 
int Insert_Front_Linked_Double_Ended_Queue_Character(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue, char data);
int Insert_Rear_Linked_Double_Ended_Queue_Character(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue, char data);
void Display_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue);
 
int main(int argc, char** argv) {
 
    Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue = Create_Linked_Double_Ended_Queue();
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULLreturn -1;
 
    Insert_Front_Linked_Double_Ended_Queue_Character((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue, 'A');
    Insert_Front_Linked_Double_Ended_Queue_Character((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue, 'B');
    Insert_Rear_Linked_Double_Ended_Queue_Character((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue, 'C');
    Insert_Rear_Linked_Double_Ended_Queue_Character((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue, 'D');
 
    Display_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
 
    Double_Ended_Queue_Node* Pointer_Node = (Double_Ended_Queue_Node*)Delete_Rear_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
    if ((Double_Ended_Queue_Node*)Pointer_Node != NULL) {
 
        printf("Delete_Rear_Linked_Double_Queue Value-[%c]\n", Pointer_Node->data);
        free((Double_Ended_Queue_Node*)Pointer_Node);
    }
    Display_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
 
    (Double_Ended_Queue_Node*)Pointer_Node = Peek_Front_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
    if (Pointer_Double_Ended_Queue != NULL) {
 
        printf("Peek_Front_Linked_Double_Ended_Queue Value-[%c]\n", Pointer_Node->data);
    }
    Display_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
 
    (Double_Ended_Queue_Node*)Pointer_Node = Delete_Front_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
    if (Pointer_Node != NULL) {
 
        printf("Delete_Front_Linked_Double_Queue Value-[%c]\n", Pointer_Node->data);
        free((Double_Ended_Queue_Node*)Pointer_Node);
    }
    Display_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
 
    Delete_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue);
    (Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULL;
 
    return 0;
}
 
int Insert_Front_Linked_Double_Ended_Queue_Character(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue, char data) {
 
    Double_Ended_Queue_Node node = { 0, };
    node.data = data;
    return Insert_Front_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue, node);
}
int Insert_Rear_Linked_Double_Ended_Queue_Character(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue, char data) {
 
    Double_Ended_Queue_Node node = { 0, };
    node.data = data;
    return Insert_Rear_Linked_Double_Ended_Queue((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue, node);
}
void Display_Linked_Double_Ended_Queue(Linked_Double_Ended_Queue* Pointer_Double_Ended_Queue) {
 
    if ((Linked_Double_Ended_Queue*)Pointer_Double_Ended_Queue == NULLreturn;
 
    printf("Current node count : %d\n", Pointer_Double_Ended_Queue->Current_Element_Count);
    
    Double_Ended_Queue_Node* Pointer_Node = (struct Double_Queue_Node_Type*)Pointer_Double_Ended_Queue->Pointer_Front_Node;
    int loop = 0;
    while ((Double_Ended_Queue_Node*)Pointer_Node != NULL) {
 
        printf("[%d]-[%c]\n", loop++, Pointer_Node->data);
        (Double_Ended_Queue_Node*)Pointer_Node = (struct Double_Queue_Node_Type*)Pointer_Node->Pointer_Right_Link;
    }
 
    return;
}
cs

0 개의 댓글:

댓글 쓰기