Thread: Implementing a medical office. – Linked List

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    61

    Implementing a medical office. – Linked List

    So far i got new patient and show_queue (working).

    I got a little doubt

    When i make malloc should i put the patient i just create to NULL or that don΄t matter ?

    Code:
        // 1Ί Get Memory from the new patient, and putting the block to NULL
        if ((new_patient = malloc(sizeof (node))) == NULL) {
            printf("Something went wrong -> %s\n", strerror(errno));
            return list;
        }
        new_patient->next = NULL; // Put the NULL the patient i just create

    Full Functions
    Code:
    node * new_patient(node * list) {
    
    
        node * new_patient;
        node * aux;
    
    
        // 1Ί Get Memory from the new patient, and putting the block to NULL
        if ((new_patient = malloc(sizeof (node))) == NULL) {
            printf("Something went wrong -> %s\n", strerror(errno));
            return list;
        }
        new_patient->next = NULL; // Put the NULL the patient i just create
    
    
        // 2Ί Getting the input from the user
        puts("What is the name of the new patient ?");
        scanf(" %[^\n]", new_patient->name);
    
    
        // 3Ί Insert on the list
        if (list == NULL) {
            list = new_patient;
        } else {
            aux = list;
    
    
            // Going to run the list until i reach the end.
            while (aux->next != NULL) {
                aux = aux->next;
            }
    
    
            // Put the last one pointing to the new block
            aux->next = new_patient;
        }
    
    
        return list;
    }
    
    
    void show_queue(node * list) {
        
        if (list == NULL) {
            printf("The list is empry\n");
        } else {
            while (list != NULL) {
                printf("The name of the patient is %s\n", list->name);
                list = list->next;
            }
        }
        
    }
    The rest of the work is on progress.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Yes, you should set it to NULL.

    When you use malloc, there is no guarantee what value is in the memory you just allocated. It could be anything. That means that if you add new_patient to the end of the list, and you didn't set the next pointer to NULL, it would probably point to some arbitrary piece of memory and you wouldn't have an end to your list. Furthermore, the arbitrary address in new_patient->next is likely memory you don't have access to, so trying to access it would result in undefined behavior. See here for more info on undefined behavior.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    61
    I got two more functions but i am a little lost where i should use free function.

    Function 1 - Take care of the first client on the list.

    Code:
    node * see_patient(node * list) {
    
    
        node * current;
    
    
        current = list;
    
    
        // Going to remove the frist patient of the list.
        if (list == NULL) {
            puts("The list is empty");
        } else {
            printf("I am going to see the patient with the name ->: %s\n", list->name);
            list = current->next;
            free(current);
        }
    
    
        return list;
    }
    Function 2 - The patient is tired of waiting and going to give up. Once again i am little bit lost where i should use free function.

    Code:
    node * tired_of_waiting(node * list) {
    
    
        node * current = list;
        node * before = NULL;
        char name[SIZE];
    
    
        if (list == NULL) {
            printf("The list is empty.\n");
            return list;
        } else {
            printf("What is the name of the patient that is tried to waitting and going to leave ?\n");
            scanf(" %[^\n]", name);
        }
        getchar();
    
    
        // Put the pointers in place.
        while (current != NULL && strcmp(current->name, name) != 0) {
            before = current;
            current = current->next;
        }
    
    
        if (current == NULL) {
            puts("That patient is not waitting");
            return list;
        } else {
            if (before == NULL) { // The first on the list
                list = current->next;
            } else {
                before->next = current->next;
            }
    
    
            printf("The client with the name %s, was tired of waiting and went away.", current->name);
            free(current); // Is important here ? should i free "before" too ?
            return list;
        }
    
    
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,415
    Both look good to me.
    You're freeing the right node in the right place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-17-2017, 02:08 AM
  2. Segmentation Fault -- Stack implementing a Linked List
    By jackalclone1 in forum C Programming
    Replies: 10
    Last Post: 02-15-2014, 12:44 AM
  3. Replies: 4
    Last Post: 05-31-2011, 11:34 AM
  4. Implementing a linked list, some problems
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2003, 09:07 AM
  5. implementing graph using linked list
    By Tim in forum C Programming
    Replies: 1
    Last Post: 11-20-2001, 02:00 AM

Tags for this Thread