Thread: delete function

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    delete function

    I am trying to delete current node in list

    after deleting current list should print 4, 3, 2, 1

    but program print strange value. How to delete current node

    Code:
    #include<stdio.h>#include<stdlib.h>
    
    
    struct node
    {
    	int data;
    	struct node *next;
    };
    
    
    void delete( struct node *current)
    {
       if (current != NULL)
       {
    	   free(current);
       } 	   
    }
    void instert( struct node **current, int value)
    {
    	struct node *new = malloc(sizeof(struct node));
    	
    	if ( new != NULL)
    	{
    		new-> data = value;
    		new -> next = *current;
    		*current = new;
    	}
    }
    
    
    void List(struct node *current)
    {
    	while (current !=NULL)
    	{
    		printf("%d ", current -> data);
    		current = current -> next;
    	}
    	
    }
    
    
    int main ()
    {
    	struct node *head = NULL;	
    	
    	instert(&head, 1); 
       	instert(&head, 2);
    	instert(&head, 3);
    	instert(&head, 4);
    	instert(&head, 5);
    	List(head);
    	delete (head);
    	printf("\n");
    	List(head);
    	return 0;
    }
    output

    Code:
    7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 7217968 7209152 7222088 ^C

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Shouldn't delete have a way to access the new head?

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    To delete a node from a list you need to modify the "next" pointer of the previous node in the list to point at the node following the node you're deleting.

    As it is, all your delete function does is free the memory of a node. The node itself is still in the list, but since the memory has been freed, if you try to access the now-freed node (including just to print the list), you'll have a use-after-free bug.

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by christop View Post
    To delete a node from a list you need to modify the "next" pointer of the previous node in the list to point at the node following the node you're deleting.

    As it is, all your delete function does is free the memory of a node. The node itself is still in the list, but since the memory has been freed, if you try to access the now-freed node (including just to print the list), you'll have a use-after-free bug.

    006A2310 // head node
    006A15C8 // node 2
    006A1598 // node 3
    00000000 // last node

    so pointer should be set to 006A15C8 to delete head node in list


    Code:
    #include<stdio.h>#include<stdlib.h>
     
     
    struct node
    {
        int data;
        struct node *next;
    };
     
     
    void delete( struct node *current)
    {
       while (current != NULL)
       {
           current = current-> next;
    	   printf(" %p \n", (void*)current-> next );
       }       
    }
    void instert( struct node **current, int value)
    {
        struct node *new = malloc(sizeof(struct node));
         
        if ( new != NULL)
        {
            new-> data = value;
            new -> next = *current;
            *current = new;
        }
    }
     
     
    void List(struct node *current)
    {
        while (current !=NULL)
        {
            printf("%d ", current -> data);
            current = current -> next;
        }
         
    }
     
     
    int main ()
    {
        struct node *head = NULL;   
         
        instert(&head, 1); 
        instert(&head, 2);
        instert(&head, 3);
        instert(&head, 4);
        instert(&head, 5);
        delete (head);
        printf("\n");
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    I suggest to draw, on paper, what you are trying to do...

  6. #6
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by flp1969 View Post
    I suggest to draw, on paper, what you are trying to do...
    Done to delete current node

    delete function-node1_delete-jpg

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void instert( struct node **current, int value)
    The reason you have a node** is because you need to be able to modify the head pointer in some situations.

    > void delete( struct node *current)
    Which is why this also needs to be a node**, because you need to be able to modify the head pointer in some situations.
    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.

  8. #8
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    > void instert( struct node **current, int value)
    The reason you have a node** is because you need to be able to modify the head pointer in some situations.

    > void delete( struct node *current)
    Which is why this also needs to be a node**, because you need to be able to modify the head pointer in some situations.
    I can search and delete node in list on the paper.

    I have to do two steps in the delete function in which i am struggling

    step 1 : search the node that need to delete
    step 2 : delete node if found in list and set pointer to point correct node

    Code:
    void delete( struct node **current)
    {
       while (current != NULL)
       {
       
       }       
    }

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I have to do two steps in the delete function in which i am struggling
    Look at how you use current in your insert function.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    > I have to do two steps in the delete function in which i am struggling
    Look at how you use current in your insert function.
    Code:
     void delete( struct node **current){
    	struct node *temp = *current;  // Node 1
    	
       while (temp != NULL)
       {
          temp = temp -> next;  // current node point to previous node in loop	  
       }       
    }
    I go from first node to last node in list but How to compare with the node i want to delete

  11. #11
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    This whole delete function can be solved and understood if you understand that C is a call by value language.
    If you understand what call by value is and what a pointer variable is(hint - A pointer variable is just a variable that can hold a memory address)... Then you'll have this whole delete function mess sorted out.
    Last edited by G4143; 01-26-2023 at 03:21 AM.

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Kittu20 View Post
    Done to delete current node

    delete function-node1_delete-jpg
    Ok... not what you thing you're doing, but what your code is currently doning, step by step...
    THEN you'll see your mistake.

  13. #13
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by flp1969 View Post
    Ok... not what you thing you're doing, but what your code is currently doning, step by step...
    THEN you'll see your mistake.
    Is there any mistake in the diagram. I do not think so.

  14. #14
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    There isn't a mistake in your diagram. There is a shortcoming in your understanding of C's call by value mechanism.

    Understand how C passes values to functions and you'll see what you have to do.

  15. #15
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Kittu20 View Post
    Is there any mistake in the diagram. I do not think so.
    OF COURSE there is a mistake.... Build the diagram step by step, following what the code is doing!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. List - Why my delete function doesn't delete?
    By juanjuanjuan in forum C Programming
    Replies: 7
    Last Post: 12-09-2014, 10:10 PM
  2. BST delete function
    By spikestar in forum C++ Programming
    Replies: 0
    Last Post: 08-31-2010, 08:55 PM
  3. Delete Function for AVL Trees
    By Lost in forum C Programming
    Replies: 5
    Last Post: 08-24-2009, 10:34 AM
  4. a simple delete function
    By manav in forum C++ Programming
    Replies: 20
    Last Post: 12-31-2007, 07:17 PM
  5. does C have a delete function??
    By aspand in forum C Programming
    Replies: 12
    Last Post: 05-17-2002, 03:14 PM

Tags for this Thread