473,320 Members | 1,949 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,320 software developers and data experts.

link list question

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
11 1457


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

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


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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Shawn Windle | last post by:
----begin node.h-------- #ifndef NODE_H #define NODE_H #include <iostream> //NULL using namespace std; class node {
8
by: Chinmoy Mukherjee | last post by:
when the node structure is like struct node { node *next; node *rand; (// random is supposed to point to another node int value; }
5
by: Rolan | last post by:
I have tried many iterations of constructing an expanded string link criteria for a list box selection, but have not been successful. The List Box uses a Totals Select Query that has a CustID,...
8
by: sudhirlko2001 | last post by:
How to swap two nodes of doubly Linklist
14
by: Steve McLellan | last post by:
Hi, Sorry to repost, but this is becoming aggravating, and causing me a lot of wasted time. I've got a reasonably large mixed C++ project, and after a number of builds (but not a constant...
10
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
4
by: ben.dixon | last post by:
Hi, I'm using links for navigation on a test page, so each link will go to a question in the test e.g. Question 1, Question 2 etc. For each link i am using an Outer Border, Middle border and...
3
by: maruf.syfullah | last post by:
Consider the following Class definitions: class AClass { int ai1; int ai2; public: CClass* c; AClass(){}
5
by: johnnash | last post by:
i'm declaring a data structure for link list of integers in A.h #ifndef A_H (can anyone please explain how ifndef works as well..i just seem to see it in almost every program) #define A_H ...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.