Clearing A Dynamic Linked List with a Single Stored Element Crashes Program

For my scholastic assignment I have been given the task of creating a Dynamic Set ADT using linked list. My program is functioning properly, but when I attempt to clear a Set which only has a single element (ergo, single node)my program crashes.

Here is how my linked list is defined:

typedef struct node{

    int value;
    struct node *next;

}Node;

typedef struct List{

    Node *head;
    Node *tail;
}*SetList;

And here are the relevant functions:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "List.h"

SetList ListCreate(){

    SetList lptr;
    lptr = malloc(sizeof(SetList));
    lptr->head = NULL;
    lptr->tail = NULL;
    return lptr;
}

SetList ListAdd(SetList lptr, int x){

    if(List_Is_Element_Of(x, lptr)){
            puts("Value already exists, enter a unique value");
    }
    else{
        Node *new = malloc(sizeof(Node));
        new->value = x;
        new->next = NULL;

        if(lptr->head == NULL){
            lptr->head = new;
            lptr->tail = new;
        }
        else{
            lptr->tail->next = new;
            lptr->tail = new;
        }
    }
        return lptr;
}


SetList ListRemove(SetList lptr, int x){

    if(lptr == NULL){
            puts("Memory Allocation Failed. Shutting down.");
            exit(EXIT_FAILURE);
    }
    else if(!List_Is_Element_Of(x, lptr)){
        puts("Value is not in set");
    }
    else if(lptr->head == NULL){
        puts("Set is empty. Cannot remove that which does not exist");
    }
    else{
        if(lptr->head->value == x){
        Node *tempnode = malloc(sizeof(Node));
        tempnode =  lptr->head;
        lptr->head = tempnode->next;
        free(tempnode);
        }
        else{
        Node *tempnode = malloc(sizeof(Node));
        tempnode =  lptr->head;
        Node *tempnode2 = malloc(sizeof(Node));
        tempnode2 = tempnode;
        while(tempnode->value != x){
            tempnode2 = tempnode;
            tempnode = tempnode->next;
        }
        tempnode2->next = tempnode->next;
        free(tempnode);
        }
    }
    return lptr;
}

SetList ListClear(SetList lptr){

    SetList templist = ListCreate();
    templist = lptr;
    while(templist->head!= NULL){
        ListRemove(templist, templist->head->value);
        templist->head = templist->head->next;
    }
    free(templist);
    return lptr;
}

I am not very experienced when it comes to pointers so I'm not entirely sure where the issue arises from. Removing a list with a single element stored works, but clearing it does not.

Thank you in advance for reading.

Answers


Your clear function doesn't change Tail, although you don't seem to use tail (except that you give it a different node than head when you add a node?).

When clear removes the last element, head should be null, so dereferencing it after removing that last element would be bad.

Plus what @NotLikeThat said.


If I'm reading it right:

SetList ListClear(SetList lptr){

    SetList templist = ListCreate();   // Create a new list
    templist = lptr;                   // Immediately throw away the new list
                                       //  by overwriting the pointer with the old one
    while(templist->head!= NULL){      // Delete everything from the old list
        ListRemove(templist, templist->head->value);
        templist->head = templist->head->next;
    }
    free(templist);                    // Free the old list
    return lptr;                       // Return a pointer to freed memory
}

If you want to clear the list by removing all the elements, there shouldn't be the need to create a new one, just iterate over the existing list removing the elements until it's empty. All you really need in that function is the while loop.

I think ListRemove() should also be checking if it's removing the last element and updating tail accordingly if so.


Need Your Help

Undefined error I can't figure out

flash actionscript-3

Hey guys...having a math problem. Trying to layout a grid of gray boxes which I want to load thumbnails on top of. I keep getting an error when trying to load the 4th thumbnail. If you remove the 4th