circular list
"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 | #ifndef _CIRCULAR_LIST_ #define _CIRCULAR_LIST_ typedef struct Circular_List_Node_Type { int data; struct Circular_List_Node_Type* Pointer_Next; } Circular_List_Node; typedef struct Circular_List_Type { int Current_Element_Count; Circular_List_Node* Pointer_Next; } Circular_List; Circular_List* Create_Circular_List(void); int Add_Circular_List_Element(Circular_List* Pointer_List, const int position, const Circular_List_Node element); int Remove_Circular_List_Element(Circular_List* Pointer_List, const int position); Circular_List_Node* Get_Circular_List_Element(Circular_List* Pointer_List, const int position); int Get_Circular_List_Length(Circular_List* Pointer_List); void Delete_Circular_List(Circular_List* Pointer_List); void Display_Circular_List(Circular_List* 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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Circular_List.h" Circular_List* Create_Circular_List(void) { Circular_List* Pointer_Return = NULL; (Circular_List*)Pointer_Return = (Circular_List*)malloc(1 * sizeof(Circular_List)); if ((Circular_List*)Pointer_Return == NULL) { printf("Error! Memory allocation, Create_Circular_List()\n"); return NULL; } (void*)memset((Circular_List*)Pointer_Return, 0, sizeof(Circular_List)); return (Circular_List*)Pointer_Return; } int Add_Circular_List_Element(Circular_List* Pointer_List, int position, Circular_List_Node element) { if ((Circular_List*)Pointer_List == NULL) { printf("Error! The circular list is null. Add_Circular_List_Element()\n"); return FALSE; } else if (!(position >= 0 && position <= (Circular_List*)Pointer_List->Current_Element_Count)) { printf("Error! Position index-[%d] Add_Circular_List_Element()\n", position); return FALSE; } Circular_List_Node* Pointer_New_Node = (Circular_List_Node*)calloc(1, sizeof(Circular_List_Node)); Circular_List_Node* Pointer_Present_Node = NULL,* Pointer_Last_Node = NULL; if ((Circular_List_Node*)Pointer_New_Node == NULL) { printf("Error! Memory allocation, Add_Circular_List_Element()\n"); return FALSE; } *(Circular_List_Node*)Pointer_New_Node = element; (Circular_List_Node*)Pointer_New_Node->Pointer_Next = NULL; if (position != 0) { (Circular_List_Node*)Pointer_Present_Node = (Circular_List*)Pointer_List->Pointer_Next; int index = 0; for (index = 0; index < position - 1; index++) { (Circular_List_Node*)Pointer_Present_Node = (Circular_List_Node*)Pointer_Present_Node->Pointer_Next; } (Circular_List_Node*)Pointer_New_Node->Pointer_Next = (Circular_List_Node*)Pointer_Present_Node->Pointer_Next; (Circular_List_Node*)Pointer_Present_Node->Pointer_Next = (Circular_List_Node*)Pointer_New_Node; (Circular_List*)Pointer_List->Current_Element_Count++; return TRUE; } else if ((Circular_List*)Pointer_List->Current_Element_Count == 0) { (Circular_List*)Pointer_List->Pointer_Next = (Circular_List_Node*)Pointer_New_Node; (Circular_List_Node*)Pointer_New_Node->Pointer_Next = (Circular_List_Node*)Pointer_New_Node; (Circular_List*)Pointer_List->Current_Element_Count++; return TRUE; } (Circular_List_Node*)Pointer_Last_Node = (Circular_List*)Pointer_List->Pointer_Next; while ((Circular_List_Node*)Pointer_Last_Node->Pointer_Next != (Circular_List*)Pointer_List->Pointer_Next) { (Circular_List_Node*)Pointer_Last_Node = (Circular_List_Node*)Pointer_Last_Node->Pointer_Next; } (Circular_List*)Pointer_List->Pointer_Next = (Circular_List_Node*)Pointer_New_Node; (Circular_List_Node*)Pointer_New_Node->Pointer_Next = (Circular_List_Node*)Pointer_Last_Node->Pointer_Next; (Circular_List_Node*)Pointer_Last_Node->Pointer_Next = (Circular_List_Node*)Pointer_New_Node; (Circular_List*)Pointer_List->Current_Element_Count++; return TRUE; } int Remove_Circular_List_Element(Circular_List* Pointer_List, int position) { int Array_Count = (int) Get_Circular_List_Length((Circular_List*)Pointer_List); if ((Circular_List*)Pointer_List == NULL) { printf("Error! The circular list is null. Remove_Circular_List_Element()\n"); return FALSE; } else if (!(position >= 0 && position < Array_Count)) { printf("Error! Position index-[%d] Remove_Circular_List_Element()\n", position); return FALSE; } Circular_List_Node* Pointer_Present_Node = NULL, *Pointer_Delete_Node = NULL, *Pointer_Last_Node = NULL; if (position != 0) { (Circular_List_Node*)Pointer_Present_Node = (Circular_List*)Pointer_List->Pointer_Next; int index = 0; for (index = 0; index < position - 1; index++) { (Circular_List_Node*)Pointer_Present_Node = (Circular_List_Node*)Pointer_Present_Node->Pointer_Next; } (Circular_List_Node*)Pointer_Delete_Node = (Circular_List_Node*)Pointer_Present_Node->Pointer_Next; (Circular_List_Node*)Pointer_Present_Node->Pointer_Next = (Circular_List_Node*)Pointer_Delete_Node->Pointer_Next; free(Pointer_Delete_Node); (Circular_List*)Pointer_List->Current_Element_Count--; return TRUE; } else if (Array_Count == 1) { // Last, free() (Circular_List_Node*)Pointer_Delete_Node = (Circular_List*)Pointer_List->Pointer_Next; free((Circular_List_Node*)Pointer_Delete_Node); (Circular_List*)Pointer_List->Current_Element_Count--; free((Circular_List*)Pointer_List); return TRUE; } (Circular_List_Node*)Pointer_Delete_Node = (Circular_List*)Pointer_List->Pointer_Next; (Circular_List_Node*)Pointer_Last_Node = (Circular_List*)Pointer_List->Pointer_Next; while ((Circular_List_Node*)Pointer_Last_Node->Pointer_Next != (Circular_List*)Pointer_List->Pointer_Next) { (Circular_List_Node*)Pointer_Last_Node = (Circular_List_Node*)Pointer_Last_Node->Pointer_Next; } (Circular_List_Node*)Pointer_Last_Node->Pointer_Next = (Circular_List_Node*)Pointer_Delete_Node->Pointer_Next; (Circular_List*)Pointer_List->Pointer_Next = (Circular_List_Node*)Pointer_Delete_Node->Pointer_Next; free((Circular_List_Node*)Pointer_Delete_Node); (Circular_List*)Pointer_List->Current_Element_Count--; return TRUE; } Circular_List_Node* Get_Circular_List_Element(Circular_List* Pointer_List, int position) { if ((Circular_List*)Pointer_List == NULL) { printf("Error! The circular list is null. Get_Circular_List_Element()\n"); return NULL; } else if (!(position >= 0 && position < (Circular_List*)Pointer_List->Current_Element_Count)) { printf("Error! Position index-[%d], Get_Circular_List_Element()\n", position); return NULL; } Circular_List_Node* Pointer_Node = NULL; (Circular_List_Node*)Pointer_Node = (Circular_List*)Pointer_List->Pointer_Next; int loop = 0; for (loop = 0; loop < position; loop++) { (Circular_List_Node*)Pointer_Node = (Circular_List_Node*)Pointer_Node->Pointer_Next; } return (Circular_List_Node*)Pointer_Node; } int Get_Circular_List_Length(Circular_List* Pointer_List) { if ((Circular_List*)Pointer_List == NULL) { printf("Error! The circular list is null. Get_Circular_List_Length()\n"); return FALSE; } return (Circular_List*)Pointer_List->Current_Element_Count; } void Delete_Circular_List(Circular_List* Pointer_List) { if ((Circular_List*)Pointer_List == NULL) { printf("Error!, The circular list is null. Delete_Circular_List()\n"); return; } else if (!((Circular_List*)Pointer_List->Current_Element_Count > 0)) { printf("Error! There is no circular list. Delete_Circular_List()\n"); return; } Circular_List_Node* Pointer_Present_Node = NULL; Circular_List_Node* Pointer_Delete_Node = NULL; int index = 0, Array_Count = Get_Circular_List_Length((Circular_List*)Pointer_List); for (index = 0; index < Array_Count; index++) { (Circular_List_Node*)Pointer_Present_Node = (Circular_List*)Pointer_List->Pointer_Next; (Circular_List_Node*)Pointer_Delete_Node = (Circular_List_Node*)Pointer_Present_Node->Pointer_Next; (Circular_List_Node*)Pointer_Present_Node->Pointer_Next = (Circular_List_Node*)Pointer_Delete_Node->Pointer_Next; free((Circular_List_Node*)Pointer_Delete_Node); } (Circular_List*)Pointer_List->Current_Element_Count = 0; if ((Circular_List*)Pointer_List != NULL) (Circular_List*)Pointer_List = NULL; free((Circular_List*)Pointer_List); return; } void Display_Circular_List(Circular_List* Pointer_List) { if ((Circular_List*)Pointer_List->Current_Element_Count < 1) { printf("Error! The circular list is null. Display_Circular_List()\n"); return; } printf("Current element count : %d\n", (Circular_List*)Pointer_List->Current_Element_Count); int index = 0; for (index = 0; index < (Circular_List*)Pointer_List->Current_Element_Count; index++) { printf("[%d], %d\n", index, Get_Circular_List_Element((Circular_List*)Pointer_List, index)->data); } 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 | #include <stdio.h> #include <stdlib.h> #include "Circular_List.h" int main(void) { Circular_List* Pointer_List = NULL; Pointer_List = Create_Circular_List(); if (Pointer_List == NULL) return -1; Circular_List_Node node = { node.data = 0, node.Pointer_Next = NULL }; node.data = 1; Add_Circular_List_Element(Pointer_List, 0, node); node.data = 3; Add_Circular_List_Element(Pointer_List, 1, node); node.data = 5; Add_Circular_List_Element(Pointer_List, 2, node); Display_Circular_List(Pointer_List); Remove_Circular_List_Element(Pointer_List, 0); int index = 0, Array_Count = Get_Circular_List_Length(Pointer_List); for (index = 0; index < Array_Count; index++) { Circular_List_Node* Pointer_Value = Get_Circular_List_Element(Pointer_List, index); printf("Position [%d]-%d\n", index, Pointer_Value->data); } Delete_Circular_List(Pointer_List); if ((Circular_List*)Pointer_List != NULL) (Circular_List*)Pointer_List = NULL; free((Circular_List*)Pointer_List); return 0; } | cs |
0 개의 댓글:
댓글 쓰기