467,198 Members | 1,207 Online

# Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

 Hello Everyone, The solution to this is to copy the data from the next node into this node and delete the next node!. 1. But if the node to be deleted is the last node. Then what should we do ? 2. If the list is Head node? 3 If the list is circular then what all conditions we need to check? Thanks, kaka Apr 14 '07 #1
• viewed: 7862
Share:
10 Replies
 ac******@gmail.com said: Hello Everyone, The solution to this is to copy the data from the next node into this node and delete the next node!. No, the solution is to re-design the interface to the deletion routine so that you get sufficient information supplied to you to enable you to do the job properly. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Apr 14 '07 #2
 On Apr 14, 10:38 am, "ac.c....@gmail.com"
 On Apr 14, 10:38 am, "ac.c....@gmail.com"
 On 2007-04-14 19:38, ac******@gmail.com wrote: Hello Everyone, The solution to this is to copy the data from the next node into this node and delete the next node!. Scan the list from head until you find the node to delete and delete it just like you'd delete any node in a single-linked list. -- Erik Wikström Apr 14 '07 #5
 HI friends Start from the header node and traverse the linked list storing two pointers the current node and previous node .If current node becomes deletion node delete it and attach the previous node to the next of current node. 1 if header node is deletion node then simply delete it and point the next node to be the header node. 2If last node is deletion node above process will work 3. In case of circular list move until current node becomes deletion node.Remember start from previous note assignment thru header. Apr 15 '07 #6
 monty said: HI friends Start from the header node and In other words, change the spec. Absolutely right. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Apr 15 '07 #7
 On Apr 14, 1:38 pm, "ac.c....@gmail.com"
 "ac******@gmail.com" next) { node = prev->next; if (node->next == delete_node) { prev->next = node->next->next; /* whether it's NULL or not doesn't matter */ free (delete_node); /* free any malloc'd data first*/ break; } } It will either become the end of the list or will simply point to the node after the delete_node. It helps to visualize this (though I've been out with the gang tonight and the vision is a little sketchy, but I believe it works). Draw the nodes on a piece of paper and think it through. Apr 16 '07 #9
 On Apr 16, 3:52 am, R Smith This is really simple. If you start from the beginning of the list: In the original statement of the question, you don't have access to the beginning of the list. Apr 16 '07 #10
 On 16 Apr, 08:24, "Bill Pursell"

### This discussion thread is closed

Replies have been disabled for this discussion.