473,287 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 8223
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.