Results 1 to 10 of 12

Thread: Delete Node function in C

Hybrid View

  1. #1
    Join Date
    Oct 2010
    Location
    Greece
    Beans
    240
    Distro
    Ubuntu 11.04 Natty Narwhal

    Delete Node function in C

    hey guys,
    i know that I have made already many posts but this one is tough! I want to make a function that deletes the Node when it finds a specific value
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node
    {
        struct node *next,*prev;
        int data;
    }Node;
    
    typedef struct list{
        Node *head,*tail;
        int len;
    }List;
    
    int find_len(List *list)
    {
        int counter = 0;
        Node *dummy;
        dummy = (*list).head;
        while(dummy != NULL)
        {
            counter = counter + 1;
            dummy = dummy->next;
        }
        list->len = counter;
        return 0;
    }
    
    int del1st(List *list,int value)
    {
        Node *dummy;
        dummy  = (*list).head;
        while(dummy != NULL)
        {
            if(dummy->data == value)
            {
                dummy->prev->next = dummy->next;
                free(dummy);
            }
            dummy = dummy -> next;
        }
        return 0;
    }
    
    int main()
    {
        Node *new1,*new2,*new3;
        List list;
        
        new1 = malloc(sizeof(Node));
        new2 = malloc(sizeof(Node));
        new3 = malloc(sizeof(Node));
        
        new1->data = 26;
        new2->data = 12;
        new3->data = 45;
        
        list.head = new1;
        list.tail = new3;
        
        list.head->next = new2;
        new2->next = new3;
        new3->next = NULL;
        
        new3->prev = new2;
        new2->prev = new1;
        
        find_len(&list);
        printf("LENGTH = %d\n",list.len);
        
        del1st(&list,12);
        
        printf("LENGTH = %d\n",list.len);
        
        return 0;
    }
    I put in the find_len function to be easier to count the nodes.

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Delete Node function in C

    Code:
            if(dummy->data == value)
            {
                dummy->prev->next = dummy->next;
                free(dummy);
            }
            dummy = dummy -> next;
    You cannot access dummy anymore after you free() it.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Oct 2010
    Location
    Greece
    Beans
    240
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Delete Node function in C

    Quote Originally Posted by Bachstelze View Post
    Code:
            if(dummy->data == value)
            {
                dummy->prev->next = dummy->next;
                free(dummy);
            }
            dummy = dummy -> next;
    You cannot access dummy anymore after you free() it.
    So if I make a pointer to dummy->next and then assign dummy to the the pointer?

  4. #4
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Delete Node function in C

    Quote Originally Posted by stamatiou View Post
    So if I make a pointer to dummy->next and then assign dummy to the the pointer?
    No, you cannot "make a pointer to dummy->next", because dummy does not point to anything anymore. What's that line for anyway? It doesn't seem to serve any purpose.
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Oct 2010
    Location
    Greece
    Beans
    240
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Delete Node function in C

    Quote Originally Posted by Bachstelze View Post
    No, you cannot make a pointer to dummy->next since dummy does not point to anything anymore. What's that line for anyway? It doesn't seem to serve any purpose.
    I mean before I free dummy, something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node
    {
        struct node *next,*prev;
        int data;
    }Node;
    
    typedef struct list{
        Node *head,*tail;
        int len;
    }List;
    
    int find_len(List *list)
    {
        int counter = 0;
        Node *dummy;
        dummy = (*list).head;
        while(dummy != NULL)
        {
            counter = counter + 1;
            dummy = dummy->next;
        }
        list->len = counter;
        return 0;
    }
    
    int del1st(List *list,int value)
    {
        Node *dummy;
        Node *temp;
        dummy  = (*list).head;
        while(dummy != NULL)
        {
            if(dummy->data == value)
            {
                temp = dummy->next;
                free(dummy);
                dummy = temp;
            }
            dummy = dummy -> next;
        }
        return 0;
    }
    
    int main()
    {
        Node *new1,*new2,*new3;
        List list;
        
        new1 = malloc(sizeof(Node));
        new2 = malloc(sizeof(Node));
        new3 = malloc(sizeof(Node));
        
        new1->data = 26;
        new2->data = 12;
        new3->data = 45;
        
        list.head = new1;
        list.tail = new3;
        
        list.head->next = new2;
        new2->next = new3;
        new3->next = NULL;
        
        new3->prev = new2;
        new2->prev = new1;
        
        find_len(&list);
        printf("LENGTH = %d\n",list.len);
        
        del1st(&list,12);
        
        printf("LENGTH = %d\n",list.len);
        
        return 0;
    }

  6. #6
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Delete Node function in C

    You're making it more complicated than it needs to be. Just make the function return after it has deleted the node:

    Code:
    int del1st(List *list,int value)
    {
        Node *dummy;
        dummy = list->head;
        while(dummy != NULL)
        {
            if(dummy->data == value)
            {
                if (dummy->prev != NULL) {
                    dummy->prev->next = dummy->next;
                }
                if (dummy->next != NULL) {
                    dummy->next->prev = dummy->prev;
                }
                free(dummy);
                return 0;
            }
            dummy = dummy->next;
        }
        return 1; /* Return 1 if object not found */
     }
    And if you want to "delete all nodes with value n", just do:

    Code:
    while (del1st(list, n) == 0)
        ;
    「明後日の夕方には帰ってるからね。」


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •