Deleting a middle node from a single linked list when pointer to the previous node is not available

前端 未结 24 1167
陌清茗
陌清茗 2020-11-29 20:42

Is it possible to delete a middle node in the single linked list when the only information available we have is the pointer to the node to be deleted and not the pointer to

相关标签:
24条回答
  • 2020-11-29 21:04

    The initial suggestion was to transform:

    a -> b -> c

    to:

    a ->, c

    If you keep the information around, say, a map from address of node to address of the next node then you can fix the chain the next time to traverse the list. If need to delete multiple items before the next traversal then you need to keep track of the order of deletes (i.e. a change list).

    The standard solution is consider other data structures like a skip list.

    0 讨论(0)
  • 2020-11-29 21:05

    Yes, but your list will be broken after you remove it.

    In this specific case, traverse the list again and get that pointer! In general, if you are asking this question, there probably exists a bug in what you are doing.

    0 讨论(0)
  • 2020-11-29 21:09
    void delself(list *list)
    {
       /*if we got a pointer to itself how to remove it...*/
       int n;
    
       printf("Enter the num:");
    
       scanf("%d",&n);
    
       while(list->next!=NULL)
       {
           if(list->number==n) /*now pointer in node itself*/
           {
               list->number=list->next->number;
               /*copy all(name,rollnum,mark..) data of next to current, disconect its next*/
               list->next=list->next->next;
           }
           list=list->next;
       }
    }
    
    0 讨论(0)
  • 2020-11-29 21:10

    The best approach is still to copy the data of the next node into the node to be deleted, set the next pointer of the node to the next node's next pointer, and delete the next node.

    The issues of external pointers pointing to the node to be deleted, while true, would also hold for the next node. Consider the following linked lists:

    A->B->C->D->E->F and G->H->I->D->E->F

    In case you have to delete node C (in the first linked list), by the approach mentioned, you will delete node D after copying the contents to node C. This will result in the following lists:

    A->B->D->E->F and G->H->I->dangling pointer.

    In case you delete the NODE C completely, the resulting lists will be:

    A->B->D->E->F and G->H->I->D->E->F.

    However, if you are to delete the node D, and you use the earlier approach, the issue of external pointers is still there.

    0 讨论(0)
  • 2020-11-29 21:10

    You could do delayed delinking where you set nodes to be delinked out of the list with a flag and then delete them on the next proper traversal. Nodes set to be delinked would need to be properly handled by the code that crawls the list.

    I suppose you could also just traverse the list again from the beginning until you find the thing that points to your item in the list. Hardly optimal, but at least a much better idea than delayed delinking.

    In general, you should know the pointer to the item you just came from and you should be passing that around.

    (Edit: Ick, with the time it took me to type out a fullish answer three gazillion people covered almost all the points I was going to mention. :()

    0 讨论(0)
  • 2020-11-29 21:10

    The only sensible way to do this is to traverse the list with a couple of pointers until the leading one finds the node to be deleted, then update the next field using the trailing pointer.

    If you want to delete random items from a list efficiently, it needs to be doubly linked. If you want take items from the head of the list and add them at the tail, however, you don't need to doubly link the whole list. Singly link the list but make the next field of the last item on the list point to the first item on the list. Then make the list "head" point to the tail item (not the head). It is then easy to add to the tail of the list or remove from the head.

    0 讨论(0)
提交回复
热议问题