#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;
}