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

S

sangram

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

thanks
sangram
 
R

ramasubramanian.rahul

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:
1> set 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.
 
S

sangram

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

MQ

sangram said:
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
 
R

raxitsheth2000

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
 
S

Simon Biber

sangram said:
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.
 
J

jaysome

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

Chris Dollin

sangram said:
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.)
 
M

mark_bluemel

sangram said:
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?
 
R

Richard Heathfield

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

Chris Dollin

Richard said:
sangram said:


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

Richard Heathfield

Chris Dollin said:
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. :)
 
S

Simon Biber

Richard said:
Chris Dollin said:


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

james of tucson

sangram said:
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?
 
T

the_init

sangram said:
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...
 
R

Richard Heathfield

(e-mail address removed) said:
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.
 
J

jxh

sangram said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top