Thread: I need help to delete new node

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    54

    I need help to delete new node

    My list contain with three nodes I want to delete new node. I do not understand how to delete new node

    I want this output 123 13

    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
    
    
    typedef struct node
    {
        int n;
        struct node *next;
    }node;
    
    int main()
    {
     node *current = NULL;
     node *new = NULL;
     node *last = NULL;
     
     node *head = NULL;
     
     current = malloc(sizeof(current));
     new = malloc(sizeof(new));
     last = malloc(sizeof(last));
     
       if (current == NULL)
           printf ("Memory not Allocated \n");
       else 
           printf ("Memory Allocated \n");
           current->n = 1;
           current->next = new;
           new->n = 2;
           new->next= last;
           last->n = 3;
           last->next= NULL;
           
           free(new);
           
           for (head = current; head != NULL; head = head->next)
               {
                   printf("%d\n", head->n);
               } 
    
         return 0;
    }
    output
    14090432
    14104256
    14093800
    14095320

    garbage value

    Last edited by Player777; 01-08-2020 at 01:37 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Your problem is that you aren't setting head to point to the head of the linked list.

    I suggest that you restrict yourself to having three node pointers: head, tail, and current. head points to the start node of the linked list, or is a null pointer if the linked list is empty. tail points to the end node of the linked list, or is a null pointer if the linked list is empty. current is used to iterate over the linked list, i.e., instead of changing head, you set current to head then change current to go to the next node. To build the linked list, you update head and tail in order to append the three nodes. (I'm assuming that you want to append, which is why I suggest having tail; if you only want to prepend, just having head will do.)

    That said, if you're going to delete the newest node before printing the linked list, why create it in the first place?
    Last edited by laserlight; 01-08-2020 at 02:33 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    54
    Quote Originally Posted by laserlight View Post
    That said, if you're going to delete the newest node before printing the linked list, why create it in the first place?
    We can add or delete anything in the list at any time. I made a list. Now I wanted to add something to it so I added a new node and after that I wanted to delete something so I was trying to delete the second node.

    1. Create list
    2. Add node
    3. Delete node

    Note : I do not want to create new functions to add or delete node , I want to do all three operations in main without creating other functions.

    I know it's good to create a function, but for now I don't want to do that, that would be my next task

    Quote Originally Posted by laserlight View Post
    Your problem is that you aren't setting head to point to the head of the linked list.

    I suggest that you restrict yourself to having three node pointers: head, tail, and current.
    I have changed pointer name as you suggested

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    typedef struct node
    {
        int n;
        struct node *next;
    }node;
    int main()
    {
      
     node *Current = NULL;
      node *Head = NULL;
     node *New = NULL;
     node *Tail = NULL;
     
     
     Head = malloc(sizeof(Head));
     New = malloc(sizeof(New));
     Tail = malloc(sizeof(Tail));
     
       if (Head == NULL)
           printf ("Memory not Allocated \n");
       else 
           printf ("Memory Allocated \n");
           Head->n = 1;
           Head->next = New;
           New->n = 2;
           New->next= Tail;
           Tail->n = 3;
           Tail->next= NULL;
          
           for (Current = Head; Current!= NULL; Current = Current->next)
               {
                   printf("%d\n", Current->n);
               } 
    
    
         return 0;
    }
    Memory Allocated
    1
    2
    3

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Are you all doing the same course? If there are only 3 nodes you'd be better off using an array. Or, if you want to simulate a linked list just do:

    Code:
    #include <stdio.h>
    
    typedef struct node {
        int n;
        struct node *next;
    } node;
    
    int main(void)
    {
        node list[] = {
            { 1, &list[1] },    /* head */
            { 2, &list[2] },    /* silly */
            { 3, NULL }         /* tail */
        };
            
        for (node *curr = list; curr; curr = curr->next) {
            printf("%d\n", curr->n);
        }
        
        return 0;
    }
    Not much point in doing that though except to, perhaps, see how linked lists work

    I guess what I'm saying is that unless you're using functions to construct the list there is essentially no point in having a linked list. By the way, in your version you're not freeing the memory you allocated using malloc(), so that's a problem

    Or, if you want to be really cunning
    Code:
    #include <stdio.h>
    
    typedef struct node {
        int n;
        struct node *next;
    } node;
    
    int main(void)
    {
        node list[] = { { 1, &list[2] }, { 3, NULL }, { 2, &list[1] } };
            
        for (node *curr = list; curr; curr = curr->next)
            printf("%d\n", curr->n);
        
        return 0;
    }
    Last edited by Hodor; 01-08-2020 at 06:04 AM.

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    54
    Quote Originally Posted by Hodor View Post

    Not much point in doing that though except to, perhaps, see how linked lists work

    Edit: I guess what I'm saying is that unless you're using functions to construct the list there is essentially no point in having a linked list. By the way, in your version you're not freeing the memory you allocated using malloc(), so that's a problem
    I know this is better way
    Code:
    #include <stdio.h> 
    typedef struct node {
        int n;
        struct node *next;
    } node;
    
    
    /* function to create linked list */
    /* function to adding node in linked list*/
    /* function to delete  node in linked list*/
    /* function to print nodes in list 
    int main(void)
    { 
       /* create linked list with 4 nodes */
       /* print list 
       /* add one node to list 
       /* delete third node in list
    
    
        return 0;
    }
    output looks like something

    list
    1
    2
    3
    4


    updated list
    1
    2
    3
    4
    5
    delete third node in list
    update list
    1
    2
    4
    5

    I am trying following way
    Code:
    #include <stdio.h>
     
    typedef struct node {
        int n;
        struct node *next;
    } node;
    
    
    
    
    int main(void)
    { 
       /* create linked list with 4 nodes 
       /* print nodes in list 
       /* add one node in list that is 5
       /* delete  node 3 in linked list
       /* print nodes in list 
    
    
        return 0;
    }
    list
    1
    2
    3
    4


    updated list
    1
    2
    3
    4
    5
    delete third node in list
    update list
    1
    2
    4
    5

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    This is wrong:
    Code:
    Head = malloc(sizeof(Head));
    It should have been:
    Code:
    Head = malloc(sizeof(*Head));
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2019
    Posts
    54
    Quote Originally Posted by laserlight View Post
    This is wrong:
    okay but compiler is not showing warning /error

    I want to write code for algorithm

    Code:
    #include <stdio.h> 
    typedef struct node {
        int n;
        struct node *next;
    } node;
    
    
    /* function to create linked list */
    /* function to adding node in linked list*/
    /* function to delete  node in linked list*/
    /* function to print nodes in list 
    int main(void)
    { 
       /* create linked list with 4 nodes */
       /* print list 
       /* add one node to list 
       /* delete third node in list
    
    
        return 0;
    }
    list
    1
    2
    3
    4


    updated list
    1
    2
    3
    4
    5
    delete third node in list
    update list
    1
    2
    4
    5

    I do not understand how this can be done ?

    I can create list, add node to list but I am having problem to delete the node I do not understand how this can be done

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Have you gone through the process of deleting a node on paper; i.e. drawing a picture of a linked list and writing down the steps you take to solve the problem manually?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    Quote Originally Posted by Player777
    okay but compiler is not showing warning /error
    It's a logic error. In theory, static analysis can uncover that particular kind of logic error because that particular argument to malloc happens to be determined at compile time, but compilers don't usually do enough static analysis to be able to detect that, so what could happen in practice is that you allocate enough space for a pointer, and then you get a segmentation fault or something when you try to write an entire object to the space allocated and end up writing beyond the memory allocated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2019
    Posts
    54
    Quote Originally Posted by Hodor View Post
    Have you gone through the process of deleting a node on paper; i.e. drawing a picture of a linked list and writing down the steps you take to solve the problem manually?
    Exactly that was my plan That's why I'm asking all operation in the main

    I know that the pointer has to be free to delete the node but I am not able to delete the node even on paper

  11. #11
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,742
    Quote Originally Posted by Player777 View Post
    Exactly that was my plan That's why I'm asking all operation in the main

    I know that the pointer has to be free to delete the node but I am not able to delete the node even on paper
    You have to 1) find the node you want to delete (call it target) and at the same time keep track of the node previous to the target (call it prev); 2) change the previous node's next pointer to point to the the target node's next (e.g. prev->next = target->next); 3) at this point the target node is out of the list (nothing is pointing to target anymore) and you can free its memory. You'll have to handle the special case of target being the first (or only) node in the list

    Personally I think that it's actually easier using functions than doing it all in main

    What book are you using? Surely it has pictures of this
    Last edited by Hodor; 01-08-2020 at 08:56 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I want to delete node
    By vajra11 in forum C Programming
    Replies: 10
    Last Post: 11-21-2019, 05:13 AM
  2. delete last node
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 05-02-2007, 08:08 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