3 Replies - 165 Views - Last Post: 24 May 2019 - 04:48 PM Rate Topic: -----

#1 mikkyspence   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 24-May 19

Nodes aren't deleting in a singly linked list when they should be

Posted 24 May 2019 - 02:53 PM

Hey, there!
I made a code that alters a singly linked list in which it prints out the entire list, and is supposed to delete all of the list's nodes. I started with a different code for deleting all nodes, however soon realized that no nodes seem to actually delete. So I altered the code a few times and thought that maybe when I go through each node and delete it, it'll work to delete all of them.

Placing the function printList (to print the list) after deleteAll (to delete all nodes) returns the entire (original) list again as an output, instead of "EMPTY LIST", where the head is supposed to be NULL. I receive no errors when executing the program, it just acts weird by not doing the work I want it to do. I thought that maybe, somehow, the nodes are being restored after deleting, or that I'm seriously not doing any work with my deleteAll function.

Could someone tell me what went wrong here?

void printList(struct node* head_ref){
	if(head_ref == NULL)
	printf("EMPTY LIST");
	while(head_ref != NULL){
		printf("%d\n", head_ref -> data);
		head_ref = head_ref -> next;
	}
}

void deleteAll(struct node** head_ref, int count)
{    
    struct node *temp, *tails = *head_ref;
    if(count == 0){
    	while(temp -> next != NULL){
    	count++;
		tails -> next = temp -> next;
    	free(temp);
    	return;
		}
	}
}



How I call the functions under int main() - in case knowing this can be helpful:
	int count = 0;
	deleteAll(&head, count);
	printList(head);



Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Nodes aren't deleting in a singly linked list when they should be

#2 jimblumberg   User is offline

  • member icon

Reputation: 5733
  • View blog
  • Posts: 17,568
  • Joined: 25-December 09

Re: Nodes aren't deleting in a singly linked list when they should be

Posted 24 May 2019 - 03:20 PM

Do you realize that just freeing the memory doesn't actually modify any memory, it just frees the memory for reuse. Also do you realize that your print function is invoking undefined behavior when it tries to print the "freed" memory?




Jim
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,665
  • Joined: 05-May 12

Re: Nodes aren't deleting in a singly linked list when they should be

Posted 24 May 2019 - 03:21 PM

There's a several issues:
You never set head_ref to NULL, so it is still pointing to the various nodes that you have already freed. This will invoke undefined behavior.
You are using the uninitialized variable temp. This will invoke undefined behavior.
You have the wrong logic in your while loop for deleting nodes.

When undefined behavior is invoked, anything can happen -- including making it look like your code is not working properly, but at the same time not necessarily crash.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7442
  • View blog
  • Posts: 15,437
  • Joined: 16-October 07

Re: Nodes aren't deleting in a singly linked list when they should be

Posted 24 May 2019 - 04:48 PM

If you can remove one item, then you can clear the list.

Here's some quick test code:
typedef struct node_s {
    int data;
    struct node_s *next;
} node;

void printList(node *);
void deleteAll(node **); // you really shouldn't need **, but if you must
node *pushList(node *, int);
node *popList(node *, int *);

int main(int argc, char * argv[]) {
    int n;
    node *xs = pushList(pushList(NULL, 3),4);
    printList(xs);
    xs = popList(xs, &n); printf("%d popped\n", n);
    printList(xs);
    xs = pushList(xs, 5);
    printList(xs);
    deleteAll(&xs);
    printList(xs);

    return 0;
}

void printList(node *p) {
    printf("printList: ");
    if (p == NULL) {
        printf("EMPTY LIST");
    } else {
        for(; p!=NULL; p = p->next) { printf("%d ", p->data); }
    }
    printf("\n");
}



Results:
pushList 3
pushList 4
printList: 4 3
popList 4
4 popped
printList: 3
pushList 5
printList: 5 3
deleteAll begin
popList 5
popList 3
deleteAll end
printList: EMPTY LIST



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1