473,467 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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
10 8234
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" <ac.c....@gmail.comwrote:
Hello Everyone,

The solution to this is to copy the data from the next node into this
node and delete the next node!.
Not necessarily. You could mark the node for future deletion and then
check if the next node was marked previously and delete that one since
you have the pointer to it.
1. But if the node to be deleted is the last node. Then what should we
do ?
Just mark it.
2. If the list is Head node?
Since you have the pointer to it, you can delete it immediately.
3 If the list is circular then what all conditions we need to check?
You need to check if the head node is the next node and adjust it
accordingly.

Hope this helps....karl m

Apr 14 '07 #3
On Apr 14, 10:38 am, "ac.c....@gmail.com" <ac.c....@gmail.comwrote:
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 ?
If you use an empty "stopper" node at the end of your list, you'll
never get the "last" node for deletion and your solution works fine.
2. If the list is Head node?
Set a new head and delete it.

Hope this helps, more..... karl m

Apr 14 '07 #4
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.c....@gmail.comwrote:
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
People have already pointed out various ways to do this, so I won't
further elaborate on them; what I will do is point out what's wrong
with the above.
In any program of significant size, you're liable to have other
pointers besides those pertaining to the linked list itself, pointing
to the "next node" (the one which your solution says to delete).
You'd have to painstakingly find every last thing which can point to a
node in your list, and handle it accordingly, or else after the "next"
node is deleted, you may have other pointers floating around still
pointing to it.

Example: your "nodes" are soldiers in a battlefield videogame. The
list is periodically run through to make all the soldiers do their
thing. But the battlefield also has machineguns, and each machinegun
actively targets a specific soldier-- which is handled by having the
machinegun structure include a "struct soldier_data *target." A
soldier dies, who was being targeted by a machinegun; the soldier is
deleted from the list in the method you described, and now the
machinegun's "target" pointer is pointing to undefined data. When it
comes time for the machinegun to shoot, your program crashes (in best
case scenario- or does even worse things, possibly).

Apr 15 '07 #8
"ac******@gmail.com" <ac******@gmail.comwrote in
news:11**********************@p77g2000hsh.googlegr oups.com:
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
This is really simple. If you start from the beginning of the list:

if (begin_ll == delete_node) /* single entry */
{
free (begin_ll); /* and any data */
begin_ll = (LNK_LST_NODE *) 0;
return;
}

/* otherwise */

for (prev = begin_ll; prev; prev = prev->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 <rhsmit...@comcast.netwrote:
"ac.c....@gmail.com" wrote the subject line and:
The solution to this is to copy the data
from the next node into this
node and delete the next node!.
>
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" <bill.purs...@gmail.comwrote:
On Apr 16, 3:52 am, R Smith <rhsmit...@comcast.netwrote:
"ac.c....@gmail.com" wrote the subject line and:
The solution to this is to copy the data
from the next node into this
node and delete the next node!.
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.
Then make the list double-linked, unless this it the absolute most
critical execution-path in the program there's no need to make it more
complicated than that.

--
Erik Wikström

Apr 16 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: HS-MOON | last post by:
I'm asking you to help me. I'm a beginner of studying c++. I'm trying to make the Singly Linked List(Ordered). Anyway, I've been debugging all day. I can't sort it out!! As you see, I don't...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
13
by: Raj | last post by:
Is there any way to delete a particular node from a singly linked list where the header of the list is unknown.Only pointer available is the one which points to the node to be deleted
59
by: Anando | last post by:
Hi, I have a linear singly linked list of 100 elements. I would like to prune it such that only user specified elements (say only the elements 1, 13, 78 and 100 of the original list) survive...
3
by: malik | last post by:
// Linked Lists in classes(excluding structures) without using tail pointer # include<iostream.h> # include<stdlib.h> void Swap(int num1, int num2) { int a = num1; num2 = num1; num2 = a;
16
by: sangram | last post by:
how to delete last node of a Linked list if you only know the address of last node. thanks sangram
14
by: vikaskumaragrawal | last post by:
hi friends, i have a question plz help me. my question is ... how can i find middle node of linked list in single traverse???????
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
7
by: davidson1 | last post by:
Hello friends, I want ur help regarding singly linked list in C,I tried in net,But it is confusing and difficult.I want singly linked list in a simple way of Understanding.Please Help Me I want...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.