link list question

P

Puneet

Hi,

I have to know how to delete a node in link list.

i have a single link list like this

1->2->3->4->5->6->7

now i dont know head pointer. I know only the pointer which is pointing
to item 5 in above list. Now i want to delete the item 5 so how i will
do that ??

can anybody help me....

--Puneet
 
J

junky_fellow

Puneet said:
Hi,

I have to know how to delete a node in link list.

i have a single link list like this

1->2->3->4->5->6->7

now i dont know head pointer. I know only the pointer which is pointing
to item 5 in above list. Now i want to delete the item 5 so how i will
do that ??

can anybody help me....

--Puneet

I think this question has nothing to do with C language.
<OT>
Still I would like to give you a hint.
Since, its a single linked list, and you don't have the address of node
4
or head pointer, its not possible to delete the node 5 itself.
But you can play some trick.
Copy the data of node 6 to node 5. ( I assume that all the nodes in
your linked list are of identical types ). Then you may delete node 6.
</OT>
 
T

T.M. Sommers

Puneet said:
Hi,

I have to know how to delete a node in link list.

i have a single link list like this

1->2->3->4->5->6->7

now i dont know head pointer. I know only the pointer which is pointing
to item 5 in above list. Now i want to delete the item 5 so how i will
do that ??

You could copy the contents of 6 into 5, point 5 at 7, and then
delete 6.

-
Thomas M. Sommers -- (e-mail address removed) -- AB2SB
 
M

Martin Ambuhl

Puneet said:
Hi,

I have to know how to delete a node in link list.

i have a single link list like this

1->2->3->4->5->6->7

now i dont know head pointer. I know only the pointer which is pointing
to item 5 in above list. Now i want to delete the item 5 so how i will
do that ??

can anybody help me....

Since you must modify the pointer in node4 which points to node5, it is
impossible to do what you want knowing only the value of the pointer to
node5. You *must* know where one of node1, node2, node3, or node4 is.

For some reason we get lots of questions about linked lists in
comp.lang.c. For some strange reason, they are almost always answered.
Yet they are completely off-topic. There is *nothing* about the C
programming language involved here.

Despite the fact that your lecturer or your textbook has doubtless made
it clear how to do this and you were not paying attention, you could --
if you thought about it -- have come up with tne process.

You have
ptr to node4 -> node4
ptr to node5 -> node5
ptr to node6 -> node6

You want to change this to
ptr to node4 -> node4
ptr to node6 -> node6


Obviously you want to replace the value of the pointer in node4 with a
pointer to node6. Where do you get that value? Where do you put it?
What do you do with the memory allocated to node5? Isn't this really a
very baby problem for you not to have worked out for yourself?
 
P

Prasanna

In any linked list problem its always advisable to keep track of the
linked list head.In order to delete a node you need to traverse the
list until you point to the node before the node you wish to delete.In
the above case you need to traverse until
head->next->data == 5.
Then you make
head->next = head->next->next; /* attaching 4 to 6*/

At all times you need to note that you are not changing the
position of the actual head.This can be
accomplished by making a function call to delete the required node or
use a temporary head pointer.
 
P

Prasanna

Ingenious, I am new to programming, I always thought that there was no
use for a linked list and no standard list operations that can be
performed without the access to its head.
Although you achieve the purpose of deleting a node.You will have a
list of 6 elements with access only to two elements.Now thats a useless
list don't you think?
 
J

junky_fellow

Prasanna said:
Ingenious, I am new to programming, I always thought that there was no
use for a linked list and no standard list operations that can be
performed without the access to its head.
Although you achieve the purpose of deleting a node.You will have a
list of 6 elements with access only to two elements.Now thats a useless
list don't you think?

I just told a way to delete a node when you dont have header pointer.
Think of a case where the function delete() is written in a way
that you pass the pointer of the node to delete.
 
R

Richard Bos

T.M. Sommers said:
You could copy the contents of 6 into 5, point 5 at 7, and then
delete 6.

That is an often-cited trick, but it isn't a general solution. Consider
only the following situations:

- your pointer is the last in the list;
- there are other pointers to this node;
- even more insidious: there are other pointers to the following node.

It is a homework problem, which should never occur outside the
classroom; and your solution is a homework solution, which should never
be accepted outside the classroom. Or even in it, frankly, but then,
void main() shouldn't be taught, either.

Richard
 
L

Lawrence Kirby

I just told a way to delete a node when you dont have header pointer.
Think of a case where the function delete() is written in a way
that you pass the pointer of the node to delete.

Then you have a faulty/broken/crippled delete() function. The answer to
that is to fix the interface to it. You should be able to access the
pointer that points to the node you want to delete so at least the
delete() function needs a pointer to that pointer.

Or just use a doubly linked list.

Lawrence
 
C

CBFalconer

I think this question has nothing to do with C language.
<OT>
Still I would like to give you a hint.
Since, its a single linked list, and you don't have the address
of node 4 or head pointer, its not possible to delete the node 5
itself. But you can play some trick. Copy the data of node 6 to
node 5. ( I assume that all the nodes in your linked list are of
identical types ). Then you may delete node 6.
</OT>

Good, but doesn't work when item 5 is the last in the list!
TANSTAAFL
 
J

Jonathan Bartlett

Prasanna said:
Ingenious, I am new to programming, I always thought that there was no
use for a linked list

You should try to tell that to Scheme and Lisp programmers :)
> I always thought that there was no
> use for a linked list and no standard list operations that can be
> performed without the access to its head.

Sometimes (as in Scheme and Lisp mentioned above), lists are used quite
usefully without ever doing delete operations, and without keeping
access to the head.

If you're interested in list processing, you should check out my
DeveloperWorks article on it:

http://www-128.ibm.com/developerworks/linux/library/l-listproc/

Jon
 
T

T.M. Sommers

Richard said:
That is an often-cited trick, but it isn't a general solution. Consider
only the following situations:

- your pointer is the last in the list;
- there are other pointers to this node;
- even more insidious: there are other pointers to the following node.

That's what I get for typing off the top of my head. You are
quite correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,249
Latest member
KattieCort

Latest Threads

Top