Array list _Alpha
"array 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 | #ifndef _ARRAY_LIST_ #define _ARRAY_LIST_ struct Array_List_Node_Type { int data; }; struct Array_List_Type { int Maximum_Element_Count; int Current_Element_Count; struct Array_List_Node_Type* Pointer_Element; }; struct Array_List_Type* Create_Array_List(int Maximum_Element_Count); void Delete_Array_List(struct Array_List_Type* Pointer_List); int Is_Array_List_Full(struct Array_List_Type* Pointer_List); int Add_Array_List_Element(struct Array_List_Type* Pointer_List, int position, struct Array_List_Node_Type element); int Remove_Array_List_Element(struct Array_List_Type* Pointer_List, int position); struct Array_List_Node_Type* Get_Array_List_Element(struct Array_List_Type* Pointer_List, int position); void Display_Array_List(struct Array_List_Type* Pointer_List); int Get_Array_List_Length(struct Array_List_Type* Pointer_List); #endif // !_ARRAY_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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "array list.h" struct Array_List_Type* Create_Array_List(int Maximum_Element_Count) { if (Maximum_Element_Count < 1) { printf("Error!, The maximum number of elements must be zero or more"); return NULL; } struct Array_List_Type* Pointer_Return = NULL; Pointer_Return = (struct Array_List_Type*) malloc(1 * sizeof(struct Array_List_Type)); if (!(struct Array_List_Type*) Pointer_Return) { printf("Error!, The dynamic memory allocation was failed(1). Create_Array_List()\n"); return NULL ; } Pointer_Return->Maximum_Element_Count = Maximum_Element_Count; Pointer_Return->Current_Element_Count = 0; Pointer_Return->Pointer_Element = NULL; Pointer_Return->Pointer_Element = (struct Array_List_Node_Type*) malloc(Maximum_Element_Count * sizeof(struct Array_List_Node_Type)); if (!(struct Array_List_Node_Type*)Pointer_Return->Pointer_Element) { printf("Error!, The dynamic memory allocation was failed(2). Create_Array_List()\n"); return NULL; } (void*)memset((struct Array_List_Node_Type*)Pointer_Return->Pointer_Element, 0, Maximum_Element_Count * sizeof(struct Array_List_Node_Type)); return (struct Array_List_Type*) Pointer_Return; } int Add_Array_List_Element(struct Array_List_Type* Pointer_List, int position, struct Array_List_Node_Type element) { if (!(struct Array_List_Type*)Pointer_List) { printf("Error!, The list was null. Add_Array_List_Element()\n"); return FALSE; } if (Is_Array_List_Full((struct Array_List_Type*)Pointer_List)) { printf("Error!, The list exceeded tolerance. Add_Array_List_Element()\n"); return FALSE; } if (!(position >= 0 && position <= Pointer_List->Current_Element_Count)) { printf("Error!, The list position exceeded tolerance. Index number is [%d]. Add_Array_List_Element()\n", position); return FALSE; } int index = 0; for (index = Pointer_List->Current_Element_Count - 1; index >= position; index--) { *(Pointer_List->Pointer_Element + index + 1) = *(struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + index); } *(Pointer_List->Pointer_Element + position) = *(struct Array_List_Node_Type*) &element; Pointer_List->Current_Element_Count++; return TRUE; } int Remove_Array_List_Element(struct Array_List_Type* Pointer_List, int position) { if (!(struct Array_List_Type*)Pointer_List) { printf("The list was null. Remove_Array_List_Element()\n"); return FALSE; } if (!(position >= 0 && position < Pointer_List->Current_Element_Count)) { printf("Error!, The list position exceeded tolerance. Index number is [%d]. Remover_Array_List_Element()\n", position); return FALSE; } int index = 0; for (index = position; index < Pointer_List->Current_Element_Count - 1; index++) { *(Pointer_List->Pointer_Element + index) = *(struct Array_List_Node_Type*) (Pointer_List->Pointer_Element + index + 1); } Pointer_List->Current_Element_Count--; return TRUE; } struct Array_List_Node_Type* Get_Array_List_Element(struct Array_List_Type* Pointer_List, int position) { if (!(struct Array_List_Type*)Pointer_List) { printf("Error! The list was null. Get_Array_list_Element()\n"); return FALSE; } if (!(position >= 0 && position < Get_Array_List_Length((struct Array_List_Type*)Pointer_List))) { printf("Error! The list position exceeded tolerance. Index number is [%d]. Get_Array_List_Element()\n", position); return FALSE; } struct Array_List_Node_Type* Pointer_Return = NULL; Pointer_Return = (struct Array_List_Node_Type*) Pointer_List->Pointer_Element + position; return (struct Array_List_Node_Type*)Pointer_Return; } void Display_Array_List(struct Array_List_Type* Pointer_List) { if (!(struct Array_List_Type*)Pointer_List) { printf("The list was null. Display_Array_List()\n"); return; } printf("Maximum number of elements : %d\n", Pointer_List->Maximum_Element_Count); printf("Current number of elements : %d\n", Pointer_List->Current_Element_Count); int index = 0; for (index = 0; index < Pointer_List->Current_Element_Count; index++) { printf("[%d], %d\n", index, Get_Array_List_Element((struct Array_List_Type*)Pointer_List, index)->data); } for (index = Pointer_List->Current_Element_Count; index < Pointer_List->Maximum_Element_Count; index++) { printf("[%d], Empty\n", index); } return; } int Is_Array_List_Full(struct Array_List_Type* Pointer_List) { if (!(struct Array_List_Type*)Pointer_List) return FALSE; else if (Pointer_List->Current_Element_Count == Pointer_List->Maximum_Element_Count) return TRUE; else return FALSE; } int Get_Array_List_Length(struct Array_List_Type* Pointer_List) { if (!(struct Array_List_Type*)Pointer_List) return 0; return Pointer_List->Current_Element_Count; } void Delete_Array_List(struct Array_List_Type* Pointer_List) { if (!(struct Array_List_Type*) Pointer_List) return; free((struct Array_List_Node_Type*)Pointer_List->Pointer_Element); free((struct Array_List_Type*)Pointer_List); 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 40 41 42 43 44 45 46 47 48 49 | #include <stdio.h> #include <stdlib.h> #include "array list.h" int main(int argc, char** argv) { typedef struct Array_List_Type Array_List; typedef struct Array_List_Node_Type Array_List_Node; Array_List* Pointer_List = (Array_List*)Create_Array_List(6); if (!(Array_List*)Pointer_List) { return -1; } Array_List_Node node; node.data = 1; (int)Add_Array_List_Element((Array_List*)Pointer_List, 0, node); node.data = 3; (int)Add_Array_List_Element((Array_List*)Pointer_List, 1, node); node.data = 5; (int)Add_Array_List_Element((Array_List*)Pointer_List, 2, node); Display_Array_List((Array_List*)Pointer_List); Remove_Array_List_Element((Array_List*)Pointer_List, 1); Display_Array_List((Array_List*)Pointer_List); node.data = 7; (int)Add_Array_List_Element((Array_List*)Pointer_List, 2, node); Display_Array_List((Array_List*)Pointer_List); int Array_Count = (int)Get_Array_List_Length((Array_List*)Pointer_List); int index = 0; for (index = 0; index < Array_Count; index++) { Array_List_Node* Pointer_Value = (Array_List_Node*)Get_Array_List_Element((Array_List*)Pointer_List, index); printf("index-[%d]-%d\n", index, Pointer_Value->data); } Delete_Array_List((Array_List*)Pointer_List); system("pause"); return 0; } | cs |
0 개의 댓글:
댓글 쓰기