无法使用C中的视觉工作室从双向链表中删除

时间:2012-10-09 03:01:13

标签: c visual-studio-2010

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

我修改了破坏和删除功能,无法理解问题。我的程序如下

的main.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;
}

2 个答案:

答案 0 :(得分:3)

这条线是坏消息:

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

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

== vs =

答案 1 :(得分:1)

由于这是一项任务,我的答案是指出你正确的方向,但我不会完全拼写出来。

你觉得这里发生了什么?

dlist_remove(list, current);
current = current->next;