#define _CRT_SECURE_NO_WARNINGS
ÀÌ°ÍÀ» ¸Ç À§¿¡ ³Ö¾î¾ß ÄÄÆÄÀÏ ¿¡·¯°¡ ¹ß»ýÇÏÁö ¾ÊÀ½
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 | #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct student_info { char name[10]; int height; float weight; } element; typedef struct ListNode { // ³ëµå ŸÀÔÀ» ±¸Á¶Ã¼·Î Á¤ÀÇÇÑ´Ù. element data; struct ListNode *link; } ListNode; ListNode* insert_first(ListNode *head, element value) { ListNode *p = (ListNode *)malloc(sizeof(ListNode));//(1) p->data = value; // (2) p->link = head; //(3) head = p; //(4) return head; } // ³ëµå pre µÚ¿¡ »õ·Î¿î ³ëµå »ðÀÔ ListNode* insert(ListNode *head, ListNode *pre, element value) { ListNode *p = (ListNode *)malloc(sizeof(ListNode)); //(1) p->data = value; //(2) p->link = pre->link; //(3) pre->link = p; //(4) return head; //(5) } ListNode* delete_first(ListNode *head) { ListNode *removed; if (head == NULL) return NULL; removed = head; // (1) head = removed->link; // (2) free(removed); // (3) return head; // (4) } // pre°¡ °¡¸®Å°´Â ³ëµåÀÇ ´ÙÀ½ ³ëµå¸¦ »èÁ¦ÇÑ´Ù. ListNode* delete(ListNode *head, ListNode *pre) { ListNode *removed; removed = pre->link; pre->link = removed->link; // (2) free(removed); // (3) return head; // (4) } void print_list(ListNode *head) { for (ListNode *p = head; p != NULL; p = p->link) printf("(%s %3d %4.1f)->", p->data.name, p->data.height, p->data.weight); printf("\n"); } element* new_element(char *name, int height, float weight) { element *p = (element *)malloc(sizeof(element)); //(1) strcpy(p->name, name); p->height = height; p->weight = weight; return p; } ListNode* find_node(ListNode *head,char *name) { for (ListNode *p = head; p != NULL; p = p->link) if (!strcmp(p->data.name, name)) return p; return NULL; } ListNode* insert_last(ListNode *head, element value) { ListNode *p = (ListNode *)malloc(sizeof(ListNode)); ListNode *t; p->data = value; if (head == NULL) { p->link = head; head = p; } else { for (t = head; t->link != NULL; t = t->link); t->link = p; p->link = NULL; } return head; } // Å×½ºÆ® ÇÁ·Î±×·¥ int main(void) { ListNode *head = NULL, *pre; element* new; new = new_element("È«±æµ¿", 167, 72.5); head = insert_first(head, *new); new = new_element("À¯°ü¼ø", 163, 58.4); head = insert_first(head, *new); new = new_element("±èÀ¯½Å", 159, 70.8); head = insert_first(head, *new); print_list(head); head = delete_first(head); print_list(head); pre = find_node(head, "À¯°ü¼ø"); if (pre != NULL) { new = new_element("À̼ø½Å", 172, 83.4); head = insert(head, pre, *new); } print_list(head); pre = find_node(head, "À̼ø½Å"); if (pre != NULL) { head = delete(head, pre); print_list(head); } new = new_element("È«±æµ¿", 167, 72.5); head = insert_last(head, *new); print_list(head); new = new_element("±èÀ¯½Å", 159, 70.8); head = insert_first(head, *new); print_list(head); return 0; } | cs |