linked list
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 | #include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { int identity; char* name; int Korean; int English; int Math; struct Student* next; }; struct Student* Create_Node(void); void Add_First(struct Student* target, int _identity, char* _name, int _Korean, int _English, int _Math); void Remove_First(struct Student* target); struct Student* Search_Node(struct Student* target, int Key); int main(void) { struct Student* information = NULL; information = Create_Node(); information->next = NULL; Add_First(information, 1, "Stive", 90, 95, 85); Add_First(information, 2, "Elris", 85, 90, 80); Add_First(information, 3, "Mike", 80, 85, 75); Add_First(information, 4, "Aris", 75, 80, 70); Add_First(information, 5, "Ava", 70, 75, 55); struct Student* Search_Target = Search_Node(information, 2); if (Search_Target == NULL) { printf("Error!, Not Find :(\n"); } else { printf("ID : %d\n", Search_Target->identity); printf("Name : %s\n", Search_Target->name); printf("Korean score : %d\n", Search_Target->Korean); printf("English score : %d\n", Search_Target->English); printf("Math score : %d\n", Search_Target->Math); int total = Search_Target->Korean + Search_Target->English + Search_Target->Math; printf ("\nTotal score : %d\n", total); printf ("Average score : %.2f\n", total / 3.f); } Search_Target = information->next; while (Search_Target != NULL) { struct Student* next = Search_Target->next; free(Search_Target); Search_Target = next; } free(information); return 0; } struct Student* Search_Node(struct Student* head, int Key) { struct Student* current = head->next; while (current != NULL) { if (current->identity == Key) return current; current = current->next; } return NULL; } void Remove_First(struct Student* target) { struct Student* Remove_Node = target->next; target->next; free(Remove_Node); return; } void Add_First(struct Student* target, int _identity, char* _name, int _Korean, int _English, int _Math) { struct Student* New_Node = (struct Student*) calloc(1, sizeof(struct Student)); New_Node->next = target->next; // data New_Node->identity = _identity; // ID New_Node->name = _name; New_Node->Korean = _Korean; New_Node->English = _English; New_Node->Math = _Math; target->next = New_Node; return; } struct Student* Create_Node(void) { struct Student* Pointer_Return = NULL; Pointer_Return = (struct Student*) calloc(1, sizeof(struct Student)); if (Pointer_Return != NULL) { (void)memset(Pointer_Return, 0, sizeof(struct Student)); } else { printf("Error!, Memory allocation Create_Node()\n"); return NULL; } return Pointer_Return; } | cs |
0 개의 댓글:
댓글 쓰기