469,927 Members | 1,737 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

how to delete last node of a Linked list if you only know the address of last node.

how to delete last node of a Linked list if you only know the address
of last node.

thanks
sangram

Dec 1 '06 #1
16 10946
hi
start traversing the node
see if the nodes it points to has the same address as the one you have
if it isnt then goto the next node and continue this process
if u find the address:
1set the prev node's( where u are right now) next parameter to null
before go to the next node and make a copy..
after that do step no 1 and free the copied node.
sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.

thanks
sangram
Dec 1 '06 #2
hi friend,

its ok if u know the start of the node.

but only you know the end node and now u delete it.

thanks for comunication.
ra*******************@gmail.com wrote:
hi
start traversing the node
see if the nodes it points to has the same address as the one you have
if it isnt then goto the next node and continue this process
if u find the address:
1set the prev node's( where u are right now) next parameter to null
before go to the next node and make a copy..
after that do step no 1 and free the copied node.
sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.

thanks
sangram
Dec 1 '06 #3
MQ

sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.
You can't, because you would need to know the address of the previous
node in order to be able to set its "next" pointer to null. If you
want to be able to remove any arbitrary node without needing to know
the address of any other node, then you will need to implement a
doubly-linked list. That is, each node will have a "next" and
"previous" pointer.

MQ

Dec 1 '06 #4
Try this out.

1. Make sure the address you are having is of Last Node.
2. Update it to Null
3. Free the memory
or step 3 and then step 2 depend on ur algo.

--raxit

sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.

thanks
sangram
Dec 1 '06 #5
sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.
The answer to your question is to make it a doubly-linked list, then use
code like this:

if(last->prev)
{
last->prev->next = NULL;
last->prev = NULL;
}

If it's a singly-linked list then you're out of luck without access to
either the address of the first node or the address of the node before
the last.

--
Simon.
Dec 1 '06 #6
On 30 Nov 2006 21:53:04 -0800, "sangram" <ke*************@gmail.com>
wrote:
>how to delete last node of a Linked list if you only know the address
of last node.
In the real world:

Tell the interviewer that if he or she ever requires you to do such a
thing, then you don't want to ever work for him or her.

On your way running out the door, ask them what is the return type of
main(). If their response seems suspect--which is highly
probable--keep running, and consider yourself fortunate.

--
jay
Dec 1 '06 #7
sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.
This isn't a C question. comp.programming would be better.

(The answer, from the information given, is "you can't", but if you'd
like to show us the actual C code with the problem, maybe that will
speak more exactly about it.)

--
Chris "subtle, like a barrel" Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Dec 1 '06 #8

sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.
If we are talking about a singly-linked list, I don't see how you
expect to do anything with it if you only know the address of the last
node... It's not really a linked list, in my mind, if you haven't got a
start point from which to start traversing...

Is this another homework or interview question? Can we have more
context please?

Dec 1 '06 #9
sangram said:
how to delete last node of a Linked list if you only know the address
of last node.
exit(0); will do it. Don't forget <stdlib.h>, though.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 1 '06 #10
Richard Heathfield wrote:
sangram said:
>how to delete last node of a Linked list if you only know the address
of last node.

exit(0); will do it. Don't forget <stdlib.h>, though.
Does the Standard guarantee that exiting the program will reclaim
the memory used? If not, I don't see that you can claim that the
last node is deleted.

--
Chris "subtle, like a barrel" Dollin
The shortcuts are all full of people using them.

Dec 1 '06 #11
Chris Dollin said:
Richard Heathfield wrote:
>sangram said:
>>how to delete last node of a Linked list if you only know the address
of last node.

exit(0); will do it. Don't forget <stdlib.h>, though.

Does the Standard guarantee that exiting the program will reclaim
the memory used?
The Standard guarantees that, after the program exits, it won't be able to
tell that the last node has /not/ been deleted. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 1 '06 #12
Richard Heathfield wrote:
Chris Dollin said:
>Richard Heathfield wrote:
>>sangram said:

how to delete last node of a Linked list if you only know the address
of last node.
exit(0); will do it. Don't forget <stdlib.h>, though.
Does the Standard guarantee that exiting the program will reclaim
the memory used?

The Standard guarantees that, after the program exits, it won't be able to
tell that the last node has /not/ been deleted. :-)
If the list is stored in a file and the addresses are file positions:

struct node
{
char data[80];
long next;
};

/* returns 0 on error, 1 on success */
int get_node(FILE *fp, long pos, struct node *buf)
{
if(fseek(fp, pos, SEEK_SET) != 0) return 0;
if(fread(buf, sizeof *buf, 1, fp) != 1) return 0;
return 1;
}

Then after the program exits the file can be expected to still exist.
Subsequent executions of the program may reopen it and find that the
last node has not been deleted.

--
Simon.
Dec 1 '06 #13
sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.
I certainly hope you know more than that, or you already have lost the
list. Unless of course it is a doubly-linked list, in which case you
can think of the "last" node as a "head". But you do know more than
just the address of the last node, right? You also have a pointer to
the head of the list?
Dec 1 '06 #14

sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.
Let 0x1000 be the address of the last node,
Then

void* p = 0x1000;

free(p);

I'm just a beginner in C, I just thought, Will it work...

Dec 1 '06 #15
th******@yahoo.co.in said:
>
sangram wrote:
>how to delete last node of a Linked list if you only know the address
of last node.

Let 0x1000 be the address of the last node,
Then

void* p = 0x1000;

free(p);

I'm just a beginner in C, I just thought, Will it work...
No, it won't even compile. And in any case, even if it did work, it wouldn't
fix up the list properly by clearing the previous node's "next" link. The
correct answer is as follows:

To delete the last node of a linked list with two or more nodes, first make
sure you have access to the node before it.

In other words, the question is broken.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 1 '06 #16
jxh

sangram wrote:
how to delete last node of a Linked list if you only know the address
of last node.

thanks
sangram
If it is a singly linked list, and there is sentinel node that is
dynamically allocated:

assert(lastnode->next == sentinel);
*lastnode = *sentinel;
free(sentinel);
sentinel = lastnode;

Of course, there are all sorts of caveats for using singly linked lists
in this way. You are almost always better off using a doubly linked
list instead.

-- James

Dec 1 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by PRadyut | last post: by
1 post views Thread by ahoway | last post: by
5 posts views Thread by Aditya | last post: by
9 posts views Thread by rohits123 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.