만들어야하는 코드의 목표였고...

입력예시 사안이 이렇게 나와있었는데...




#pragma warning (disable : 4996)

#include<stdio.h>

#include<string.h>

#include <stdlib.h> 

struct Node {

    char data;

    struct Node* next;

    struct Node* prev;

};


struct Node* getNewNode(int x) {


    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = x;

    newNode->next = NULL;

    newNode->prev = NULL;


    return newNode;

}


int Add(struct Node** head, struct Node** trailer, int r, char e, int size) {


    struct Node* newNode = getNewNode(e);



    if (r < 1 || r> size + 1) {

        printf("invalid position\n");

        return size;

    }


    if ((*head)->next == *trailer) {

        (*head)->next = newNode;

        newNode->prev = *head;


        (*trailer)->prev = newNode;

        newNode->next = *trailer;

        size++;

        return size;

    }




    if (r == 1) {

        newNode->next = (*head)->next;

        (*head)->next->prev = newNode;


        newNode->prev = *head;

        (*head)->next = newNode;

        size++;

        return size;

    }



    struct Node* temp = (*head)->next;


    for (int i = 0; i < r - 2; i++) {

        temp = temp->next;

    }



    newNode->next = temp->next;

    temp->next->prev = newNode;


    temp->next = newNode;

    newNode->prev = temp;

    size++;

    return size;


}


int  Delete(struct Node** head, int r, int size) {


    struct Node* temp = (*head)->next;

    if (r < 1 || r> size + 1) {

        printf("invalid position\n");

        return size;

    }



    if (r == 1) {

        (*head)->next = temp->next;

        temp->next->prev = *head;


        free(temp);

        size--;

        return size;

    }


    int c = 0;



    struct Node* temp2 = (*head)->next->next;


    for (int i = 0; i < r - 2; i++) {

        temp2 = temp2->next;

    }



    temp2->next->prev = temp2->prev;

    temp2->prev = temp2->next;

    free(temp2);

    size--;

    return size;

}


void Get(struct Node** head, int r) {

    struct Node* temp = (*head)->next;



    for (int i = 0; i < r - 1; i++) {

        temp = temp->next;

    }


    if (temp->data == NULL) {

        printf("invalid position\n");

        return;

    }


    printf("%c\n", temp->data);

}



void Print(struct Node* head) {

    struct Node* temp = head->next;


    while (temp->next != NULL) {


        printf("%c", (temp)->data);

        (temp) = (temp)->next;

    }

    printf("\n");


}


void main() {

    struct Node* head = (struct Node*)malloc(sizeof(struct Node));

    struct Node* trailer = (struct Node*)malloc(sizeof(struct Node));


    head->next = trailer;

    head->data = NULL;

    head->prev = NULL;


    trailer->next = NULL;

    trailer->data = NULL;

    trailer->prev = head;


    int n, r, size = 0;

    char x[10], e;

    scanf("%d", &n);


    for (int i = 0; i < n; i++) {

        scanf("%s", x);


        if (x[0] == 'A') {

            scanf("%d %c", &r, &e);

            size = Add(&head, &trailer, r, e, size);

        }


        if (x[0] == 'D') {

            scanf("%d", &r);

            size = Delete(&head, r, size);

        }


        if (x[0] == 'G') {

            scanf("%d", &r);

            Get(&head, r);

        }


        if (x[0] == 'P') {

            Print(head);

        }


    }




}


코드를 구성하는 것은 이렇게 까지 되었습니다.

다만 입력예시 1,2의 경우에는 문제가 없었으나 특정 경우에 runtime error가 발생하고 있습니다.

아무래도 작동중 어딘가에서 segmentation fault가 발생하고 있는 듯합니다만, 아직 미숙한지라 어떤 값을 입력해야지 그런 오류가 뜨는지, 어떤 부분에서 문제가 생기는지도 모르겠습니다... 부디 가엽게 여겨 조그마한 도움 부탁드립니다...