프로그래밍 언어 활용난도 중상
18번 · 주관식 · 5점
다음 C 프로그램의 실행 결과를 쓰시오.
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int value;
struct Node *next;
} Node;
Node *push(Node *head, int value) {
Node *node = malloc(sizeof(Node));
node->value = value;
node->next = head;
return node;
}
Node *move_front(Node *head, int target) {
if (head == NULL || head->value == target) return head;
Node *previous = NULL;
Node *current = head;
while (current != NULL && current->value != target) {
previous = current;
current = current->next;
}
if (current != NULL) {
previous->next = current->next;
current->next = head;
head = current;
}
return head;
}
int main(void) {
Node *head = NULL;
for (int i = 1; i <= 5; i++) head = push(head, i);
head = move_front(head, 3);
for (Node *p = head; p != NULL; p = p->next) {
printf("%d", p->value);
}
return 0;
}