Menu
Coddy logo textTech

removeLast

Coddy의 이중 연결 리스트 - 자료구조 시리즈 #6 코스 레슨 — 14개 중 8번째.

challenge icon

챌린지

쉬움

DoublyLinkedList 클래스에 removeLast 메서드를 추가하세요.

이 메서드는 입력값을 받지 않으며 리스트에서 마지막 노드를 제거합니다:

  • 리스트가 비어 있으면 아무것도 하지 않습니다.
  • 그렇지 않으면 마지막 노드를 제거하고, tail을 업데이트하며(리스트에 노드가 하나만 있었던 경우 head도 업데이트), count를 감소시킵니다.

직접 해보기

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "doublylinkedlist.h"

int main() {
    DoublyLinkedList ll;
    DoublyLinkedList_init(&ll);
    char line[256];
    while (fgets(line, sizeof(line), stdin)) {
        line[strcspn(line, "\r\n")] = '\0';
        char* cmd = strtok(line, " \t");
        if (!cmd) continue;
        if (strcmp(cmd, "state") == 0) printf("%s %s %d\n", ll.head == NULL ? "true" : "false", ll.tail == NULL ? "true" : "false", ll.count);
        if (strcmp(cmd, "count") == 0) printf("%d\n", ll.count);
        if (strcmp(cmd, "headValue") == 0) printf("%d\n", Node_getValue(ll.head));
        if (strcmp(cmd, "tailValue") == 0) printf("%d\n", Node_getValue(ll.tail));
        if (strcmp(cmd, "addFirst") == 0) {
            char* arg = strtok(NULL, " \t");
            DoublyLinkedList_addFirst(&ll, atoi(arg));
        }
        if (strcmp(cmd, "addLast") == 0) {
            char* arg = strtok(NULL, " \t");
            DoublyLinkedList_addLast(&ll, atoi(arg));
        }
        if (strcmp(cmd, "get") == 0) {
            char* arg = strtok(NULL, " \t");
            printf("%d\n", DoublyLinkedList_get(&ll, atoi(arg)));
        }
        if (strcmp(cmd, "removeLast") == 0) DoublyLinkedList_removeLast(&ll);
    }
    return 0;
}

이중 연결 리스트 - 자료구조 시리즈 #6의 모든 레슨