linked list (origin)
"Linked_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 _LINKED_LIST_ #define _LINKED_LIST_ typedef struct List_Node_Type { int data; struct List_Node_Type* Pointer_Next; } List_Node; typedef struct Linked_List_Type { int Current_Element_Count; List_Node Header_Node; } Linked_List; Linked_List* Create_Linked_List(void); int Add_Linked_List_Element(Linked_List* Pointer_List, const int position, const List_Node element); int Remove_Linked_List_Element(Linked_List* Pointer_List, const int position); List_Node* Get_Linked_List_Element(Linked_List* Pointer_List, const int position); //void Clear_Linked_List(Linked_List* Pointer_List); int Get_Linked_List_Length(Linked_List* Pointer_List); void Delete_Linked_List(Linked_List* Pointer_List); void Display_Linked_List(Linked_List* Pointer_List); #endif // !_LINKED_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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Linked_List.h" Linked_List* Create_Linked_List(void) { Linked_List* Pointer_Return = NULL; (Linked_List*)Pointer_Return = (Linked_List*)malloc(1 * sizeof(Linked_List)); if (Pointer_Return == NULL) { printf("Error! Dynamic memory allocation, Create_Linked_List()\n"); return NULL; } (void*)memset(Pointer_Return, 0, sizeof(Linked_List)); return Pointer_Return; } int Add_Linked_List_Element(Linked_List* Pointer_List, const int position, const List_Node element) { if ((Linked_List*)Pointer_List == NULL) { printf("Error!, The linked list is null. Add_Linked_List_Element()\n"); return FALSE; } if (!(position >= 0 && position <= (Linked_List*)Pointer_List->Current_Element_Count)) { printf("Error!, Position index-[%d], Add_Linked_List_Element()\n", position); return FALSE; } List_Node* Pointer_Present_Node = NULL; List_Node* Pointer_New_Node = NULL; (List_Node*)Pointer_New_Node = (List_Node*)calloc(1, sizeof(List_Node)); if ((List_Node*)Pointer_New_Node == NULL) { printf("Error!, Dynamic memory allocation, Add_Linked_List_Element()\n"); return FALSE; } *(List_Node*)(Pointer_New_Node) = element; // type casting.. how? (List_Node*)Pointer_New_Node->Pointer_Next = NULL; (List_Node*)Pointer_Present_Node = (List_Node*) &(Pointer_List->Header_Node); int loop = 0; for (loop = 0; loop < position; loop++) { (List_Node*)Pointer_Present_Node = (List_Node*)Pointer_Present_Node->Pointer_Next; } (List_Node*)Pointer_New_Node->Pointer_Next = (List_Node*)Pointer_Present_Node->Pointer_Next; (List_Node*)Pointer_Present_Node->Pointer_Next = (List_Node*)Pointer_New_Node; (Linked_List*)Pointer_List->Current_Element_Count++; return TRUE; } int Remove_Linked_List_Element(Linked_List* Pointer_List, const int position) { if ((Linked_List*)Pointer_List == NULL) { printf("Error!, The linked list is null. Remove_Linked_Element()\n"); return FALSE; } int Array_Count = Get_Linked_List_Length((Linked_List*)Pointer_List); if (!(position >= 0 && position < Array_Count)) { printf("Error! Position index - [%d] Remove_Linked_List_Element()\n", position); return FALSE; } List_Node* Pointer_Present_Node = NULL; List_Node* Pointer_Delete_Node = NULL; (List_Node*)Pointer_Present_Node = (List_Node*) &(Pointer_List->Header_Node); int loop = 0; for (loop = 0; loop < position; loop++) { (List_Node*)Pointer_Present_Node = (List_Node*)Pointer_Present_Node->Pointer_Next; } (List_Node*)Pointer_Delete_Node = (List_Node*)Pointer_Present_Node->Pointer_Next; (List_Node*)Pointer_Present_Node->Pointer_Next = (List_Node*)Pointer_Delete_Node->Pointer_Next; // free memory if ((List_Node*)Pointer_Delete_Node != NULL) (List_Node*)Pointer_Delete_Node = NULL; free((List_Node*)Pointer_Delete_Node); (Linked_List*)Pointer_List->Current_Element_Count--; return TRUE; } List_Node* Get_Linked_List_Element(Linked_List* Pointer_List, const int position) { if ((Linked_List*)Pointer_List == NULL) { printf("Error! The linked list is null. Get_Linked_List_Element()\n"); return NULL; } if (!(position >= 0 && position < (Linked_List*)Pointer_List->Current_Element_Count)) { printf("Error! Position index - [%d] Get_Linked_List_Element()\n", position); return NULL; } List_Node* Pointer_Present_Node = NULL; (List_Node*)Pointer_Present_Node = (List_Node*) &(Pointer_List->Header_Node); int loop = 0; for (loop = 0; loop <= position; loop++) { (List_Node*)Pointer_Present_Node = (List_Node*)Pointer_Present_Node->Pointer_Next; } return (List_Node*)Pointer_Present_Node; } void Delete_Linked_List(Linked_List* Pointer_List) { if ((Linked_List*)Pointer_List == NULL) { printf("Error! The linked list is null. Delete_Linked_List()\n"); return; } else if (!((Linked_List*)Pointer_List->Current_Element_Count > 0)) { printf("Error! There is no Pointer_List. Pointer_List()\n"); return; } List_Node* Pointer_Present_Node = NULL; List_Node* Pointer_Delete_Node = NULL; (List_Node*)Pointer_Present_Node = (List_Node*) &(Pointer_List->Header_Node); int index = 0, Number_Of_Element = (List_Node*)Pointer_List->Current_Element_Count; for (index = 0; index < Number_Of_Element; index++) { (List_Node*)Pointer_Delete_Node = (List_Node*)Pointer_Present_Node->Pointer_Next; (List_Node*)Pointer_Present_Node->Pointer_Next = (List_Node*)Pointer_Delete_Node->Pointer_Next; // free memory if ((List_Node*)Pointer_Delete_Node != NULL) (List_Node*)Pointer_Delete_Node = NULL; free((List_Node*)Pointer_Delete_Node); (Linked_List*)Pointer_List->Current_Element_Count--; } if ((List_Node*)Pointer_Present_Node != NULL) (List_Node*) Pointer_Present_Node = NULL; free((List_Node*)Pointer_Present_Node); return; } int Get_Linked_List_Length(Linked_List* Pointer_List) { if ((Linked_List*)Pointer_List == NULL) { printf("Error! The linked list is null. Get_Linked_List_Length()\n"); return FALSE; } return (Linked_List*)Pointer_List->Current_Element_Count; } int Is_Empty(Linked_List* Pointer_List) { if ((Linked_List*)Pointer_List == NULL) { printf("Error! The linked list is null. Is_Empty()\n"); return FALSE; } else if ((Linked_List*)Pointer_List->Current_Element_Count != 0) { printf("Error! There is not zero. Is_Empty()\n"); return FALSE; } printf("Empty Node.\n"); return TRUE; } void Display_Linked_List(Linked_List* Pointer_List) { if ((Linked_List*)Pointer_List == NULL) { printf("No Element! Display_Linked_List()\n"); return; } printf("Current element count : %d\n", (Linked_List*)Pointer_List->Current_Element_Count); int index = 0; for (index = 0; index < (Linked_List*)Pointer_List->Current_Element_Count; index++) { printf("[%d], %d\n", index, Get_Linked_List_Element((Linked_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 | #include <stdio.h> #include <stdlib.h> #include "Linked_List.h" int main(void) { Linked_List* Pointer_List = NULL; List_Node node = { node.data = 0, node.Pointer_Next = NULL }; // Dynamic Memory allocation (Linked_List*)Pointer_List = (Linked_List*)Create_Linked_List(); if (Pointer_List == NULL) return -1; (int)node.data = 1; (int)Add_Linked_List_Element((Linked_List*)Pointer_List, 0, node); (int)node.data = 3; (int)Add_Linked_List_Element((Linked_List*)Pointer_List, 1, node); (int)node.data = 5; (int)Add_Linked_List_Element((Linked_List*)Pointer_List, 2, node); Display_Linked_List((Linked_List*)Pointer_List); (int)Remove_Linked_List_Element((Linked_List*)Pointer_List, 1); Display_Linked_List((Linked_List*)Pointer_List); Delete_Linked_List((Linked_List*)Pointer_List); Display_Linked_List((Linked_List*)Pointer_List); (int)Is_Empty((Linked_List*)Pointer_List); return 0; } | cs |
0 개의 댓글:
댓글 쓰기