0

你好我目前正在做一个应该读入文件,使用信息,然后打印出另一个文件的作业。全部使用双向链表。目前我正在尝试将文件读入双向链表,将其打印到屏幕和文件上,最后删除列表并关闭程序。只要我不调用应该删除字符串的 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."

我已经修改了销毁和删除功能,但无法理解问题所在。我的程序如下

主程序

#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;
}
4

2 回答 2

3

这条线是个坏消息:

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

您分配NULLtoelement->next而不是检查它是否是NULL

===

于 2012-10-09T03:36:20.813 回答
1

由于这是一项任务,我的回答是为您指明正确的方向,但我不会完全说明。

你觉得这里会发生什么?

dlist_remove(list, current);
current = current->next;
于 2012-10-09T03:39:42.617 回答