By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,864 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,864 IT Pros & Developers. It's quick & easy.

link list question

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a


Puneet wrote:
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>

Nov 14 '05 #2

P: n/a
Puneet wrote:
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 -- tm*@nj.net -- AB2SB

Nov 14 '05 #3

P: n/a
Puneet wrote:
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?
Nov 14 '05 #4

P: n/a
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.

Nov 14 '05 #5

P: n/a

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?

Nov 14 '05 #6

P: n/a


Prasanna wrote:
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.

Nov 14 '05 #7

P: n/a
"T.M. Sommers" <tm*@nj.net> wrote:
Puneet wrote:
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.


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
Nov 14 '05 #8

P: n/a
On Wed, 15 Jun 2005 23:33:26 -0700, junky_fellow wrote:


Prasanna wrote:
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.


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

Nov 14 '05 #9

P: n/a
ju**********@yahoo.co.in wrote:
Puneet wrote:

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 ??


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #10

P: n/a
Prasanna wrote:
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/developerwork...ry/l-listproc/

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 14 '05 #11

P: n/a
Richard Bos wrote:
"T.M. Sommers" <tm*@nj.net> wrote:

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.


That's what I get for typing off the top of my head. You are
quite correct.
--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.