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

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

Similar topics

2
by: PRadyut | last post by:
in this code the delete function does not delete the last node of the double linked list the code ---------------------------------------------------------------- #include <stdio.h>...
13
by: Raj | last post by:
Is there any way to delete a particular node from a singly linked list where the header of the list is unknown.Only pointer available is the one which points to the node to be deleted
1
by: ahoway | last post by:
I am having problems deleting a node from a link list. I need to delete the node which contains the number six. This is what I have so far..... Thank you in advance. #include <iostream>...
5
by: Aditya | last post by:
This is a normal interview question, which goes like this "Given a linked list of integers, delete every mth node and return the last remaining node." eg: Linked list =...
9
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
10
by: ac.c.2k7 | last post by:
Hello Everyone, The solution to this is to copy the data from the next node into this node and delete the next node!. 1. But if the node to be deleted is the last node. Then what should we do ?...
4
by: moon24 | last post by:
Hi im working with linked list and i have to implement a function that deletes the duplicates of a number. for example if given 2 7 1 7 12 7 then the result should be 2 7 1 12 here is what I have:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.