Array list
"arraylist.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 160 161 162 163 164 165 166 167 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "arraylist.h" struct Array_List_Type* Create_Array_List(int Maximum_Element_Count) { if (Maximum_Element_Count < 0) { printf("Error! The Maximum number of elements must be greater then zero. Create_Array_List()\n"); return NULL; } struct Array_List_Type* Pointer_Return = NULL; (struct Array_List_Type*)Pointer_Return = (struct Array_List_Type*)malloc(1 * sizeof(struct Array_List_Type)); if ((struct Array_List_Type*)Pointer_Return == NULL) { printf("Error! Dynamic memory allocation failed(1). :( Create_Array_List()\n"); return NULL; } Pointer_Return->Maximum_Element_Count = Maximum_Element_Count; Pointer_Return->Current_Element_Count = 0; (struct Array_List_Node_Type*)Pointer_Return->Pointer_Element = NULL; (struct Array_List_Node_Type*)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 == NULL) { printf("Error! Dynamic memory allocation failed(2). :( Create_Array_List()\n"); free((struct Array_List_Type*)Pointer_Return); 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 == NULL) { printf("Error! The array list is null. Add_Array_List_Element()\n"); return FALSE; } else if (Is_Array_List_Full((struct Array_List_Type*)Pointer_List)) { printf("Error! Exceeded capacity of a array list. -[%d]/[%d]\n", position, Pointer_List->Maximum_Element_Count); return FALSE; } else if (!(position >= 0 && position <= Pointer_List->Current_Element_Count)) { printf("Error! Position index-[%d] out of range. Add_Array_List_Element()\n", position); return FALSE; } int index = 0; for (index = Pointer_List->Current_Element_Count - 1; index >= position; index--) { *(struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + index + 1) = *(struct Array_List_Node_Type*)(Pointer_List->Pointer_Element + index); } *(struct Array_List_Node_Type*)(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 == NULL) { printf("Error! The array list is null. Remove_Array_List()\n"); return FALSE; } else if (!(position >= 0 && position < Pointer_List->Current_Element_Count)) { printf("Error! Position index-[%d] out of range. Remove_Array_List()\n"); return FALSE; } int index = 0; for (index = position; index < Pointer_List->Current_Element_Count - 1; index++) { *(struct Array_List_Node_Type*)(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 == NULL) { printf("Error! The array list is null. Get_Array_List_Element()\n"); return FALSE; } else if (!(position >= 0 && position < Get_Array_List_Length((struct Array_List_Type*)Pointer_List))) { printf("Error! Position index-[%d] out of range. Get_Array_List_Element()\n"); return FALSE; } struct Array_List_Node_Type* Pointer_Return = NULL; (struct Array_List_Node_Type*)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 == NULL) { printf("Error! The array list is null. Display_Array_List()\n"); return; } printf("Number of Maximum element : %d\n", Pointer_List->Maximum_Element_Count); printf("Number of Current element : %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 == NULL) { printf("Error! The array list is null. Is_Array_List_Full()\n"); return FALSE; } else if (Pointer_List->Current_Element_Count != Pointer_List->Maximum_Element_Count) { return FALSE; } else return TRUE; } int Get_Array_List_Length(struct Array_List_Type* Pointer_List) { if ((struct Array_List_Type*)Pointer_List == NULL) { printf("Error! The array list is null. Get_Array_List_Length()\n"); return 0; } else return Pointer_List->Current_Element_Count; } void Delete_Array_List(struct Array_List_Type* Pointer_List) { if ((struct Array_List_Type*)Pointer_List == NULL) { printf("Error! The array list is null. Delete_Array_List()\n"); 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 | #include <stdio.h> #include <stdlib.h> #include "arraylist.h" int main(int argc, char** argv) { int size = 6; struct Array_List_Type* Pointer_List = NULL; (struct Array_List_Type*)Pointer_List = (struct Array_List_Type*)Create_Array_List(size); if ((struct Array_List_Type*)Pointer_List == NULL) return -1; struct Array_List_Node_Type node; node.data = 1; Add_Array_List_Element((struct Array_List_Type*)Pointer_List, 0, node); node.data = 5; Add_Array_List_Element((struct Array_List_Type*)Pointer_List, 1, node); node.data = 9; Add_Array_List_Element((struct Array_List_Type*)Pointer_List, 2, node); node.data = 11; Add_Array_List_Element((struct Array_List_Type*)Pointer_List, 3, node); Display_Array_List((struct Array_List_Type*)Pointer_List); Remove_Array_List_Element((struct Array_List_Type*)Pointer_List, 3 - 1); Display_Array_List((struct Array_List_Type*)Pointer_List); printf("\n"); int Array_Count = Get_Array_List_Length((struct Array_List_Type*)Pointer_List), index = 0; for (index = 0; index < Array_Count; index++) { printf("Position[%d]-[%d]\n", index, Get_Array_List_Element((struct Array_List_Type*)Pointer_List, index)->data); } Delete_Array_List((struct Array_List_Type*)Pointer_List); (struct Array_List_Type*)Pointer_List = NULL; Display_Array_List((struct Array_List_Type*)Pointer_List); return 0; } | cs |
0 개의 댓글:
댓글 쓰기