74. 연결 리스트의 앞쪽 삽입과 노드 삭제

다음 C 프로그램의 실행 결과를 쓰시오.

1#include <stdio.h>
2#include <stdlib.h>
3
4struct Node {
5 int val;
6 struct Node *next;
7};
8
9void push(struct Node **head, int v) {
10 struct Node *n = (struct Node *)malloc(sizeof(struct Node));
11 n->val = v;
12 n->next = *head;
13 *head = n;
14}
15
16int show(struct Node *p) {
17 int sum = 0;
18 while (p != NULL) {
19 printf("%d", p->val);
20 sum += p->val;
21 p = p->next;
22 printf(p != NULL ? "->" : "\n");
23 }
24 return sum;
25}
26
27int main() {
28 int data[4] = {4, 7, 2, 9};
29 struct Node *head = NULL;
30 for (int i = 0; i < 4; i++) {
31 push(&head, data[i]);
32 }
33 int s1 = show(head);
34 struct Node *del = head->next;
35 head->next = del->next;
36 free(del);
37 int s2 = show(head);
38 printf("%d %d\n", s1, s2);
39 while (head != NULL) {
40 struct Node *t = head;
41 head = head->next;
42 free(t);
43 }
44 return 0;
45}

해설을 먼저 보면 이후 정답 시 점수가 50%만 반영됩니다.