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 | #define _CRT_SECURE_NO_WARNINGS #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)); p->data = value; p->link = head; head = p; return head; } // ƯÁ¤ ³ëµå µÚ¿¡ »õ·Î¿î ³ëµå¸¦ »ðÀÔÇÏ´Â ÇÔ¼ö ListNode* insert(ListNode* head, ListNode* pre, element value) { ListNode* p = (ListNode*)malloc(sizeof(ListNode)); p->data = value; p->link = pre->link; pre->link = p; return head; } // ¸®½ºÆ®ÀÇ ¸ðµç ³ëµå¸¦ Ãâ·ÂÇÏ´Â ÇÔ¼ö 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)); 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; } // ƯÁ¤ ³ëµå µÚ¿¡ µ¿ÀÏÇÑ ³ëµå¸¦ k°³ Ãß°¡ÇÏ´Â ÇÔ¼ö ListNode* insert_multiple_after_node(ListNode* head, char* name, int k) { ListNode* node = find_node(head, name); if (node == NULL) { printf("ÇØ´ç À̸§ÀÇ ³ëµå¸¦ ãÀ» ¼ö ¾ø½À´Ï´Ù.\n"); return head; } for (int i = 0; i < k; i++) { element new_data = node->data; // ±âÁ¸ µ¥ÀÌÅ͸¦ º¹»ç head = insert(head, node, new_data); // ÇØ´ç ³ëµå µÚ¿¡ »õ ³ëµå¸¦ Ãß°¡ node = node->link; // »õ·Î Ãß°¡ÇÑ ³ëµå·Î À̵¿ } return head; } // Å×½ºÆ® ÇÁ·Î±×·¥ int main(void) { ListNode* head = NULL; 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); // "À¯°ü¼ø" µÚ¿¡ µ¿ÀÏÇÑ µ¥ÀÌÅ͸¦ 3°³ Ãß°¡ head = insert_multiple_after_node(head, "À¯°ü¼ø", 3); print_list(head); return 0; } | cs |