Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ac.c.2k7@gmail.com

    Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

    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 ?
    2. If the list is Head node?
    3 If the list is circular then what all conditions we need to check?

    Thanks,
    kaka

  • Richard Heathfield

    #2
    Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

    ac.c.2k7@gmail. com said:
    Hello Everyone,
    >
    The solution to this is to copy the data from the next node into this
    node and delete the next node!.
    No, the solution is to re-design the interface to the deletion routine
    so that you get sufficient information supplied to you to enable you to
    do the job properly.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999

    email: rjh at the above domain, - www.

    Comment

    • Karl Malbrain

      #3
      Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

      On Apr 14, 10:38 am, "ac.c....@gmail .com" <ac.c....@gmail .comwrote:
      Hello Everyone,
      >
      The solution to this is to copy the data from the next node into this
      node and delete the next node!.
      Not necessarily. You could mark the node for future deletion and then
      check if the next node was marked previously and delete that one since
      you have the pointer to it.
      1. But if the node to be deleted is the last node. Then what should we
      do ?
      Just mark it.
      2. If the list is Head node?
      Since you have the pointer to it, you can delete it immediately.
      3 If the list is circular then what all conditions we need to check?
      You need to check if the head node is the next node and adjust it
      accordingly.

      Hope this helps....karl m

      Comment

      • Karl Malbrain

        #4
        Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

        On Apr 14, 10:38 am, "ac.c....@gmail .com" <ac.c....@gmail .comwrote:
        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 ?
        If you use an empty "stopper" node at the end of your list, you'll
        never get the "last" node for deletion and your solution works fine.
        2. If the list is Head node?
        Set a new head and delete it.

        Hope this helps, more..... karl m

        Comment

        • =?ISO-8859-1?Q?Erik_Wikstr=F6m?=

          #5
          Re: Given only a pointer to a node to be deleted in a singly linkedlist, how do u delete it?

          On 2007-04-14 19:38, ac.c.2k7@gmail. com wrote:
          Hello Everyone,
          >
          The solution to this is to copy the data from the next node into this
          node and delete the next node!.
          Scan the list from head until you find the node to delete and delete it
          just like you'd delete any node in a single-linked list.

          --
          Erik Wikström

          Comment

          • monty

            #6
            Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

            HI friends
            Start from the header node and traverse the linked list
            storing two pointers
            the current node and previous node .If current node
            becomes deletion node
            delete it and attach the previous node to the next of
            current node.

            1 if header node is deletion node then simply delete it and point the
            next node to be the header node.
            2If last node is deletion node above process will work
            3. In case of circular list move until current node becomes deletion
            node.Remember start from previous note assignment thru
            header.

            Comment

            • Richard Heathfield

              #7
              Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

              monty said:
              HI friends
              Start from the header node and
              In other words, change the spec. Absolutely right.

              --
              Richard Heathfield
              "Usenet is a strange place" - dmr 29/7/1999

              email: rjh at the above domain, - www.

              Comment

              • Snis Pilbor

                #8
                Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

                On Apr 14, 1:38 pm, "ac.c....@gmail .com" <ac.c....@gmail .comwrote:
                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 ?
                2. If the list is Head node?
                3 If the list is circular then what all conditions we need to check?
                >
                Thanks,
                kaka
                People have already pointed out various ways to do this, so I won't
                further elaborate on them; what I will do is point out what's wrong
                with the above.
                In any program of significant size, you're liable to have other
                pointers besides those pertaining to the linked list itself, pointing
                to the "next node" (the one which your solution says to delete).
                You'd have to painstakingly find every last thing which can point to a
                node in your list, and handle it accordingly, or else after the "next"
                node is deleted, you may have other pointers floating around still
                pointing to it.

                Example: your "nodes" are soldiers in a battlefield videogame. The
                list is periodically run through to make all the soldiers do their
                thing. But the battlefield also has machineguns, and each machinegun
                actively targets a specific soldier-- which is handled by having the
                machinegun structure include a "struct soldier_data *target." A
                soldier dies, who was being targeted by a machinegun; the soldier is
                deleted from the list in the method you described, and now the
                machinegun's "target" pointer is pointing to undefined data. When it
                comes time for the machinegun to shoot, your program crashes (in best
                case scenario- or does even worse things, possibly).

                Comment

                • R Smith

                  #9
                  Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

                  "ac.c.2k7@gmail .com" <ac.c.2k7@gmail .comwrote in
                  news:1176572330 .635635.115560@ p77g2000hsh.goo glegroups.com:
                  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 ?
                  2. If the list is Head node?
                  3 If the list is circular then what all conditions we need to check?
                  >
                  Thanks,
                  kaka
                  >
                  This is really simple. If you start from the beginning of the list:

                  if (begin_ll == delete_node) /* single entry */
                  {
                  free (begin_ll); /* and any data */
                  begin_ll = (LNK_LST_NODE *) 0;
                  return;
                  }

                  /* otherwise */

                  for (prev = begin_ll; prev; prev = prev->next)
                  {
                  node = prev->next;
                  if (node->next == delete_node)
                  {
                  prev->next = node->next->next; /* whether it's NULL or not
                  doesn't matter */
                  free (delete_node); /* free any malloc'd data first*/
                  break;
                  }
                  }

                  It will either become the end of the list or will simply point to the node
                  after the delete_node.

                  It helps to visualize this (though I've been out with the gang tonight and
                  the vision is a little sketchy, but I believe it works). Draw the nodes on
                  a piece of paper and think it through.

                  Comment

                  • Bill Pursell

                    #10
                    Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

                    On Apr 16, 3:52 am, R Smith <rhsmit...@comc ast.netwrote:
                    "ac.c....@gmail .com" wrote the subject line and:
                    The solution to this is to copy the data
                    from the next node into this
                    node and delete the next node!.
                    >
                    This is really simple. If you start from
                    the beginning of the list:
                    In the original statement of the question, you don't have
                    access to the beginning of the list.

                    Comment

                    • =?iso-8859-1?q?Erik_Wikstr=F6m?=

                      #11
                      Re: Given only a pointer to a node to be deleted in a singly linked list, how do u delete it?

                      On 16 Apr, 08:24, "Bill Pursell" <bill.purs...@g mail.comwrote:
                      On Apr 16, 3:52 am, R Smith <rhsmit...@comc ast.netwrote:
                      >
                      "ac.c....@gmail .com" wrote the subject line and:
                      The solution to this is to copy the data
                      from the next node into this
                      node and delete the next node!.
                      >
                      This is really simple. If you start from
                      the beginning of the list:
                      >
                      In the original statement of the question, you don't have
                      access to the beginning of the list.
                      Then make the list double-linked, unless this it the absolute most
                      critical execution-path in the program there's no need to make it more
                      complicated than that.

                      --
                      Erik Wikström

                      Comment

                      Working...