Thread: delete function

  1. #16
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by G4143 View Post
    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.
    This is what I understand call by value and call by address

    Code:
      #include<stdio.h>
    
    void bar ( int fvar)
    {
        fvar++;
    }
    void foo ( int *ptr)
    {
        *ptr = 1000;      // This line change value of y 
    }
    int main()
    {
        int x = 1;
        int y = 100;
        printf(" value of x before to functional call is %d \n", x);
        printf(" value of y before to functional call is %d \n", y);
        bar (x) ;        // pass value of variable x to function bar 
        printf(" value of x after function call is %d \n", x);
        foo (&y);        // pass address of variable y to function foo
        printf(" value of y after to function call is %d \n", y);
        
        return 0;
    }
    Output
    Code:
     
    
    
     value of x before to functional call is 1
     value of y before to functional call is 100
     value of x after function call is 1
     value of y after to function call is 1000

  2. #17
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by flp1969 View Post
    OF COURSE there is a mistake.... Build the diagram step by step, following what the code is doing!!!
    This what I understand up to 4 nodes

    delete function-node1_delete-jpg

  3. #18
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Kittu20 View Post
    This what I understand up to 4 nodes
    And what are you doing on delete()?

  4. #19
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Kittu20: What exactly do you want your delete function to do? That is, what do you expect the "current" parameter to be? Is it always the head of the list, so you can delete only the first node in a list? If that's the case then it's a very easy task.

    First define exactly what the function does and what the function's parameters are supposed to mean, and then design and write the function. If you do it in the other order, you're gonna have a bad time.

  5. #20
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by flp1969 View Post
    And what are you doing on delete()?
    You told me to draw a procedure to delete node in list on paper, I did that first.

    After that how to insert node in the list, you asked to make it on paper, I did that too.

    i don't know what's wrong with my drawing. as i said before i don't see anything wrong. can you please point out my mistake so that i can think about it.

  6. #21
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by christop View Post
    Kittu20: What exactly do you want your delete function to do? That is, what do you expect the "current" parameter to be? Is it always the head of the list, so you can delete only the first node in a list? If that's the case then it's a very easy task.

    First define exactly what the function does and what the function's parameters are supposed to mean, and then design and write the function. If you do it in the other order, you're gonna have a bad time.
    I want to create a function that can delete any node from the list. but i am struggling to write the function. If you ask me to delete any node in my list, I can show it on paper.

  7. #22
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    I suggest for you to read "Algorithms in C" by Robert Sedgewick. There's a nice implementation of single linked lists there I bet you didn't thought about...
    Last edited by flp1969; 01-26-2023 at 12:05 PM.

  8. #23
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Kittu20 View Post
    I want to create a function that can delete any node from the list. but i am struggling to write the function. If you ask me to delete any node in my list, I can show it on paper.
    OK, then show on paper how you would delete any node in the list. Then you will see what information you need to give to your function for it to do its job.

  9. #24
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by christop View Post
    OK, then show on paper how you would delete any node in the list. Then you will see what information you need to give to your function for it to do its job.
    I have already done please look at image in my previous post where i am deleting the current node form list

  10. #25
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    I see a diagram in #12, but it's deleting only the first node (what you misleadingly call the "current" node). Again, how would you delete any node in the list, not just the first node?

  11. #26
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by christop View Post
    I see a diagram in #12, but it's deleting only the first node (what you misleadingly call the "current" node). Again, how would you delete any node in the list, not just the first node?
    In figure, I am showing what will be the list if one specific node delete from five nodes

    delete function-temp-jpg

  12. #27
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    #include<stdio.h>
    #include<stdlib.h>
     
    struct node
    {
        int data;
        struct node *next;
    };
     
    void delete( struct node **current, int value)
    {
        // your code here
    }
    
    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;
        }
        printf("NULL\n");
    }
     
     
    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,4);
        List(head);
        delete (&head,3);
        List(head);
        delete (&head,5);
        List(head);
        printf("\n");
        return 0;
    }
    Your first task is to make delete just find the element to delete.

    You'll need
    - a while loop
    - an if statement
    - and printf("Found node containing %d\n", value);

    Don't try to delete the node yet, just make sure your code is right at just being able to find the node to delete.
    Baby steps.
    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.

  13. #28
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    Your first task is to make delete just find the element to delete.

    You'll need
    - a while loop
    - an if statement
    - and printf("Found node containing %d\n", value);

    Don't try to delete the node yet, just make sure your code is right at just being able to find the node to delete.
    Baby steps.
    Done it
    Code:
     #include<stdio.h>#include<stdlib.h>
     
    struct node
    {
        int data;
        struct node *next;
    };
     
    void delete( struct node **current, int value)
    {
    	struct node *temp = *current;
    	
        while (temp != NULL)
    	{
    		if ( temp -> data == value)
    		{		
    			printf("Found node containing %d\n", value);
    		}
    		temp = temp->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;
        }
        printf("NULL\n");
    }
     
     
    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,4);
        List(head);
        delete (&head,3);
        List(head);
        delete (&head,5);
     //   List(head);
        printf("\n");
        return 0;
    }
    Code:
    5 -> 4 -> 3 -> 2 -> 1 -> NULLFound node containing 4
    5 -> 4 -> 3 -> 2 -> 1 -> NULL
    Found node containing 3
    5 -> 4 -> 3 -> 2 -> 1 -> NULL
    Found node containing 5

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    OK, now in the delete function, also print out whether the found node is the head node, or one of the other nodes.

    The head node is special, it takes extra care to delete it, so you need to know which it is.
    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.

  15. #30
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    OK, now in the delete function, also print out whether the found node is the head node, or one of the other nodes.

    The head node is special, it takes extra care to delete it, so you need to know which it is.
    exactly at this stage i'm having trouble. I am getting the position of the node by comparing the data value.

    Can the position of a node be detected without comparing the data value, if yes how?

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