Я пытаюсь создать связанный список приоритетной очереди, но продолжаю сталкиваться с ошибкой сегментации.
Мои определения структуры ниже
typedef struct node {
char *new_element;
struct node *next;
int priority;
} Qnode;
typedef struct {
Qnode *top;
Qnode *tail;
int size;
} Priority_queue;
int main() {
Priority_queue q;
init(&q);
enqueue(&q, "hi", 1);
return 0;
}
void init(Priority_queue *const q) {
q->top = NULL;
q->tail = NULL;
q->size = 0;
return 0;
}
И мой метод постановки в очередь, где ошибкавызвано ниже
void enqueue(Priority_queue *const q, const char new_element[], int priority) {
/*......*/
Qnode *newNode = (Qnode*) malloc(sizeof(Qnode));
q->tail->next = newNode; /*causes segmentation fault*/
q->tail = newNode; /*doesn't cause segmentation fault*/
/*.......*/
}
Я предполагаю, что не правильно распределяю свою память динамически, но, как написана моя функция, я указываю от одной структуры к другой, так что есть ли способ исправитьэто?