Thread: I want to delete node

  1. #1
    Registered User
    Join Date
    Jul 2018
    Posts
    81

    I want to delete node

    I tried to delete the second node in list

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct Node
    {
      int data;
      struct Node *next;
    };
      int main()
    {
      struct Node* head = NULL;
      struct Node* second = NULL;
      struct Node* third = NULL;
    
      head  = (struct Node*)malloc(sizeof(struct Node));
      second = (struct Node*)malloc(sizeof(struct Node));
      third  = (struct Node*)malloc(sizeof(struct Node));
     
      head->data = 1;     
      printf(" %d ", head->data);
      head->next = second; 
     
      second->data = 2;     
      printf(" %d ", second->data);
      
      second->next = third;
      
      free(second);
     
      third->data = 3;   
      printf(" %d ", third->data);
      third->next = NULL;
    
      return 0;
    }
    1 2 3

    free(second);

    but It doesn't delete
    Last edited by vajra11; 11-17-2019 at 06:25 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Oh it deletes it just fine.

    Your code is broken because it keeps a second reference to your deleted node (in the form of first->next).

    You need to stop hand rolling lists and write some functions like we showed you in other threads.

    Code:
    int main ( ) {
      struct Node* head = NULL;
      head = insertFront(head,1);
      head = insertFront(head,2);
      head = insertFront(head,3);
      printList(head);
      // search for the node with the value 2 and remove it.
      // repair the list to ensure the node prior to node 2 
      // ends up pointing to the node after 2
      head = deleteNode(head,2);
      printList(head);
    }
    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.

  3. #3
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Oh it deletes it just fine.

    You need to stop hand rolling lists and write some functions like we showed you in other threads.
    I am still struggling a lot to make delete function
    this is my idea
    Code:
    struct node* deleteNode(int number, struct node *currentPointer) {
        struct node *previousPointer = malloc(sizeof(*previousPointer ));
        
        if (currentPointer->next ==NULL)
        {
            printf("Node not found \n");
        }
        
        else 
        {
            previousPointer->next = currentPointer->next ;
            
            free(currentPointer);
        }
             
        return previousPointer;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Deleting a node should only call free()

    Why do you want to make a new node, if all you're doing is trying to remove one.

    Try something simple first, just remove the first node of the list unconditionally.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Deleting a node should only call free()

    Why do you want to make a new node, if all you're doing is trying to remove one.

    Try something simple first, just remove the first node of the list unconditionally.
    I can only remove last node but I want to delete any node

    Code:
    #include<stdio.h>#include<stdlib.h> 
        
    struct node{
      int Number;
      struct node *next;
    };
      
    struct node* newNode(int number, struct node *next) {
        struct node *new = malloc(sizeof(*new));
               new->Number = number;
               new->next = next;
        return new;
    }
    
    
      
    void show(struct node *head){
         struct node *c;
         c = head;
         while (c!=NULL){
               printf("%d\n",c->Number);
               c = c->next;
               }
       
         }
    
    
    struct node* deleteNode(int n,  struct node* node)
    {
        struct node *first1, *n_th_node, *save;
        int count = 0;
        save = first1 = n_th_node = node;
        while(first1->next)
        {
            first1 = first1->next;
            count++;
            if(count == (n-1))
            break;
        }
    
    
        while ( first1->next != NULL )
        {
            first1 = first1->next;
            save = n_th_node;
            n_th_node = n_th_node->next;
        }
    
    
        save->next = n_th_node->next;
        free(n_th_node);
        return node;
    
    
    }
    
    
           
    int main (void ) {
       struct node *head = NULL;  
        
    head = newNode(10, head);
    head = newNode(20, head);
    head = newNode(30, head);
    
    
    show(head);
    head = deleteNode(2, head);
    
    
    printf("new list \n");
    show(head);
           
        return 0;
    }
    Last edited by vajra11; 11-19-2019 at 11:46 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your first while loop should be something like
    Code:
    prev = NULL;
    while ( node && count < n ) {
        prev = node;
        node = node->next;
        count++;
    }
    When the loop exits, you have these conditions to deal with.
    1. prev may be NULL
    2. node may be NULL
    3. count may be == n

    Work out what each of those things means for your list.
    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.

  7. #7
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    Your first while loop should be something like
    Code:
    prev = NULL;
    while ( node && count < n ) {
        prev = node;
        node = node->next;
        count++;
    }
    When the loop exits, you have these conditions to deal with.
    1. prev may be NULL
    2. node may be NULL
    3. count may be == n

    Work out what each of those things means for your list.
    code remove fist and last node
    Code:
    #include<stdio.h>#include<stdlib.h> 
         
    struct node{
      int Number;
      struct node *next;
    };
       
    struct node* newNode(int number, struct node *next) {
        struct node *new = malloc(sizeof(*new));
               new->Number = number;
               new->next = next;
        return new;
    }
     
     
       
    void show(struct node *head){
         struct node *c;
         c = head;
         while (c!=NULL){
               printf("%d\n",c->Number);
               c = c->next;
               }
        
         }
     
     
    struct node* deleteNode(int n,  struct node* node)
    {
        struct node *first1, *n_th_node, *save;
        int count = 0;
        save = first1 = n_th_node = node;
      
        first1 = NULL;
        while ( node && count < n ) {
        first1 = node;
        node = node->next;
        count++;
    }
     
        while ( first1->next != NULL )
        {
            first1 = first1->next;
            save = n_th_node;
            n_th_node = n_th_node->next;
        }
     
        save->next = n_th_node->next;
        free(n_th_node);
        return node;
    }
     
            
    int main (void ) {
       struct node *head = NULL;  
         
    head = newNode(10, head);
    head = newNode(20, head);
    head = newNode(30, head);
    head = newNode(40, head);
    head = newNode(50, head);
    head = newNode(60, head);
     
    show(head);
    head = deleteNode(2, head);
     
     
    printf("new list \n");
    show(head);
            
        return 0;
    }
    60
    50
    40
    30
    20
    10
    new list
    40
    30
    10

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So what's the purpose of your 2nd while loop.

    You only need one while loop, and that one I posted.

    At that point, node is pointing to the node you want to delete.
    You don't need another loop.
    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.

  9. #9
    Registered User
    Join Date
    Jul 2018
    Posts
    81
    Quote Originally Posted by Salem View Post
    You don't need another loop.
    I tried single while loop in function code doesn't delete specif node.
    Code:
    #include<stdio.h>
    #include<stdlib.h> 
          
    struct node{
      int Number;
      struct node *next;
    };
        
    struct node* newNode(int number, struct node *next) {
        struct node *new = malloc(sizeof(*new));
               new->Number = number;
               new->next = next;
        return new;
    }
      
      
        
    void show(struct node *head){
         struct node *c;
         c = head;
         while (c!=NULL){
               printf("%d\n",c->Number);
               c = c->next;
               }
         
         }
      
      
    struct node* deleteNode(int n,  struct node* node)
    {
        struct node *first1, *n_th_node, *save;
        int count = 0;
        save = first1 = n_th_node = node;
       
        first1 = NULL;
        while ( node && count < n ) {
        first1 = node;
        node = node->next;
        count++;}
    
    
        return node;
    }
      
             
    int main (void )
     {
       struct node *head = NULL;  
          
    head = newNode(10, head);
    head = newNode(20, head);
    head = newNode(30, head);
    head = newNode(40, head);
    head = newNode(50, head);
    head = newNode(60, head);
      
    show(head);
    head = deleteNode(3, head);
      
      
    printf("new list \n");
    show(head);
             
        return 0;
    }
    60
    50
    40
    30
    20
    10
    new list
    30
    20
    10

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I tried single while loop in function code doesn't delete specif node.
    That's because you didn't read what I wrote.

    Quote Originally Posted by myself
    When the loop exits, you have these conditions to deal with.
    1. prev may be NULL
    2. node may be NULL
    3. count may be == n

    Work out what each of those things means for your list.
    Code:
    struct node *deleteNode(int n, struct node *head ) {
      struct node *node = head;
      struct node *prev = NULL;
      while ( node && count < n ) {
        prev = node;
        node = node->next;
        count++;
      }
    
      if ( prev == NULL ) {
        // it's the head node being deleted
        head = node->next;
        free(node);
      } else
      if ( node == NULL ) {
        // ran off the end of the list, nothing to do
      } else {
        prev->next = node->next;
        free(node);
      }
      return head;
    }
    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.

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    What textbook are you using? This stuff is very easy to understand but you shouldn't be jumping straight into code you should be drawing linked lists on a piece of paper. After drawing them on paper and doing something like, for example, deleting a node then translating that to working code is trivial. If your textbook doesn't show what linked lists look like using a pretty picture and if it doesn't have exercises where it asks you to draw the operations down on paper and work with them for 10 minutes or so then time for a new textbook I reckon. I.e. if you cannot do this on paper, with a pencil, then how are you going to code it?!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete last node
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 08:08 AM
  2. cant get this delete node working
    By qubit67 in forum C Programming
    Replies: 3
    Last Post: 04-25-2007, 02:36 AM
  3. BST - delete node
    By marrk in forum C Programming
    Replies: 5
    Last Post: 12-20-2006, 10:46 AM
  4. Delete node
    By AmazingRando in forum C Programming
    Replies: 1
    Last Post: 09-23-2003, 04:44 PM
  5. Delete node!!!!!
    By khpuce in forum C Programming
    Replies: 3
    Last Post: 05-31-2003, 06:33 AM

Tags for this Thread