【发布时间】:2019-04-14 05:04:13
【问题描述】:
我正在处理一个队列并且一直遇到排队问题。以下是我认为相关的代码:
typedef struct Qnode QNODE;
struct Qnode
{
int length;
QNODE* next;
QNODE* prev;
};
typedef struct lqueue lQUEUE;
struct lqueue
{
QNODE *head;
QNODE *tail;
};
lQueue lqueue_init_default(void)
{
lQUEUE* pQ = NULL;
pQ = (lQUEUE*)malloc(sizeof(lQUEUE));
if (pQ != NULL)
{
pQ->head = NULL;
pQ->tail = NULL;
}
pQ->head = pQ->tail;
return pQ;
}
Status lqueue_henqueue(lQueue* hLQ, int lc)
{
lQUEUE* pLQ = (lQUEUE*)hLQ;
QNODE* new = (QNODE*)malloc(sizeof(QNODE));
if (new == NULL)
{
printf("Couldn't allocate space.\n");
return FAILURE;
}
new->length = lc;
new->next = pLQ->tail->next;
pLQ->tail = new;
return SUCCESS;
}
每当我尝试运行程序时,都会在运行时收到此错误:
抛出异常:读取访问冲突。
pLQ->tail 是 nullptr。
为什么是空指针?跟初始化函数有关系吗?
这是它的名称:
int cl = 0;//Individual car length
lQueue hLQ = lqueue_init_default();//Handle to the left queue
printf("Enter the length of the lcar:\n");
scanf("%d", &cl);
lqueue_henqueue(hLQ, cl);
【问题讨论】:
-
你必须展示
lqueue_henqueue是如何被调用的 -
lQueue lqueue_init_default(void)您是否忘记了返回类型上的*? -
不,因为当我这样做时,会出现编译器错误。
标签: c pointers linked-list queue