0

Always get the error code. I'm trying to reverse transverse the list and print. Is there something I'm missing in the creation of the list? I can print the list normal but just not reversed Any help would be appreciated

struct stNode {
    char            *pName;
    double          avg;
    struct stNode   *next;
    struct stNode   *prev;
}; //stNode
typedef struct stNode Node;

void  createList(Node **head, Node **tail) {
    Node* tmp;
    Node* x;

    for(int i = 0;i < INPUTSIZE; i++){
        //stores values in nodes
        tmp = malloc(sizeof(Node));
        tmp->avg = f1input_avg[i];
        tmp->pName = f1input_name[i];
        tmp->next = NULL;

        //If list is empty
        if( *head == NULL){
            *head  = tmp;
        }
        //Creates subsequent nodes
        else{
            x = *head;
            while(x->next != NULL){
                x = x->next;
            }
            x->next = tmp;
        }
    }
} // createList

void RevprintList(Node *head){
         Node        *cur = NULL;


    printf("=======Reversed List =======\n");
    cur = head;
    while (cur->next != NULL) {
        cur = cur->next;
    }

    while (cur != head){
        printf("%-15s %5.2lf\n", cur->pName, cur->avg);
        cur = cur->prev;
    }
}

int main() {
    Node        *head = NULL;
    Node        *tail = NULL;

    createList(&head, &tail);
    printList(head);
    RevprintList(head);
    freeList(&head, &tail);
}

Process returned -1073741819 (0xC0000005)

5
  • What is the full error message?
    – Locke
    May 26, 2021 at 0:26
  • You should include the code that makes the createList and RevprintList calls. May 26, 2021 at 0:33
  • Process terminated with status -1073741819 (11 minute(s), 46 second(s)) This is all I get
    – Gai Binion
    May 26, 2021 at 0:35
  • This is a good time to learn to use a debugger. At a minimum it will immediately give you the exact line of code that triggers the crash and can also be used to examine the program as it runs. And where does "access denied" come from?
    – kaylum
    May 26, 2021 at 0:58
  • The prev pointer is never set. So there's no way traversing backwards with that pointer has any chance of working.
    – kaylum
    May 26, 2021 at 1:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.