0
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
};

struct Node* pRoot = NULL;

void Inorder(struct Node* root) {
    if (root->prev != NULL) {
        Inorder(root->prev);
    }
    printf("%d ", root->data);
    if (root->next != NULL) {
        Inorder(root->next);
    }
}

void Assort(struct Node* cur, struct Node* pstan) {
    if (pRoot == NULL) {
        pRoot = pstan = cur;
    }
    else if (cur->data < pstan->data) {
        if (pstan->prev == NULL) {
            pstan->prev = cur;  
        }
        else if (pstan->prev != NULL) {
            Assort(cur, pstan->prev);
            printf("작동함\n");
        }
    }
    else if (cur->data >= pstan->data) {
        if (pstan->next == NULL) {
            pstan->next = cur;
        }
        else if (pstan->prev != NULL) {
            Assort(cur, pstan->next);
            printf("작동함\n");
        }
    }

}

int main() {
    struct Node* pStan = NULL;
    struct Node* Current;

    for (int i = 0; i < 5; i++) {
        struct Node* Current = (struct Node*)malloc(sizeof(struct Node));
        printf("정수를 입력하세요.\n");
        scanf("%d", &Current->data);

        Assort(Current, pStan);
        pStan = pRoot;
    }

    Inorder(pRoot);
    return 0;
}

else if (cur-\>data \< pstan-\>data) { In this paragraph an unhandled exception was thrown: read access violation. pstan was 0xCDCDCDCD. This error occurs.

I expect this to be a problem related to pointers, but since I don't know much about data structures and pointers yet, I don't know where the exception is not handled, so I'm asking a question. This code is made for the purpose of a bidirectional linked list. However, I think that part of this is something I need to think about. I just want to know why I am getting an error regarding pointers.

Gerhard
  • 6,850
  • 8
  • 51
  • 81
ty par
  • 3
  • 1
  • 2
    Welcome to SO. `0xCDCDCDCD` is a value intented to indicate you did not initialized your variables. This is a special service of your compiler. You may not rely on that being done for you. You forgot to set a pointer to `NULL` or a valid address. You seem to expect `malloc` might set the memory to some value like all 0. That is not the case. You must take care about valid content. – Gerhardh Mar 22 '23 at 11:19
  • The problem mentioned by @Gerhardh that's just _one_ problem. The `Assort` function is totally broken. I'd throw this function away and rewrite it from scratch. Use a piece of paper and a pencil and draw the elements of your list using arrows as pointers. Google _doubly linked list c tutorial_, there are tons of tutorials. [This one](https://www.simplilearn.com/tutorials/c-tutorial/doubly-linked-list-in-c) looks pretty good, but as mentioned there are lots of them – Jabberwocky Mar 22 '23 at 13:12

1 Answers1

2

Firstly, you forgot to initialize Current->prev and Current->next. Initialize them to avoid errors.

Also, casting results of malloc() family is considered as a bad practice.

struct Node* Current = malloc(sizeof(struct Node)); /* remove cast */
Current->prev = Current->next = NULL; /* add this to initialize members */

Secondly, the part

        else if (pstan->prev != NULL) {
            Assort(cur, pstan->next);

contains a bug that pstan->prev is checked while pstan->next should be checked.

pstan->prev != NULL should be true when pstan->prev == NULL is false, so the two if part in the Assort function are redundant and should be removed.

void Assort(struct Node* cur, struct Node* pstan) {
    if (pRoot == NULL) {
        pRoot = pstan = cur;
    }
    else if (cur->data < pstan->data) {
        if (pstan->prev == NULL) {
            pstan->prev = cur;  
        }
        else { /* remove redundant condition */
            Assort(cur, pstan->prev);
            printf("작동함\n");
        }
    }
    else if (cur->data >= pstan->data) {
        if (pstan->next == NULL) {
            pstan->next = cur;
        }
        else { /* remove redundant and buggy condition */
            Assort(cur, pstan->next);
            printf("작동함\n");
        }
    }

}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thank you. //Current->prev = Current->next = NULL; // I wasn't aware of this part. However, regarding the casting of malloc, it seems that I have no choice but to cast because I am coding the C language using VisualStudio's C++. However, your advice on casting will be helpful when I am programming in the future. – ty par Mar 22 '23 at 11:38
  • @typar if youre programming in C, give your file the extension .c instead of .cpp. .c files will be compiled as C, .cpp files will be compield as C++. – Jabberwocky Mar 22 '23 at 13:03
  • I'm not quite sure what the OP's code is supposed to do, but I assume that it should create a doubly linked list. With your correction, the program no longer crashes, but the list is wrong, because the `prev` pointer of nodes in the list is NULL. The problem doesn't show up because `Inorder` only uses `next` for the list traversel. – Jabberwocky Mar 22 '23 at 13:08
  • @typar if you compile as C++ code, you are not writing a C program but C++. Different language, different rules. – Gerhardh Mar 22 '23 at 13:26