cannot delete from doubly linked list using visual studios in C

Posted by Jesus Sanchez on Stack Overflow See other posts from Stack Overflow or by Jesus Sanchez
Published on 2012-10-09T03:01:12Z Indexed on 2012/10/09 3:37 UTC
Read the original article Hit count: 311

Filed under:
|

hello I am currently doing an assignment that is supposed to read in a file, use the information, and then print out another file. all using doubly linked list. Currently i am trying to just read in the file into a doubly linked list, print it out onto the screen and a file, and finally delete the list and close the program. The program works fine as long as I don't call the dlist_distroy function which is supposed to delete the string. as soon as I do it the program starts running and then a window pops up saying

"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."

I have revised the destroy and remove functions and cant understand the problem. my program is the following

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, *element;
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))){

                    dlist_remove(list, current);
                    current=current->next;
                }
                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;

}

© Stack Overflow or respective owner

Related posts about c

    Related posts about visual-studio-2010