gpt4 book ai didi

无法使用 C 中的 visual studio 从双向链表中删除

转载 作者:太空宇宙 更新时间:2023-11-04 06:37:42 24 4
gpt4 key购买 nike

你好,我目前正在做一个作业,应该读入一个文件,使用信息,然后打印出另一个文件。全部使用双向链表。目前我正在尝试将文件读入双向链表,将其打印到屏幕和文件上,最后删除列表并关闭程序。只要我不调用应该删除字符串的 dlist_distroy 函数,该程序就可以正常工作。我一这样做,程序就开始运行,然后弹出一个窗口说

"Windows has triggered a breakpoint in tempfilter.exe.

This may be due to a corruption of the heap, which indicates a bug in tempfilter.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while tempfilter.exe has focus.

The output window may have more diagnostic information."

修改了 destroy 和 remove 函数,无法理解这个问题。我的程序如下

主.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dlinklist.h"
#include "DlistElmt.h"
#include "Dlist.h"
#include "dlistdata.h"

/****************************************************************************/

int main (int argc, char *argv[])
{
FILE *ifp, *ofp;
int hour, min;
Dlist *list;
DlistElmt *current = NULL, *current2 = NULL;
float temp;

list = (Dlist *)malloc(sizeof(list));
element = (DlistElmt *)malloc(sizeof(element));

if (argc != 3) { /* argc should be 3 for correct execution */
/* We print argv[0] assuming it is the program name */

/* TODO: This is wrong, it should be: usage: %s inputfile outputfile */
printf( "usage: %s filename", argv[0] );
} else {
// We assume argv[1] is a filename to open
ifp = fopen(argv[1], "r");
if (ifp == 0) {
printf("Could not open file\n");
} else {
ofp = fopen(argv[2], "w");
dlist_init(list);//, (destroy)(hour, min, temp));
while (fscanf(ifp, "%d:%d %f ", &hour, &min, &temp) == 3) {
current=list->tail;
if (dlist_size(list) == 0) {
dlist_ins_prev(list, current, hour, min, temp);
} else {
dlist_ins_next(list, current, hour, min, temp);
}
}
current = list->head;
while (current != NULL) {
if (current==list->head) {
current=current->next;
} else
if ((current->temp > (current->prev->temp +5)) ||
(current->temp < (current->prev->temp -5))) {
current2 = current->next
dlist_remove(list, current);
current = current2;
} else
current=current->next;
}

current = list->head;
while(current != NULL) {
printf("%d:%d %2.1lf\n",
current->time,
current->time2,
current->temp
);
fprintf(ofp, "%d:%d %2.1lf\n",
current->time,
current->time2,
current->temp
);
current = current->next;
}
//dlist_destroy(list);
//}

fclose(ifp);
fclose(ofp);
}
}

getchar();
}

dlistdata.c

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dlinklist.h"
#include "DlistElmt.h"
#include "dlistdata.h"

/****************************************************************************/

void dlist_init(Dlist *list)
{
list->size = 0;
list->head = NULL;
list->tail = NULL;
return;
}

void dlist_destroy(Dlist *list) {
while (dlist_size(list) > 0) {
dlist_remove(list, list->head);
}
memset(list, 0, sizeof(Dlist));

return;
}

int dlist_ins_next(Dlist *list, DlistElmt *element, const int time,
const int time2, const float temp)
{
DlistElmt *new_element;

if (element == NULL && dlist_size(list) != 0)
return -1;
if ((new_element = (DlistElmt *)malloc(sizeof(new_element))) == NULL)
return -1;

new_element->time = (int)time;
new_element->time2 = (int)time2;
new_element->temp = (float)temp;

if (dlist_size(list) == 0) {
list->head = new_element;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_element;
} else {
new_element->next = element->next;
new_element->prev = element;

if (element->next == NULL)
list->tail = new_element;
else
element->next->prev = new_element;
element->next = new_element;
}

list->size++;

return 0;
}

int dlist_ins_prev(Dlist *list, DlistElmt *element, const int time,
const int time2, const float temp)
{
DlistElmt *new_element;

if (element == NULL && dlist_size(list) != 0)
return -1;

if ((new_element = (DlistElmt *)malloc(sizeof(new_element))) == NULL)
return -1;

new_element->time = (int)time;
new_element->time2 = (int)time2;
new_element->temp = (float)temp;

if (dlist_size(list) == 0){
list->head = new_element;
list->head->prev = NULL;
list->head->next = NULL;
list->tail = new_element;
} else {
new_element->next = element;
new_element->prev = element->prev;

if (element->prev == NULL)
list->head = new_element;
else
element->prev->next = new_element;
element->prev = new_element;
}

list->size++;

return 0;
}

int dlist_remove(Dlist *list, DlistElmt *element)
{ /*, int time, int time2, float temp){ */

if (element == NULL || dlist_size(list) == 0)
return -1;
if (element == list->head) {
list->head = element->next;
if (list->head == NULL)
list->tail = NULL;
else
element->next->prev = NULL;
} else {
element->prev->next = element->next;
if (element->next == NULL)
list->tail = element->prev;
else
element->next->prev = element->prev;
}

free(element);

list->size--;

return 0;
}

最佳答案

这一行是个坏消息:

if (element->next = NULL)(靠近 dlistdata.c 的底部)

您将 NULL 分配给 element->next 而不是检查它是否为 NULL

===

关于无法使用 C 中的 visual studio 从双向链表中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12792267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com