1

I have to work on linked lists and get a bad access error even before something happens in my main. I don't know what's wrong. I am relatively new to dynamic memory management. It would be nice if someone could take a look on the functions. The declaration was given by the professor, so we have to return a DoubleNote*. My code is below:

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

typedef struct node{

    double var;

    struct node *next;


} DoubleNode;


DoubleNode* insertFirst(DoubleNode* head, double d){

    DoubleNode* new_head;
    new_head = (DoubleNode*)malloc(sizeof(DoubleNode));

    if (new_head == NULL) {
        printf("Error: Allocating memory for new node failed!");
        exit(1);
    }

    new_head->var = d;

    new_head->next = head;

    head = new_head;


    return head;

}



DoubleNode* inserLast(DoubleNode* head, double d){

    DoubleNode* current = head;

    while (current != NULL) {
        current = current->next;
    }

    current->next = (DoubleNode*)malloc(sizeof(DoubleNode));
    if (current->next == NULL) {
        printf("Error: Allocating memory for new node failed!");
        exit(1);
    }
    current->next->var = d;
    current->next->next = NULL;

    return head;

}

DoubleNode* inverseDoubleListCon(DoubleNode* head){

    DoubleNode* current = head; // iteration variable starts on head of old list
    DoubleNode* conHead = current; // Head of the new list

    while (current != NULL) {

        current = current->next; //iteration step

        DoubleNode* newConHead = (DoubleNode*)malloc(sizeof(DoubleNode)); //allocating memory for new head

        if (newConHead == NULL) {
            printf("Error: Allocating memory for new node failed!");
            exit(1);
        }

        newConHead = current; // new_head is the next variable in the old list

        newConHead->next = conHead; //new head points to old head of the new list

        conHead = newConHead; // new head is set
    }


    return conHead;

}

void printList(DoubleNode* head){

    DoubleNode* current = head;

    while (current != NULL) {
        printf("%lf\n", current->var);
        current = current->next;
    }

}




int main(){

    DoubleNode* head = NULL;
    DoubleNode* inverseHead = NULL;
    double d;
    int i;
    int sizeOfList;

    printf("Insert amount of variables: \n");
    scanf("%d", &sizeOfList);

    for (i = 0; i < sizeOfList; i++) {
        printf("Insert variable for node [%d]: \n", i);
        scanf("%lf", &d);

        head = insertFirst(head, d);
    }

    printList(head);

    inverseHead = inverseDoubleListCon(head);

    printList(inverseHead);



    return 0;
}
Melvin
  • 407
  • 5
  • 16
  • What operating system are you working on? – John Zwinck Dec 15 '15 at 11:55
  • @JohnZwinck OS X El Capitan. I'm using Xcode. – Melvin Dec 15 '15 at 11:56
  • OK, so use the Xcode debugger to run your program and see where exactly it is crashing. If you're still stuck, tell us where it crashes--which line. – John Zwinck Dec 15 '15 at 11:58
  • 3
    `DoubleNode* head = NULL;`....`head = insertFirst(head, d);` – BLUEPIXY Dec 15 '15 at 11:58
  • 3
    `sizeOfList` is not initialised before it is used in `for (i = 0; i < sizeOfList; i++)` – Rishikesh Raje Dec 15 '15 at 12:00
  • Above code run properly on windows. try what @BLUEPIXY have mentioned and let's us know if you are still stuck. – roottraveller Dec 15 '15 at 12:01
  • BTW, the last line in the function: head = new_head; doesn't change the "head" argument, its value changes only inside the function and is forgotten when the function finishes. You can just return new_head with the same result. – Mykhaylo Kopytonenko Dec 15 '15 at 12:03
  • Thanks a lot, I've edited the main and now printList prints out the right result after using insertFirst. I think something in the inverseDoubleListCon is wrong. It should construct a new inverted list. – Melvin Dec 15 '15 at 12:07

1 Answers1

1

Firstly sizeOfList is not initalised. You need to add code to get the value of the size from the user.

You are also not updating the value of the head pointer from the insertFirst function. The code below should help.

DoubleNode* head= NULL;

// Code to get the value of sizeofList

for (i = 0; i < sizeOfList; i++)
{
   ...
    head = insertFirst(head, d);
}

The reverse function is overly complicated. You are allocating memory in newConHead which is not required for reversing a linked list.

I would suggest a rewrite along the lines of How to reverse a singly linked list using only two pointers? or http://www.geeksforgeeks.org/write-a-function-to-reverse-the-nodes-of-a-linked-list/

Community
  • 1
  • 1
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • Thanks, I haven't noticed that. Such a stupid mistake. – Melvin Dec 15 '15 at 12:10
  • We should implement two different functions to invert a list. One in a constructive (create new inverted list) way and the other one in a destructive way. – Melvin Dec 15 '15 at 12:35