Circular list _Alpha
"circular 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 | #ifndef _CIRCULAR_LIST_ #define _CIRCULAR_LIST_ struct Circular_List_Node_Type { int data; struct Circular_List_Node_Type* Pointer_Link; }; struct Circular_List_Type { int Current_Element_Count; struct Circular_List_Node_Type* Pointer_Link; }; struct Circular_List_Type* Create_Circular_List(void); void Delete_Circular_List(struct Circular_List_Type* Pointer_List); void Delete_Circular_List_Alpha(struct Circular_List_Type* Pointer_List); int Add_Circular_List_Element(struct Circular_List_Type* Pointer_List, int position, struct Circular_List_Node_Type element); int Remove_Circular_List_Element(struct Circular_List_Type* Pointer_List, int position); void Clear_Circular_List(struct Circular_List_Type* Pointer_List); int Get_Circular_List_Length(struct Circular_List_Type* Pointer_List); struct Circular_List_Node_Type* Get_Circular_List_Element(struct Circular_List_Type* Pointer_List, int position); void Display_Circular_List(struct Circular_List_Type* Pointer_List); #endif // !_CIRCULAR_LIST_ #ifndef _COMMON_LIST_DEFAULT_ #define _COMMON_LIST_DEFAULT_ #define TRUE 1 #define FALSE 0 #endif // !_COMMON_LIST_DEFAULT_ | cs |
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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "circular list.h" struct Circular_List_Type* Create_Circular_List(void) { struct Circular_List_Type* Pointer_Return = NULL; Pointer_Return = (struct Circular_List_Type*) malloc(1 * sizeof(struct Circular_List_Type)); if (!(struct Circular_List_Type*)Pointer_Return) { printf("Error! The dynamic memory allocation failed. :( Create_Circular_List()\n"); return NULL; } (void*)memset((struct Circular_List_Type*)Pointer_Return, 0, 1 * sizeof(struct Circular_List_Type)); return (struct Circular_List_Type*)Pointer_Return; } int Add_Circular_List_Element(struct Circular_List_Type* Pointer_List, int position, struct Circular_List_Node_Type element) { if (!(struct Circular_List_Type*)Pointer_List) { printf("Error! The circular list was empty. Add_Circular_List_Element()\n"); return FALSE; } if (!(position >= 0 && position <= Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List))) { printf("Error! The position index was invalided. Add_Circular_List_Element()\n"); printf("Index : %d\n", position); return FALSE; } struct Circular_List_Node_Type* Pointer_New_Node = NULL; Pointer_New_Node = (struct Circular_List_Node_Type*) malloc(1 * sizeof(struct Circular_List_Node_Type)); if (!(struct Circular_List_Node_Type*) Pointer_New_Node) { printf("Error! The dynamic memory allocation failed. :( Add_Circular_List_Element()\n"); return FALSE; } *Pointer_New_Node = *(struct Circular_List_Node_Type*) &element; Pointer_New_Node->Pointer_Link = NULL; if (position == 0 && Pointer_List->Current_Element_Count == 0) { Pointer_List->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_New_Node; Pointer_New_Node->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_New_Node; Pointer_List->Current_Element_Count++; return TRUE; } else if (position == 0 && Pointer_List->Current_Element_Count != 0) { struct Circular_List_Node_Type* Pointer_Last_Node = (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link; while ((struct Circular_List_Node_Type*)Pointer_Last_Node->Pointer_Link != (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link) { Pointer_Last_Node = (struct Circular_List_Node_Type*)Pointer_Last_Node->Pointer_Link; } Pointer_List->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_New_Node; Pointer_New_Node->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_Last_Node->Pointer_Link; Pointer_Last_Node->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_New_Node; Pointer_List->Current_Element_Count++; return TRUE; } struct Circular_List_Node_Type* Pointer_Previous_Node = NULL; Pointer_Previous_Node = (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link; int loop = 0; for (loop = 0; loop < position - 1; loop++) { Pointer_Previous_Node = (struct Circular_List_Node_Type*)Pointer_Previous_Node->Pointer_Link; } Pointer_New_Node->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_Previous_Node->Pointer_Link; Pointer_Previous_Node->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_New_Node; Pointer_List->Current_Element_Count++; return TRUE; } int Remove_Circular_List_Element(struct Circular_List_Type* Pointer_List, int position) { if (!(struct Circular_List_Type*)Pointer_List) { printf("Error! The circular list was empty. Remove_Circular_List_Element()\n"); return FALSE; } if (!(position >= 0 && position < Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List))) { printf("Error! The position index was invalided. Remove_Circular_List_Element()\n"); printf("Index : %d\n", position); return FALSE; } struct Circular_List_Node_Type* Pointer_Delete_Node = NULL; if (position == 0 && Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List) == 1) { Pointer_Delete_Node = (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link; free((struct Circular_List_Node_Type*)Pointer_Delete_Node); Pointer_List->Pointer_Link = NULL; Pointer_List->Current_Element_Count--; return TRUE; } else if (position == 0 && Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List) != 1) { Pointer_Delete_Node = (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link; struct Circular_List_Node_Type* Pointer_Last_Node = (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link; while ((struct Circular_List_Node_Type*)Pointer_Last_Node->Pointer_Link != (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link) { Pointer_Last_Node = (struct Circular_List_Node_Type*)Pointer_Last_Node->Pointer_Link; } Pointer_Last_Node->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_Delete_Node->Pointer_Link; Pointer_List->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_Delete_Node->Pointer_Link; free((struct Circular_List_Node_Type*)Pointer_Delete_Node); Pointer_List->Current_Element_Count--; return TRUE; } struct Circular_List_Node_Type* Pointer_Previous_Node = NULL; Pointer_Previous_Node = (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link; int loop = 0; for (loop = 0; loop < position - 1; loop++) { Pointer_Previous_Node = (struct Circular_List_Node_Type*)Pointer_Previous_Node->Pointer_Link; } Pointer_Delete_Node = (struct Circular_List_Node_Type*)Pointer_Previous_Node->Pointer_Link; Pointer_Previous_Node->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_Delete_Node->Pointer_Link; free((struct Circular_List_Node_Type*)Pointer_Delete_Node); Pointer_List->Current_Element_Count--; return TRUE; } struct Circular_List_Node_Type* Get_Circular_List_Element(struct Circular_List_Type* Pointer_List, int position) { if (!(struct Circular_List_Type*)Pointer_List) { printf("Error! The circular list was empty. Get_Circular_List_Element()\n"); return NULL; } if (!(position >= 0 && position < Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List))) { printf("Error! The position index was invalied. Get_Circular_List_Element()\n"); printf("Index : %d\n", position); return NULL; } struct Circular_List_Node_Type* Pointer_Node = NULL; Pointer_Node = (struct Circular_List_Node_Type*)Pointer_List->Pointer_Link; int loop = 0; for (loop = 0; loop < position; loop++) { Pointer_Node = (struct Circular_List_Node_Type*) Pointer_Node->Pointer_Link; } return (struct Circular_List_Node_Type*) Pointer_Node; } void Display_Circular_List(struct Circular_List_Type* Pointer_List) { if (!(struct Circular_List_Type*)Pointer_List) { printf("Error! There is no element. :( Display_Circular_List()\n"); return; } printf("Number of current element : %d\n", Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List)); int index = 0; for (index = 0; index < Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List); index++) { printf("[%d], %d\n", index, Get_Circular_List_Element((struct Circular_List_Type*)Pointer_List, index)->data); } return; } int Get_Circular_List_Length(struct Circular_List_Type* Pointer_List) { if (!(struct Circular_List_Type*)Pointer_List) { printf("Error! The circular list was empty. Get_Circular_List_Length()\n"); return 0; } return Pointer_List->Current_Element_Count; } void Delete_Circular_List_Alpha(struct Circular_List_Type* Pointer_List) { if (!(struct Circular_List_Type*)Pointer_List) { printf("Error! The circular list was empty. Delete_Circular_List_Alpha()\n"); return; } if (!Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List)) { printf("Error! The number of current element is zero. Delete_Circular_List_Alpha()\n"); return; } struct Circular_List_Type* Pointer_Head = (struct Circular_List_Type*) Pointer_List; struct Circular_List_Node_Type* Pointer_Delete_Node = NULL; int loop = 0; for (loop = Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List) - 1; loop; loop--) { struct Circular_List_Node_Type* Pointer_Last_Node = (struct Circular_List_Node_Type*)Pointer_Head->Pointer_Link; while ((struct Circular_List_Node_Type*)Pointer_Last_Node->Pointer_Link != (struct Circular_List_Node_Type*)Pointer_Head->Pointer_Link) { Pointer_Last_Node = (struct Circular_List_Node_Type*)Pointer_Last_Node->Pointer_Link; } Pointer_Delete_Node = (struct Circular_List_Node_Type*)Pointer_Head->Pointer_Link; Pointer_Last_Node->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_Delete_Node->Pointer_Link; Pointer_Head->Pointer_Link = (struct Circular_List_Node_Type*)Pointer_Delete_Node->Pointer_Link; free((struct Circular_List_Node_Tpe*)Pointer_Delete_Node); Pointer_Head->Current_Element_Count--; } Pointer_Delete_Node = (struct Circular_List_Node_Type*)Pointer_Head->Pointer_Link; free((struct Circular_List_Node_Type*)Pointer_Delete_Node); Pointer_Head->Current_Element_Count--; free((struct Circular_List_Node*)Pointer_Head); return; } void Delete_Circular_List(struct Circular_List_Type* Pointer_List) { if (!(struct Circular_List_Type*) Pointer_List) { printf("Error! The circular list was empty. Delete_Circular_List()\n"); return; } Clear_Circular_List((struct Circular_List_Type*)Pointer_List); free((struct Circular_List_Type*)Pointer_List); return; } void Clear_Circular_List(struct Circular_List_Type* Pointer_List) { if (!(struct Circular_List_Type*)Pointer_List) { printf("Error! The circular list was empty. Clear_Circular_List()\n"); return; } while (Get_Circular_List_Length((struct Circular_List_Type*)Pointer_List) > 0) { Remove_Circular_List_Element((struct Circular_List_Type*)Pointer_List, 0); } return; } | cs |
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 | #include <stdio.h> #include <stdlib.h> #include "circular list.h" int main(int argc, char** argv) { typedef struct Circular_List_Type Circular_List; typedef struct Circular_List_Node_Type Circular_List_Node; Circular_List* Pointer_List = (Circular_List*)Create_Circular_List(); if ((Circular_List*)Pointer_List == NULL) return -1; Circular_List_Node node; node.data = 1; Add_Circular_List_Element((Circular_List*)Pointer_List, 0, node); node.data = 3; Add_Circular_List_Element((Circular_List*)Pointer_List, 1, node); node.data = 5; Add_Circular_List_Element((Circular_List*)Pointer_List, 2, node); node.data = 7; Add_Circular_List_Element((Circular_List*)Pointer_List, 3, node); node.data = 9; Add_Circular_List_Element((Circular_List*)Pointer_List, 4, node); Display_Circular_List((Circular_List*)Pointer_List); Remove_Circular_List_Element((Circular_List*)Pointer_List, 0); Display_Circular_List((Circular_List*)Pointer_List); Delete_Circular_List_Alpha((Circular_List*)Pointer_List); system("pause"); return 0; } | cs |
0 개의 댓글:
댓글 쓰기