#define _CRT_SECURE_NO_WARNINGS #include #include #include 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; }