linked list _ Add Reverse_Node()
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 | #include <stdio.h> #include <stdlib.h> #include <string.h> struct Node { int identity; int data; struct Node* next; }; struct Node* Create_Node(void); void Add_First(struct Node* target, int _identity, int data); void Remove_First(struct Node* target); struct Node* Reverse_Node(struct Node* head); struct Node* Search_Node(struct Node* head, int Key); int main(void) { struct Node* head = NULL; head = Create_Node(); head->next = NULL; Add_First(head, 201801, 10); Add_First(head, 201802, 20); Add_First(head, 201803, 30); Add_First(head, 201804, 40); Add_First(head, 201805, 50); struct Node* current = head->next; while (current != NULL) { printf("ID : %d\n", current->identity); printf("Data : %d\n\n", current->data); current = current->next; } head = Reverse_Node(head); printf("\n== Reverse node. ==\n\n"); current = head->next; while (current != NULL) { printf("ID : %d\n", current->identity); printf("Data : %d\n\n", current->data); current = current->next; } struct Node* Search_Target = Search_Node(head, 201802); if (Search_Target == NULL) { printf("Error!, Not Find :(\n"); } else { printf("\n== Search ==\n"); printf("ID : %d\n", Search_Target->identity); printf("Data : %d\n", Search_Target->data); } Search_Target = head->next; while (Search_Target != NULL) { struct Node* next = Search_Target->next; free(Search_Target); Search_Target = next; } free(head); return 0; } struct Node* Reverse_Node(struct Node* head) { struct Node* tail = NULL; struct Node* current = NULL; struct Node* _head = NULL; _head = head->next; while (_head != NULL) { current = _head->next; _head->next = tail; tail = _head; _head = current; } // head head->next = tail; tail = head; return tail; } struct Node* Search_Node(struct Node* head, int Key) { struct Node* current = head->next; while (current != NULL) { if (current->identity == Key) return current; current = current->next; } return NULL; } void Remove_First(struct Node* target) { struct Node* Remove_Node = target->next; target->next; free(Remove_Node); return; } void Add_First(struct Node* target, int _identity, int data) { struct Node* New_Node = NULL; New_Node = Create_Node(); New_Node->next = target->next; // data New_Node->identity = _identity; // ID New_Node->data = data; target->next = New_Node; return; } struct Node* Create_Node(void) { struct Node* Pointer_Return = NULL; Pointer_Return = (struct Node*) calloc(1, sizeof(struct Node)); if (Pointer_Return != NULL) { (void)memset(Pointer_Return, 0, sizeof(struct Node)); } else { printf("Error!, Memory allocation Create_Node()\n"); return NULL; } return Pointer_Return; } | cs |
0 개의 댓글:
댓글 쓰기