我试图在C语言中建立一个指针的双端队列。到目前为止,我已经完成了推送功能的工作和测试。我的问题似乎是在两端弹出条目。#include <stdlib.h>#include <stdio.h>#include "dequeue.h"dequeue* dequeue_create(){ return NULL;}void dequeue_push_front(dequeue** dq, int data){ dequeue* tmp = malloc(sizeof(*tmp)); tmp->data = data; tmp->next=NULL; if((*dq) == NULL) { (*dq) = tmp; } else { if ((*dq)->next == NULL) { (*dq)->next = tmp; tmp->prev = (*dq); } else { dequeue* tmp_it = malloc(sizeof(struct _dequeue_)); tmp_it = (*dq)->next; while(tmp_it->next != NULL) { tmp_it = tmp_it->next; } tmp_it->next = tmp; tmp->prev = tmp_it; } }}void dequeue_push_back(dequeue** dq, int data){ dequeue* tmp = malloc(sizeof(struct _dequeue_)); tmp->data = data; tmp->prev=NULL; if((*dq) == NULL) { (*dq) = tmp; } else { if ((*dq)->prev == NULL) { (*dq)->prev = tmp; tmp->next = (*dq); } else { dequeue* tmp_it = malloc(sizeof(struct _dequeue_)); tmp_it = (*dq)->prev; while(tmp_it->prev != NULL) { tmp_it = tmp_it->prev; } tmp_it->prev = tmp; tmp->next = tmp_it; } }}int dequeue_pop_front(dequeue** dq){ dequeue* tmp_get = malloc(sizeof(struct _dequeue_)); int output = 0; if((*dq)->next == NULL) { printf("\ndqnext==null\n"); } else { tmp_get = (*dq); while(tmp_get->next != NULL) { tmp_get= tmp_get->next; output = tmp_get->data; } tmp_get=tmp_get->prev; free(tmp_get->next); tmp_get->next=NULL; } return output;}int dequeue_pop_back(dequeue** dq){ dequeue* tmp_get = malloc(sizeof(struct _dequeue_)); int output = 0; if((*dq)->prev == NULL) { printf("\ndqprev==null\n"); } else { tmp_get = (*dq); while(tmp_get->prev != NULL) { output = tmp_get->data; tmp_get= tmp_get->prev; } free(tmp_get); tmp_get=NULL; } return output;}出列.h:#ifndef dequeue_H#define dequeue_Hstruct _dequeue_ { struct _dequeue_* next; struct _dequeue_* prev; int data;};typedef struct _dequeue_ dequeue;dequeue* dequeue_create();void dequeue_destroy(dequeue** queue);int dequeue_pop_front(dequeue** dq);void dequeue_push_front(dequeue** dq, int data);int dequeue_pop_back(dequeue** dq);void dequeue_push_back(dequeue** dq, int data);#endif /* dequeue_H */主要c:int main(){ dequeue* dq = dequeue_create(); dequeue_push_front(&dq, 1); dequeue_push_back(&dq, 2); dequeue_push_front(&dq, 3); for (int cnt = 1; cnt <=4; cnt++) { printf("FINAL=%d ", dequeue_pop_front(&dq)); } //TODO : dequeue_destroy(&dq); return 0;}我对指针很陌生,这似乎是我的问题。我在pop函数中试图做的是遍历指针以到达最后一个指针并释放最后一个指针。但它似乎并没有释放指针。现在尝试了几种不同的方法,但似乎都不起作用,是不是我设置推送函数的方式不可能释放指针?任何帮助都非常感谢。干杯 最佳答案 推动dequeue_push_front(&dq, 1);空空dq指向中间节点dequeue_push_front(&dq, 2);空右(2)空dq仍然指向中间节点,因为只有当dq作为NULL传入时,代码才会更改它空中(1)右(2)空dq仍指向中间节点的原因相同流行音乐让我们跟随您的流行代码: tmp_get = (*dq); while(tmp_get->next != NULL) { tmp_get= tmp_get->next; output = tmp_get->data; } tmp_get=tmp_get->prev; free(tmp_get->next); tmp_get->next=NULL;这将导致以下状态:空中(1)空dq仍指向中间节点。下一次,以及每隔一次执行函数时if((*dq)->next == NULL)将计算为true,不会导致进一步的更改。其他问题如注释中所述,pop函数中存在内存泄漏。见下文:dequeue* tmp_get = malloc(sizeof(struct _dequeue_));...tmp_get = (*dq);调用malloc(),分配sizeof(struct _dequeue_)字节的内存。返回一个指针,并将其分配给变量tmp_get。然后用另一个指针覆盖该指针,这意味着现在有一块分配的内存无法访问或释放。我看不出你为什么首先需要分配这个内存。
09-12 20:00