如何在链表中调用队列的前端?
这是一个结构,我给了一个返回指向新的空队列的指针的队列。我知道队列 - >后端指向队列后面的节点,队列 - >后 - >下一个指向队列前端的节点。我如何称呼队列的前端?每当我使用queue-> rear-> next时,我都会遇到运行时错误。如何在链表中调用队列的前端?
intqueue_t *intqueue_construct(void) {
intqueue_t *queue = malloc(sizeof(intqueue_t));
assert(queue != NULL);
queue->rear = NULL;
queue->size = 0;
return queue;
}
回答:
我不知道我是否正确,但我认为你看起来像这样。 Pleas提供所有代码,尤其是结构定义。
typedef struct node_t {
int data;
struct node_t* next;
} node;
void intqueue_addelement(node* n, int d) //add new element to linked list
{
if(n->next == NULL)
{
n->next = malloc(sizeof(node));
assert(n->next != NULL);
n->next->next = NULL;
n->next->data = d;
}
else
{
intqueue_addelement(n->next, d);
}
}
node* intqueue_lastelement(node* n)
{
if(n->next == NULL)
{
return n;
}
else
{
return intqueue_lastelement(n->next);
}
}
而且在主要应该是这个样子:
node *q = malloc(sizeof(node)); q->next = NULL;
q->data = 0;
您现在有一个链表。 q指向开头,intqueue_lastelement给出最后一个元素的指针,intqueue_addelement添加一个元素。
以上是 如何在链表中调用队列的前端? 的全部内容, 来源链接: utcz.com/qa/258980.html