다음 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;
}모범답안
35421
풀이와 판단 근거
push는 새 노드를 항상 맨 앞에 붙인다. 1부터 5까지 넣고 나면 리스트는 5 → 4 → 3 → 2 → 1이 된다.
move_front는 값이 3인 노드를 기존 위치에서 떼어 맨 앞으로 옮긴다. 계산 결과는 3 → 5 → 4 → 2 → 1이기 때문에 35421이 최종 출력에 나타난다.
자주 틀리는 지점
- 연산자 우선순위나 재귀 반환 순서를 생략하고 눈에 보이는 값만 바로 계산함
- 값 전달·참조 전달, 오버라이딩·오버로딩 또는 배열과 포인터의 차이를 혼동함
- 출력의 공백·줄바꿈·대소문자를 무시해 35421와 다른 형식으로 작성함
자동 판정 방식
한글·영문 동의어와 문항별 필수 개념을 확인하며, 정답 단어가 부정되거나 반대 개념과 함께 쓰이면 자동 정답으로 확정하지 않습니다. 부분점수는 ITPASSLAB의 학습용 예상 점수입니다.