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

Discussion in 'C Programming' started by sangram, Dec 1, 2006.

  1. sangram

    sangram Guest

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

    thanks
    sangram
     
    sangram, Dec 1, 2006
    #1
    1. Advertising

  2. sangram

    Guest

    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.
    sangram wrote:
    > how to delete last node of a Linked list if you only know the address
    > of last node.
    >
    > thanks
    > sangram
     
    , Dec 1, 2006
    #2
    1. Advertising

  3. sangram

    sangram Guest

    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.
    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:
    > 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.
    > sangram wrote:
    > > how to delete last node of a Linked list if you only know the address
    > > of last node.
    > >
    > > thanks
    > > sangram
     
    sangram, Dec 1, 2006
    #3
  4. sangram

    MQ Guest

    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
     
    MQ, Dec 1, 2006
    #4
  5. sangram

    Guest

    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, 2006
    #5
  6. sangram

    Simon Biber Guest

    Re: how to delete last node of a Linked list if you only know theaddress of last node.

    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.
     
    Simon Biber, Dec 1, 2006
    #6
  7. sangram

    jaysome Guest

    On 30 Nov 2006 21:53:04 -0800, "sangram" <>
    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
     
    jaysome, Dec 1, 2006
    #7
  8. sangram

    Chris Dollin Guest

    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/
     
    Chris Dollin, Dec 1, 2006
    #8
  9. sangram

    Guest

    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, 2006
    #9
  10. 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.
     
    Richard Heathfield, Dec 1, 2006
    #10
  11. sangram

    Chris Dollin Guest

    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.
     
    Chris Dollin, Dec 1, 2006
    #11
  12. 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.
     
    Richard Heathfield, Dec 1, 2006
    #12
  13. sangram

    Simon Biber Guest

    Re: how to delete last node of a Linked list if you only know theaddress of last node.

    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.
     
    Simon Biber, Dec 1, 2006
    #13
  14. Re: how to delete last node of a Linked list if you only know theaddress of last node.

    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?
     
    james of tucson, Dec 1, 2006
    #14
  15. sangram

    Guest

    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, 2006
    #15
  16. 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.
     
    Richard Heathfield, Dec 1, 2006
    #16
  17. sangram

    jxh Guest

    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
     
    jxh, Dec 1, 2006
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sumika
    Replies:
    4
    Views:
    609
    Emmanuel Delahaye
    Jul 15, 2005
  2. fool
    Replies:
    14
    Views:
    513
    Barry Schwarz
    Jul 3, 2006
  3. Raj
    Replies:
    13
    Views:
    1,618
  4. Replies:
    10
    Views:
    744
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Apr 16, 2007
  5. Replies:
    10
    Views:
    1,037
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Apr 16, 2007
Loading...

Share This Page