【发布时间】:2013-10-05 02:43:19
【问题描述】:
在解决了我的结构的其他问题后,我的推送按预期工作,但是我的 pop 返回了错误的地址,我不知道为什么 -
QNode* const Q_Pop(Q* const pointerQ){
... // empty check
QNode* tempNode = pointerQ->front.next;
pointerQ->front.next = (tempNode->next);
tempNode->next->prev = &(pointerQ->front);
return tempNode;
}
我相当确定我实际删除和重新链接堆栈的逻辑是正确的,但我对指针的使用和返回它们是一团糟。
结构 -
struct QueueNode {
struct QueueNode *prev; /* Previous list element. */
struct QueueNode *next; /* Next list element. */
};
typedef struct QueueNode QNode;
struct Queue {
QNode front; // sentinel node at the front of the queue
QNode rear; // sentinel node at the tail of the queue
};
typedef struct Queue Q;
感谢您的帮助!
【问题讨论】:
-
必须查看更多代码,但似乎pointerQ->front 可能会指向队列的头部。你会返回那个而不是下一个。但是,如果没有看到数据结构,我就无法判断。
-
我猜你想弹出当前对象,即
pointerQ->front,而不是pointerQ->front->next,这可能是你的问题。 -
如果您可以提供一个可运行的程序并告诉您期望的输出,那么回答您的问题会更容易。 sscce.org
-
奇怪的是
pointerQ->front不是指针。根据我对堆栈的了解,这应该是一个指针,以便您可以更改它,如果队列为空等,则设置为NULL。 -
刚刚编辑它以提供结构代码