1
\$\begingroup\$

I have written a working code that generates a simple linked list of dates and associated holidays. I realize that I have a problem with memory leaking as I copy lists repeatedly, so I wrote a freeList function. I am just having issues determining where to place this function.

//Creates a linked list of dates/holidays

#include <stdio.h>
#include <stdlib.h>

typedef struct hashElem{
    char* date;
    char* holiday;
    struct hashElem* next;

}hashElem;

// Initialize list with input value
hashElem* createNode(char* input_date, char* input_holiday)
{  
    hashElem* ptr = malloc(sizeof(*ptr));

    if (ptr == NULL) 
    {
        perror("Error");
        return NULL;
    }

    ptr -> date = input_date;
    ptr -> holiday = input_holiday;
    ptr -> next = NULL;

    return ptr;
}

void freeList(hashElem* head)
{
    hashElem* ptr = head;
    while (head != NULL)
    {
        ptr = ptr->next;
        free(head);
        head = ptr;
    }
}

hashElem* addToTail(hashElem* head, char* input_date, char* input_holiday)
{
    hashElem* ptr = malloc(sizeof(*ptr));
    hashElem* temp = head; 

    // If there is not yet a list, initialize the list with the input value
    if (head == NULL)
        return createNode(input_date, input_holiday);

    if (ptr == NULL)
        return NULL;
    else
    {
        ptr->date = input_date;
        ptr->holiday = input_holiday;
        ptr->next = NULL;   

        // Find the last element of the list
        while (temp->next != NULL)
            temp = temp->next;

        // Add new node to the end of the list
        temp->next = ptr;
        ptr = head;

        //free(head);
        return ptr;
    }
}

hashElem* holidayList(void)
{
    hashElem* List = NULL;
    List = addToTail(List, "01/01/13", "New Years Day");
    List = addToTail(List, "14/02/13", "Valentine's Day");
    List = addToTail(List, "26/03/13", "Start of Passover");
    List = addToTail(List, "16/06/13", "Father's Day");
    List = addToTail(List, "31/10/13", "Halloween");
    List = addToTail(List, "28/11/13", "Thanksgiving");
    List = addToTail(List, "25/12/13", "Christmas");

    return List;
}


void printList(hashElem* head)
{
    hashElem* ptr = head;

    printf("------------------------------\n");
    printf("PRINTING LINKED LIST\n");
    while (ptr != NULL)
    {
        printf("Date: %s,  Holiday: %s\n", ptr->date, ptr->holiday);
        ptr = ptr->next;
    }

    printf("------------------------------\n");
}
\$\endgroup\$
1
\$\begingroup\$

In your current code, I can't see the copying of lists repeatedly as you stated. You are using pointers here so you're not actually duplicating the list.

The freeList function should be called once you're done using your list (the one returned by holidayList()).

Side Note: You can refactor the code by utilizing createNode function when creating nodes and adding it to the tail.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.