Thread: List - Why my delete function doesn't delete?

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    105

    List - Why my delete function doesn't delete?

    Hello everyone. I hope you can help me because I really don't know what to do

    I have to manage a Clinic. I need to delete a booking (for example, if John said he's coming on March 22nd at 15:30 but then he say he's not going to come, I have to delete that booking so another person can use it).

    idSearched: the id of the person that is not going to come.
    I have a lot of specialties and each one has a list. So I ask for the speciality to delete the node (the node contains John's booking). If I don't find it, I return (-1).
    searchSpecByID return a pointer to the list where the speciality is. So head will point to the first node of the list.
    In nodeToDelete I have the node I want to delete.

    The program detects OK when is the first in the list and when not, but it doesn't delete the node.

    Code:
    typedef struct bookingList{
        tSpecialty specialty;
        struct nodo* intro;
    }tList;
    
    int deleteNode(tClinic* clinic, char idSearched[MDNI])
    {
        tNode* nodeToDelete;
        tNode* previous=NULL;
        tNode* head=NULL;
        tList* searchedSpec;
        int esp;
    
        printf("\nSpecialty: ");
        scanf("%d", &esp);
        searchedSpec=searchSpecByID(clinica, esp);
        head=searchedSpec->intro;
        nodeToDelete=searchedNode(clinica, idSearched, esp);
        if(nodeToDelete==NULL)
            return -1;
        //If the list is empty
        if((head->next)==NULL)
        {
            printf("\nThe list is empty");
            return -1;
        }
    
        // if I want to delete the first one
        if(strcmp(head->dato->patient->dni, nodeToDelete->dato->patient->dni)==0)
        {
            head=nodeToDelete->next;
            searchedSpec->intro=head;
            free(nodeToDelete);
            return 0;
        }
    
        // the one I want to delete is not the first one
        previous=searchedSpec->intro;
    
        while(previous->next!=NULL && previous->next!=nodeToDelete)
            previous=previous->next;
    
        previous->next=previous->next->next;
        free(nodeToDelete);
        return 0;
    }
    I hope you can help me. I'm going to cry
    Last edited by juanjuanjuan; 12-09-2014 at 08:03 AM.

  2. #2
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I could fix it!!

    Now I have another problem:
    I have some nodes with information. I copy all the nodes to a binary file. Then, I delete one node and I want to have the binary files with all the nodes except the one I deleted. I suppose I have to overwrite the binary file. How?
    I tried to open the file with wb+ but it deletes all the nodes, not just the one I want

    Any suggestions?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There's not way to delete a node from the middle of the file and have it completely disappear. You have a few options, in the order I would suggest (though you should pick the best for your purpose):

    1. If you have a function that writes all the info you need from the in-memory lists to the binary file, simply delete the node from your variable, and re-write the whole thing to the file. I prefer this because you're reusing existing code instead of writing new code
    2. Read through the old file and write to the new temp file node-by-node. When you come to the deleted node, simply skip it -- don't write it to the new file. When done, remove the old file and rename the temp file to the correct name. This requires writing a new function, but it will keep your file from having old/stale data in it. However it's a bit less efficient than #1.
    3. "Lazy delete". Find a way of marking a node in the file as "unused" or "deleted". This could be an ID of -1 or a name of "********" or whatever you want to do. This requires writing a new function. It results in the least amount of writing to disk since you only update a single node instead of re-writing the whole clinic schedule, but it can cause your file to be huge as it fills with "unused/deleted" nodes.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    Quote Originally Posted by anduril462 View Post

    1. If you have a function that writes all the info you need from the in-memory lists to the binary file, simply delete the node from your variable, and re-write the whole thing to the file. I prefer this because you're reusing existing code instead of writing new code
    This is what I'm trying to do!

    Now, I delete the node from the list but, when I print all the binary file again, it appears again! (So, I didn't delete it). To put the lists in the binary file I used this:
    Code:
        FILE* fturnos;
    
        fturnos = fopen(TURNOSFILE, "rb");
        if(!fturnos){
            fturnos = fopen(TURNOSFILE, "wb+");
            return;
        }
    (the code continues but it is too long to post it here)
    I tried with that code again and it doesn't disappear. So, I tried to do this:
    Code:
        FILE* fturnos;
        fturnos=fopen(TURNOSFILE, "wb+");
    but it deletes all the information from the binary file and doesn't write the new information.

    Any suggestions? Thanks!!!

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Do some reading on fopen and the different modes: C Programming/C Reference/stdio.h/fopen - Wikibooks, open books for an open world.
    In the first snippet from #4, you attempt to open the file in read mode. If it succeeds, of course you wont be able to write to it, so it wont update without the deleted node. Only if it fails to open for reading, do you attempt to open it for writing. It's unlikely that opening "rb" will fail, but "wb+" will succeed.
    In the second snippet, fopen is doing exactly as it's supposed to.

    If the code is too long to post, then this is all the help I can give.

  6. #6
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    OK, so the 2nd is correct?

    This is the code:

    datebook is a struct that has year, month, day, hour and minutes of the booking
    in patientAux I save the patient I'm looking to eliminate
    Code:
    void copyBookings(tClinic* clinic){
        int id;
        char DNI[MDNI];
        tdate datebook;
        tBook* bookAux;
        tPatient* patientAux;
        tLista* Lista;
        FILE* fbooks;
    
        fbooks = fopen(TURNOSFILE, "wb+");
    
        while(fread(&id, sizeof(int), 1, fbooks)){
            bookAux = initializebook();
    
    
            fread(DNI, sizeof(char)*MDNI, 1, fbooks);
            fread(&datebook.yyyy, sizeof(int), 1, fbooks);
            fread(&datebook.mm, sizeof(int), 1, fbooks);
            fread(&datebook.dd, sizeof(int), 1, fbooks);
            fread(&datebook.hora, sizeof(int), 1, fbooks);
            fread(&datebook.min, sizeof(int), 1, fbooks);
    
            patientAux = searchByID(clinic, DNI);
            Lista = searchSpecByID(clinic, id);
    
            bookAux->patient->name = (char*)malloc(sizeof(char) * (strlen(patientAux->name) + 1));
    
            strcpy(bookAux->patient->dni, patientAux->dni);
            bookAux->patient->sexo = patientAux->sexo;
            strcpy(bookAux->patient->name, patientAux->name);
            bookAux->date.yyyy = datebook.yyyy;
            bookAux->date.mm = datebook.mm;
            bookAux->date.dd = datebook.dd;
            bookAux->date.hora = datebook.hora;
            bookAux->date.min = datebook.min;
    
            addToList(Lista, bookAux);
            printbooking(bookAux, id);
        }
        fclose(fbooks);
    
    }
    Thanks!!!!!!

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I don't have a clear picture of your whole program, and when and why you need to delete a patient. It's hard for me to figure out what copyBookings is really doing and how it fits in. I suspect a better name would be readBookingsFromFile or similar.

    I don't see where in copyBookings you actually compare the ID for the deleted appointment and skip or delete that appointment.

    My general suggestion, is to look at my post #3, and implement suggestion #1 from there: make a function that writes the datebook contents to a file (writeBookingsToFile). You read the database contents into memory and make all the changes you want, adding or deleting appointments, patients, etc. Then, when you need to update the database file, you simply re-write the entire datebook to the database file. So basically, when you open the program, you read the bookings from the file. The user makes changes: adds a patient, deletes an appointment, etc. Write the clinic information to the database whenever you need to. This may be only when the user exits (easy, minimal writes to disk), on an explicit "save changes to database" menu option (easy, write to disk when user says to) or after every operation (more writes to disk, have to put a writeToFile call after each change, but safer if the program crashes).

    I don't mean to sound rude here, but you've been working on this for a while, and you keep coming back with the same kind of mistakes. Mistakes that tend to point to poor planning and organization: you didn't have a clear picture of how this worked when you started, so you couldn't plan well for how all the pieces will fit together.

    And finally (here I mean to sound a little bit rude, because you kinda deserve it):
    Why don't you check the return value of fopen? How do you know if fread is working? Why do you cast the return value of malloc. I personally have told you a number of times, as have others here, to check for errors and not cast malloc. If you don't take our advice, we'll stop giving it.

  8. #8
    Registered User
    Join Date
    Feb 2014
    Posts
    105
    I fixed everything!
    I was using fread when I need to use fwrite. Thanks anduril, you're the best!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete function in singly linked list
    By moondrums in forum C Programming
    Replies: 1
    Last Post: 01-20-2014, 02:39 AM
  2. Function Free() does not delete the linked list?
    By hefese in forum C Programming
    Replies: 5
    Last Post: 08-11-2012, 07:39 AM
  3. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  4. backspacing over a string doesn't delete it
    By talz13 in forum C++ Programming
    Replies: 9
    Last Post: 04-22-2004, 09:38 AM
  5. File doesn't delete!!!!
    By moonwalker in forum Tech Board
    Replies: 6
    Last Post: 07-10-2003, 01:40 AM