Linked list (rewrite)
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 | #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node_Data_Type { char data; }; struct List_Node_Type { struct Node_Data_Type Node_Data; struct List_Node_Type* Pointer_Next; }; struct Linked_List_Type { int Current_Element_Count; struct List_Node_Type Header_Node; }; struct Linked_List_Type* Create_Linked_List (void); void Insert_Linked_List_Element (struct Linked_List_Type* Pointer_List, struct List_Node_Type element); void Delete_Linked_List (struct Linked_List_Type* Pointer_List); void Display_Linked_List (struct Linked_List_Type* Pointer_List); int main(int argc, char** argv) { struct Linked_List_Type* Pointer_List = NULL; struct List_Node_Type node = { node.Node_Data.data = 0, node.Pointer_Next = NULL }; Pointer_List = (struct Linked_List_Type*) Create_Linked_List (); if (Pointer_List == NULL) return -1; node.Node_Data.data = 'A'; Insert_Linked_List_Element ((struct Linked_List_Type*) Pointer_List, node); node.Node_Data.data = 'B'; Insert_Linked_List_Element ((struct Linked_List_Type*) Pointer_List, node); node.Node_Data.data = 'C'; Insert_Linked_List_Element ((struct Linked_List_Type*) Pointer_List, node); Display_Linked_List ((struct Linked_List_Type*) Pointer_List); Delete_Linked_List ((struct Linked_List_Type*) Pointer_List); Pointer_List = NULL; return 0; } void Display_Linked_List (struct Linked_List_Type* Pointer_List) { if ((struct Linked_List_Type*) Pointer_List == NULL) { printf ("Error! The linked list is null. Display_Linked_List()\n"); return; } struct List_Node_Type* Pointer_Previous_Node = &Pointer_List->Header_Node; int index = 0; for (index = 0; index < Pointer_List->Current_Element_Count; index++) { Pointer_Previous_Node = Pointer_Previous_Node->Pointer_Next; printf ("[%c] - ", Pointer_Previous_Node->Node_Data.data); } printf ("NULL\n"); return; } void Delete_Linked_List (struct Linked_List_Type* Pointer_List) { struct List_Node_Type* Pointer_Previous_Node = &Pointer_List->Header_Node; Pointer_Previous_Node = Pointer_Previous_Node->Pointer_Next; int index = 0; for (index = 0; index < Pointer_List->Current_Element_Count; index++) { struct List_Node_Type* Pointer_Delete_Node = Pointer_Previous_Node; Pointer_Previous_Node = Pointer_Delete_Node->Pointer_Next; free ((struct List_Node_Type*)Pointer_Delete_Node); } free ((struct List_Node_Type*)Pointer_Previous_Node); return; } void Insert_Linked_List_Element (struct Linked_List_Type* Pointer_List, struct List_Node_Type element) { if ((struct Linked_List_Type*)Pointer_List == NULL) { printf ("Error! The linked list is null. Insert_Linked_List_Element()\n"); return; } struct List_Node_Type* Pointer_New_Node = (struct List_Node_Type*) malloc (1 * sizeof (struct List_Node_Type)); if ((struct List_Node_Type*) Pointer_New_Node == NULL) { printf ("Error! Dynamic memory allocation failed()\n"); return; } *Pointer_New_Node = element; Pointer_New_Node->Pointer_Next = NULL; struct List_Node_Type* Pointer_Previous_Node = &Pointer_List->Header_Node; Pointer_New_Node->Pointer_Next = Pointer_Previous_Node->Pointer_Next; Pointer_Previous_Node->Pointer_Next = Pointer_New_Node; Pointer_List->Current_Element_Count++; return; } struct Linked_List_Type* Create_Linked_List (void) { struct Linked_List_Type* Pointer_Return = (struct Linked_List_Type*) malloc (1 * sizeof (struct Linked_List_Type)); if ((struct Linked_List_Type*)Pointer_Return == NULL) { printf ("Error! Dynamic memory allocation failed. Create_Linked_List()\n"); return NULL; } (void*) memset ((struct Linked_List_Type*) Pointer_Return, 0, 1 * sizeof (struct Linked_List_Type)); return (struct Linked_List_Type*) Pointer_Return; } | cs |
0 개의 댓글:
댓글 쓰기