我在使用此代码时遇到问题.我是C的新手,据我所知,我正在使用malloc操作.
#include "fifo.h"
#include <stdlib.h>
/* add a new element to a fifo */
void Enqueue( fifo* queue, int customerId)
{
//allocate memory for the element being added
//initialize fifo_element
fifo_element *temp;
temp = (fifo_element*)malloc(sizeof(fifo_element));
temp->customerId = customerId;
temp->prev = NULL;
temp->next = NULL;
//if the queue is empty, add the element to the start
if(&queue->head == NULL){
queue->head = queue->tail = temp;
return;
}
else{
queue->tail->next = temp;
temp->prev = queue->tail;
queue->tail = temp;
return;
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有出现分段错误,我无法执行此操作:
queue->tail->next …Run Code Online (Sandbox Code Playgroud)