Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

deleting a node without head pointer

dssuresh6dssuresh6 Posts: 17Member
When the head pointer of a linked list is given, it is easy to delete any node in that linked list given a pointer to that node, by just browsing from head till that node. If no head pointer is given,then how to delete a node given just a pointer to that node? I've heard it is possible but don't know how.

Note that the linked list is a singly-linked one.

Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    : When the head pointer of a linked list is given, it is easy to delete any node in that linked list given a pointer to that node, by just browsing from head till that node. If no head pointer is given,then how to delete a node given just a pointer to that node? I've heard it is possible but don't know how.
    :
    : Note that the linked list is a singly-linked one.
    :

    it is possible for double-linked lists, where there is both a next and previous pointer. If there's only a "next" pointer, then it can't be done.
  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : : When the head pointer of a linked list is given, it is easy to delete any node in that linked list given a pointer to that node, by just browsing from head till that node. If no head pointer is given,then how to delete a node given just a pointer to that node? I've heard it is possible but don't know how.
    : :
    : : Note that the linked list is a singly-linked one.
    : :
    :
    : it is possible for double-linked lists, where there is both a next and previous pointer. If there's only a "next" pointer, then it can't be done.
    :

    [blue]"Deleting" a node can mean different things. Physically deleting the node by calling free/delete on a pointer is one interpretation. Logically deleting a node, by somehow setting a flag within the node or otherwise marking it as unused, is another possible interpretation.

    If we are talking the second interpretation here, then your nodes should have some sort of flag variable that can indicate whether or not the node is "active". Your node processing routines would likely need to handle active and inactive nodes differently, i.e. displaying contents of a list may involve skipping any inactive nodes, but an insertion routine might be set up to find the first NULL (end-of-list) to create a new node or the first "inactive" node to copy data into (and marking it as now active in the process). Ultimately however, you will need a "head" pointer in order to cleanly deal with the list's allocated memory or you risk a memory leak.[/blue]
Sign In or Register to comment.