Thread: quick question on deleting a node from a linked list

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    14

    quick question on deleting a node from a linked list

    I just started learning linked list and nodes and I'm stil getting use to the syntax.

    http://i1330.photobucket.com/albums/...ps61afd59b.jpg

    I made this to help me visualize my list.

    do I set the code to skip to get to the next node

    like this

    Code:
    
    p=p->next=p->next


    And I'm guessing that the node I have to delete has to be deallocated because it still exist somewhere in memory.

    So does that mean I will have to traverse through the list first and make p point to the one I want to delete and make another pointer let's say node * d and then have d point to where p is pointing.

    Then traverse again and when p gets to the node before the one I want to delete and have p->next point to the node after the one I want to delete and then delete d.

    Code:
    delete d;
    Last edited by c++noob145; 06-20-2014 at 09:01 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c++noob145
    And I'm guessing that the node I have to delete has to be deallocated because it still exist somewhere in memory.
    Yes, though I would rather say "destroyed" rather than "deallocated" since "deallocated" generally refers to memory, whereas there may (or may not) be other things that should (also) happen when you remove the node.

    Quote Originally Posted by c++noob145
    So does that mean I will have to traverse through the list first and make p point to the one I want to delete and make another pointer let's say node * d and then have d point to where p is pointing.

    Then traverse again and when p gets to the node before the one I want to delete and have p->next point to the node after the one I want to delete and then delete d.
    No, you only need to traverse once: find the node before the one that you want to delete. From there, you only need to make its next pointer point to the node after the one you want to delete, then you destroy the node that you want to delete.

    Note that this contains redundancy:
    Code:
    p=p->next=p->next
    It could be simplified to:
    Code:
    p = p->next;
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you've found the node to destroy, and the node before it, why would you need to traverse it twice? No human would need so, so why would a computer?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting a node from a linked list.
    By Mr.Lnx in forum C Programming
    Replies: 15
    Last Post: 10-22-2013, 01:55 AM
  2. Deleting a node from a linked list
    By thekid in forum C Programming
    Replies: 7
    Last Post: 04-20-2013, 12:18 AM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  4. deleting a node in a linked list
    By barneygumble742 in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2005, 09:36 AM
  5. Deleting A Node From A Linked List
    By Anonymous Freak in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2002, 08:59 PM