deleting a node without head pointer - Programmers Heaven

Howdy, Stranger!

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

Categories

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.