[7급 국가직 자료구조론 2026년 5번]

다음은 연결 리스트를 이용하여 스택을 구현한 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;
}
(가) (나)
자료구조론
  1. ①
    t->next = head;	head->next = t->next;
  2. ②
    t->next = head->next;	head->next = t->next;
  3. ③
    t->next = head->next;	head = t->next;
  4. ④
    t->next = head->next;	head->next = t;

선지를 골라 바로 채점해 보세요.

정답 ②

출처: 인사혁신처 공개 기출(공공데이터포털, 이용허락 제한 없음)

신고가 접수됐습니다. 감사합니다.

오류 신고