从流行函数返回地址错误 [英] returning wrong address from pop function
问题描述
解决我的结构等问题后,我推按预期工作但是我弹出返回错误的地址,我不知道为什么 -
After solving other issues with my struct, my push works as intended however my pop returns the wrong address and I'm not sure why -
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;
}
我相当肯定我对栈的实际拆除和重新链接的逻辑是正确的,但我使用指针和返回他们被搞砸了。
I'm fairly certain my logic for the actual removal and relinking of the stack is correct but my use of pointers and returning them is messed up.
结构 -
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;
感谢您的帮助!
推荐答案
您不应该使用前哨淋巴结进行;这是毫无意义的,非常混乱。队列可以简单地重新psented为 QNode *
来的第一个元素$ P $。它总是指向所述第一元件;如果是 NULL
,队列是空的;如果元素 - >接下来
是 NULL
,这是最后一个元素,因为没有一家挨着一家。这是非常简单的与工作。
You shouldn't be using "sentinel nodes"; this is pointless and very confusing. A queue can be simply represented as a QNode*
to the first element. It always points to the first element; if it's NULL
, the queue is empty; if element->next
is NULL
, it's the last element because there isn't a next one. It's very simple to work with that.
struct QueueNode {
// stuff
// stuff
// stuff
struct QueueNode* prev; // this may be optional
struct QueueNode* next;
};
typedef struct QueueNode QNode;
void push_front(QNode** queue, QNode* pushme) {
pushme->prev = NULL;
pushme->next = *queue;
(*queue)->prev = pushme;
*queue = pushme;
}
void push_end(QNode** queue, QNode* pushme) {
QNode* node = *queue;
if (node) {
while (node->next) node = node->next;
node->next = pushme;
pushme->prev = node;
pushme->next = NULL;
}
else {
*queue = pushme;
(*queue)->next = (*queue)->prev = NULL;
}
}
QNode* pop_front(QNode** queue) {
QNode* node = *queue;
if (node)
*queue = node->next;
return node;
}
QNode* pop_end(QNode** queue) {
QNode* node = *queue;
if (node) {
while (node->next) node = node->next;
if (node->prev) {
node->prev->next = NULL;
node->prev = NULL;
}
else
*queue = NULL;
}
return node;
}
QNode* create_node_front(QNode** queue) {
QNode* node = malloc(sizeof(QNode));
push_front(queue, node);
return node;
}
QNode* create_node_end(QNode** queue) {
QNode* node = malloc(sizeof(QNode));
push_end(queue, node);
return node;
}
QNode* my_queue = NULL; // declare an empty queue
QNode* my_node = create_node_end(&my_queue); // create a new node, its already stored in the queue
我没有测试它,但它给出了一个大致的了解。
I didn't test it, but it gives a general idea.
您可以用推push_front()
或 create_node_front()
(无循环,最佳性能),然后用流行 pop_end()
有队列效应( FIFO 一>),或 pop_front()
有一个烟囱效应(的 LIFO )。
You can push with push_front()
or create_node_front()
(no loops, best performance) then pop with pop_end()
to have a queue effect (FIFO), or pop with pop_front()
to have a stack effect (LIFO).
这篇关于从流行函数返回地址错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!