B. Wayne B. Wayne - 23 days ago 4x
C Question

Why am i gettting segmentation fault (core dumped) in my C queue?

So i was trying to implement a Queue data structure in C but i keep getting this error: Segmentation fault(core dumped). I think its coming from my enqueue method. Can someone help me solve this...

This is my queue.c file

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

void createQueue(LinkedList* list)
list = (LinkedList*)calloc(1, sizeof(LinkedList));

int isEmpty(LinkedList* list)
int empty = 0;
if(list->head == NULL)
empty = 1;
return empty;

void enqueue(LinkedList* list, Person p)
LinkedListNode* newNode = (LinkedListNode*)malloc(sizeof(LinkedListNode));
newNode->data = p;
if (isEmpty(list))
list->head = list->tail = newNode;
} else
LinkedListNode* first = list->head;
first->previous = newNode;
newNode->next = list->head;
list->head = newNode;
list->count ++;

Person dequeue(LinkedList* list)
LinkedListNode* temp;
temp = list->tail;
list->tail = list->tail->previous;
list->count --;

return temp->data;

void viewQueue(LinkedList* list)
LinkedListNode* current;
current = list->head;
while (current != NULL)
printf("Name: %s Birthdate: %s\n", current->data.name, current->data.birthdate);
current = current->next;
printf("\nPeople: %d\n", list->count);

And this is my header file...


#define FALSE 0
#define TRUE !FALSE

typedef struct{
char name[20];
char birthdate[10];
} Person;

typedef struct LinkedListNode{
Person data;
struct LinkedListNode* next;
struct LinkedListNode* previous;
} LinkedListNode;

typedef struct{
LinkedListNode* head;
LinkedListNode* tail;
int count;
} LinkedList;

void createQueue(LinkedList* list);
int isEmpty(LinkedList* list);
void enqueue(LinkedList* list, Person p);
Person dequeue(LinkedList* list);
void viewQueue(LinkedList* list);


I also have this method to test my queue. When i run it i get a segmentation fault. Whats wrong with my program?

#include <stdio.h>
#include "queue.h"

int main(void)
LinkedList* list;
Person p1 = {"J Cole", "28/01/1985"};
Person p2 = {"Kendrick Lamar", "19/01/1994"};
Person p3 = {"Slim Shady", "17/10/1972"};
Person p4 = {"Tupac Shakur", "12/09/1971"};

enqueue(list, p1);
enqueue(list, p2);
enqueue(list, p3);
enqueue(list, p4);

return 0;


You are passing the value of the list pointer. You need to pass the reference.

void createQueue(LinkedList** list)
  *list = (LinkedList*)calloc(1, sizeof(LinkedList));

And from the main