doubly list
"Doubly_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 _DOUBLY_LIST_ #define _DOUBLY_LIST_ typedef struct Doubly_List_Node_Type { int data; struct Doubly_List_Node_Type* Pointer_Left_Link; struct Doubly_List_Node_Type* Pointer_Right_Link; } Doubly_List_Node; typedef struct Doubly_List_Type { int Current_Element_Count; Doubly_List_Node Header_Node; } Doubly_List; Doubly_List* Create_Doubly_List(void); int Add_Doubly_List_Element(Doubly_List* Pointer_List, int position, Doubly_List_Node element); int Remove_Doubly_List_Element(Doubly_List* Pointer_List, int position); void Delete_Doubly_List(Doubly_List* Pointer_List); int Get_Doubly_List_Length(Doubly_List* Pointer_List); Doubly_List_Node* Get_Doubly_List_Element(Doubly_List* Pointer_List, int position); void Display_Doubly_List(Doubly_List* Pointer_List); #endif // !_DOUBLY_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 | #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Doubly_List.h" Doubly_List* Create_Doubly_List(void) { Doubly_List* Pointer_Return = NULL; (Doubly_List*)Pointer_Return = (Doubly_List*)malloc(1 * sizeof(Doubly_List)); if ((Doubly_List*)Pointer_Return == NULL) { printf("Error! Dynamic memory allocation, Create_Doubly_List()\n"); return NULL; } (void*)memset((Doubly_List*)Pointer_Return, 0, sizeof(Doubly_List)); (Doubly_List*)Pointer_Return->Header_Node.Pointer_Left_Link = (Doubly_List*) &(Pointer_Return->Header_Node); (Doubly_List*)Pointer_Return->Header_Node.Pointer_Right_Link = (Doubly_List*) &(Pointer_Return->Header_Node); return (Doubly_List*)Pointer_Return; } int Add_Doubly_List_Element(Doubly_List* Pointer_List, int position, Doubly_List_Node element) { if ((Doubly_List*)Pointer_List == NULL) { printf("Error! The doubly list is null. Add_Doubly_List_Element()\n"); return FALSE; } else if (!(position >= 0 && position <= (Doubly_List*)Pointer_List->Current_Element_Count)) { printf("Error! Position index-[%d], Add_Doubly_List_Element()\n", position); return FALSE; } Doubly_List_Node* Pointer_New_Node = (Doubly_List_Node*)calloc(1, sizeof(Doubly_List_Node)); if ((Doubly_List_Node*)Pointer_New_Node == NULL) { printf("Error! Dynamic memory allocation, Add_Doubly_List()\n"); return FALSE; } Doubly_List_Node* Pointer_Present_Node = NULL; *(Doubly_List_Node*)Pointer_New_Node = element; (Doubly_List_Node*)Pointer_New_Node->Pointer_Left_Link = NULL; (Doubly_List_Node*)Pointer_New_Node->Pointer_Right_Link = NULL; (Doubly_List_Node*)Pointer_Present_Node = (Doubly_List*) &(Pointer_List->Header_Node); int loop = 0; for (loop = 0; loop < position; loop++) { (Doubly_List_Node*)Pointer_Present_Node = (Doubly_List_Node*)Pointer_Present_Node->Pointer_Right_Link; } (Doubly_List_Node*)Pointer_New_Node->Pointer_Left_Link = (Doubly_List_Node*)Pointer_Present_Node; (Doubly_List_Node*)Pointer_New_Node->Pointer_Right_Link = (Doubly_List_Node*)Pointer_Present_Node->Pointer_Right_Link; // efficiency, == (Doubly_List_Node*)Pointer_New_Node->Pointer_Right_Link = (Doubly_List_Node*)Pointer_Present_Node; (Doubly_List_Node*)Pointer_Present_Node->Pointer_Right_Link = (Doubly_List_Node*)Pointer_New_Node; (Doubly_List_Node*)Pointer_New_Node->Pointer_Right_Link->Pointer_Left_Link = (Doubly_List_Node*)Pointer_New_Node; (Doubly_List*)Pointer_List->Current_Element_Count++; return TRUE; } int Remove_Doubly_List_Element(Doubly_List* Pointer_List, int position) { int Array_Count = Get_Doubly_List_Length((Doubly_List*)Pointer_List); if ((Doubly_List*)Pointer_List == NULL) { printf("The doubly list is null. Remove_Doubly_List_Element()\n"); return FALSE; } else if (!(position >= 0 && position < Array_Count)) { printf("Error! Position index -[%d], Remove_Doubly_List_Element()\n"); return FALSE; } Doubly_List_Node* Pointer_Present_Node = (Doubly_List*) &(Pointer_List->Header_Node); Doubly_List_Node* Pointer_Delete_Node = NULL; int loop = 0; for (loop = 0; loop < position; loop++) { (Doubly_List_Node*)Pointer_Present_Node = (Doubly_List_Node*)Pointer_Present_Node->Pointer_Right_Link; } (Doubly_List_Node*)Pointer_Delete_Node = (Doubly_List_Node*)Pointer_Present_Node->Pointer_Right_Link; (Doubly_List_Node*)Pointer_Present_Node->Pointer_Right_Link = (Doubly_List_Node*)Pointer_Delete_Node->Pointer_Right_Link; (Doubly_List_Node*)Pointer_Delete_Node->Pointer_Right_Link->Pointer_Left_Link = (Doubly_List_Node*)Pointer_Present_Node; free(Pointer_Delete_Node); (Doubly_List*)Pointer_List->Current_Element_Count--; return TRUE; } void Delete_Doubly_List(Doubly_List* Pointer_List) { if ((Doubly_List*)Pointer_List == NULL) { printf("The doubly list is null. Delete_Doubly_List()\n"); return; } while ((Doubly_List*)Pointer_List->Current_Element_Count > 0) { Doubly_List_Node* Pointer_Present_Node = (Doubly_List*) &(Pointer_List->Header_Node); Doubly_List_Node* Pointer_Delete_Node = NULL; (Doubly_List_Node*)Pointer_Delete_Node = (Doubly_List_Node*)Pointer_Present_Node->Pointer_Right_Link; (Doubly_List_Node*)Pointer_Present_Node->Pointer_Right_Link = (Doubly_List_Node*)Pointer_Delete_Node->Pointer_Right_Link; (Doubly_List_Node*)Pointer_Delete_Node->Pointer_Right_Link->Pointer_Left_Link = (Doubly_List_Node*)Pointer_Present_Node; free((Doubly_List_Node*)Pointer_Delete_Node); (Doubly_List*)Pointer_List->Current_Element_Count--; } if ((Doubly_List*)Pointer_List != NULL) (Doubly_List*)Pointer_List = NULL; free((Doubly_List*)Pointer_List); return; } int Get_Doubly_List_Length(Doubly_List* Pointer_List) { if ((Doubly_List*)Pointer_List == NULL) { printf("Error! The doubly list is null. Get_Doubly_List_Length()\n"); return FALSE; } return (Doubly_List*)Pointer_List->Current_Element_Count; } Doubly_List_Node* Get_Doubly_List_Element(Doubly_List* Pointer_List, int position) { if ((Doubly_List*)Pointer_List == NULL) { printf("Error! The doubly list is null. Get_Doubly_List_Element()\n"); return NULL; } else if (!(position >= 0 && position < (Doubly_List*)Pointer_List->Current_Element_Count)) { printf("Error! Position index-[%d], Get_Doubly_List_Element()\n", position); return FALSE; } Doubly_List_Node* Pointer_Return = NULL; Doubly_List_Node* Pointer_Node = (Doubly_List*)Pointer_List->Header_Node.Pointer_Right_Link; int loop = 0; for (loop = 0; loop < position; loop++) { (Doubly_List_Node*)Pointer_Node = (Doubly_List_Node*)Pointer_Node->Pointer_Right_Link; } (Doubly_List_Node*)Pointer_Return = (Doubly_List_Node*)Pointer_Node; return (Doubly_List_Node*)Pointer_Return; } void Display_Doubly_List(Doubly_List* Pointer_List) { if ((Doubly_List*)Pointer_List == NULL) { printf("Error! The doubly list is null. Display_Doubly_List()\n"); return; } printf("Current node count : %d\n", (Doubly_List*)Pointer_List->Current_Element_Count); int index = 0; for (index = 0; index < (Doubly_List*)Pointer_List->Current_Element_Count; index++) { printf("[%d], %d\n", index, Get_Doubly_List_Element((Doubly_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 | #include <stdio.h> #include <stdlib.h> #include "Doubly_List.h" int main(int argc, char* argv[]) { Doubly_List* Pointer_List = Create_Doubly_List(); if ((Doubly_List*)Pointer_List == NULL) return -1; Doubly_List_Node node = { node.data = 0, node.Pointer_Left_Link = NULL, node.Pointer_Right_Link = NULL }; node.data = 1; Add_Doubly_List_Element((Doubly_List*)Pointer_List, 0, node); node.data = 3; Add_Doubly_List_Element((Doubly_List*)Pointer_List, 1, node); node.data = 5; Add_Doubly_List_Element((Doubly_List*)Pointer_List, 2, node); //Display_Doubly_List((Doubly_List*)Pointer_List); //Remove_Doubly_List_Element((Doubly_List*)Pointer_List, 0); Display_Doubly_List((Doubly_List*)Pointer_List); Delete_Doubly_List((Doubly_List*)Pointer_List); Display_Doubly_List((Doubly_List*)Pointer_List); return 0; } | cs |
0 개의 댓글:
댓글 쓰기