Query related to link list

Discussion in 'C Programming' started by deba, May 17, 2007.

  1. deba

    deba Guest

    Hi
    i want to know is it possible to delete the node where my
    pointer currently pointing without knowing the head node address.Dis
    question ask to me in an interview.

    I really want to know the soloution of it.Thanx
    deba, May 17, 2007
    #1
    1. Advertising

  2. deba

    Tejas Kokje Guest

    deba wrote:

    > Hi
    > i want to know is it possible to delete the node where my
    > pointer currently pointing without knowing the head node address.Dis
    > question ask to me in an interview.
    >
    > I really want to know the soloution of it.Thanx


    Here is the pseudocode

    int node_delete(node_t **node) {

    if (!node || !*node)
    return -1;

    //if I am last node, then free and set myself to NULL.
    if (*node->next == NULL) {
    free(*node);
    *node = NULL;
    }
    else {
    //if there is a node after me, copy its contents to me and free that next
    //node

    *node->data = *node->next->data;
    *node->next = *node->next->next;
    free(*node->next);
    }

    return 0;
    }

    Hope this helps.

    Tejas Kokje
    Tejas Kokje, May 17, 2007
    #2
    1. Advertising

  3. On 16 May 2007 22:18:15 -0700, deba <>
    wrote:

    >Hi
    > i want to know is it possible to delete the node where my
    >pointer currently pointing without knowing the head node address.Dis
    >question ask to me in an interview.
    >
    > I really want to know the soloution of it.Thanx


    If the node you currently point to is not last, then you can copy the
    next node into your node (and delete that one). This has the logical
    effect of deleting the node you are pointing to but not the physical
    effect (you freed a different block of memory).

    If you are pointing to the last node of the list, you must somehow
    have access to the next-to-last node so you can reset its "next"
    pointer.


    Remove del for email
    Barry Schwarz, May 17, 2007
    #3
  4. deba

    Joe Wright Guest

    deba wrote:
    > Hi
    > i want to know is it possible to delete the node where my
    > pointer currently pointing without knowing the head node address.Dis
    > question ask to me in an interview.
    >
    > I really want to know the soloution of it.Thanx
    >

    You need to know 'here', (the current node), 'prev' (the previous node)
    and 'next' (here->next).

    To remove 'here' from the list logically,

    prev->next = here->next;

    then or course to remove it physically..

    free(here), here = prev;

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, May 18, 2007
    #4
  5. Groovy hepcat Barry Schwarz <> was jivin' on Thu, 17 May
    2007 05:19:05 -0700. It's a cool scene! Dig it.

    > On 16 May 2007 22:18:15 -0700, deba <> wrote:
    >
    >>Hi
    >> i want to know is it possible to delete the node where my
    >>pointer currently pointing without knowing the head node address.Dis
    >>question ask to me in an interview.
    >>
    >> I really want to know the soloution of it.Thanx

    >
    > If the node you currently point to is not last, then you can copy the next
    > node into your node (and delete that one). This has the logical effect of
    > deleting the node you are pointing to but not the physical effect (you
    > freed a different block of memory).
    >
    > If you are pointing to the last node of the list, you must somehow have
    > access to the next-to-last node so you can reset its "next" pointer.


    Of course, it's different (simpler) for double linked lists. Since the
    current node has pointers not only to the next node but also the previous
    one, you simply set the next node's "previous" pointer to point at the
    previous node (or set it to NULL, adjusting the "head" pointer to point at
    the next node, if there is no previous node), set the previous node's
    "next" pointer to point at the next node (or set it to NULL, adjusting the
    "tail" pointer to point at the previous node, if there is no next node),
    then remove the current node by setting its "next" and "previous" pointers
    to NULL.

    --
    Dig the sig!



    ----------- Peter 'Shaggy' Haywood ------------

    Ain't I'm a dawg!!
    Peter 'Shaggy' Haywood, May 20, 2007
    #5
    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. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    794
    Kevin Spencer
    May 17, 2005
  2. Datagrid related query

    , Mar 20, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    413
    Kalpesh
    Mar 20, 2006
  3. Replies:
    5
    Views:
    385
    David Dorward
    May 14, 2006
  4. Replies:
    0
    Views:
    938
  5. Maxwell Hammer
    Replies:
    7
    Views:
    631
    Peter Hansen
    Jun 18, 2005
Loading...

Share This Page