다음은 연결 리스트를 이용하여 스택을 구현한 C 코드이다. (가), (나)에 들어갈 내용을 바르게 연결한 것은?
typedef struct _node {
int key;
struct _node *next;
} node;
node *head = NULL;
node *tail = NULL;
void init() {
head = (node*)malloc(sizeof(node));
tail = (node*)malloc(sizeof(node));
head->next = tail;
tail->next = tail;
}
int push(int k) {
node *t;
if((t = (node*)malloc(sizeof(node))) == NULL) {
printf("\n Out of memory");
return -1;
}
t->key = k;
(가)
head->next = t;
return k;
}
int pop() {
node *t;
int i;
if(head->next == tail) {
printf("\n Stack underflow");
return -1;
}
t = head->next;
i = t->key;
(나)
free(t);
return i;
} (가) (나)
- ①
t->next = head; head->next = t->next;
- ②
t->next = head->next; head->next = t->next;
- ③
t->next = head->next; head = t->next;
- ④
t->next = head->next; head->next = t;
선지를 골라 바로 채점해 보세요.
정답 ②
출처: 인사혁신처 공개 기출(공공데이터포털, 이용허락 제한 없음)