Inserting into my linked list is causing a segmentation fault and I'm not sure why. It's an int linked list. It works sometimes and then other times not. I can't see where it would be going wrong? Maybe it has something to do with when I create the new linked list. As the linked list insert last method is working during file reading but not when I create a new list.
LinkedList* createLinkedList()
{
LinkedList* list;
list = (LinkedList*)malloc(sizeof(LinkedList));
list->head = NULL;
list->tail = NULL;
list->length = 0;
#ifdef DEBUG
printf("Linked list. Creation of list complete. Checking list length. %d \n", list->length);
#endif
return list;
}
void insertLast(LinkedList* list, int entry)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = entry;
/*If list is empty*/
if(list->length == 0)
{
list->head = newNode;
list->tail = newNode;
}
else
{
newNode->prev = list->tail;
/*if this is the second item in the list*/
if(list->length == 1)
{
list->head->next = newNode;
}
else
{
list->tail->next = newNode;
}
}
list -> tail = newNode;
list->length++;
#ifdef DEBUG
printf("Linked List. Insert last complete. Checking list length. %d\n", list->length);
#endif
}
This works
int main (void)
{
LinkedList* list = createLinkedList();
insertLast(list, 1);
}
but this doesn't
int shortestSeekTimeFirst(LinkedList* list)
{
LinkedList* usedDisks = createLinkedList();
int data = list->head->data;
insertLast(usedDisks, data);
insertLast(usedDisks, list->head->next->data);
}
shortestSeekTimeFirst,the head is NULL, you cannot get anything from NULL