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

前端 未结 24 1165
陌清茗
陌清茗 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 20:51

    Given

    A -> B -> C -> D

    and a pointer to, say, item B, you would delete it by
    1. free any memory belonging to members of B
    2. copy the contents of C into B (this includes its "next" pointer)
    3. delete the entire item C

    Of course, you'll have to be careful about edge cases, such as working on lists of one item.

    Now where there was B, you have C and the storage that used to be C is freed.

    0 讨论(0)
  • 2020-11-29 20:52

    Maybe do a soft delete? (i.e., set a "deleted" flag on the node) You can clean up the list later if you need to.

    0 讨论(0)
  • 2020-11-29 20:55

    yes, but you can't delink it. If you don't care about corrupting memory, go ahead ;-)

    0 讨论(0)
  • 2020-11-29 20:55
    Void deleteMiffffdle(Node* head)
    {
         Node* slow_ptr = head;
         Node* fast_ptr = head;
         Node* tmp = head;
         while(slow_ptr->next != NULL && fast_ptr->next != NULL)
         {
            tmp = slow_ptr;
            slow_ptr = slow_ptr->next;
            fast_ptr = fast_ptr->next->next;
         }
         tmp->next = slow_ptr->next;
         free(slow_ptr);
        enter code here
    
    }
    
    0 讨论(0)
  • 2020-11-29 20:57

    In order to get to the previous list item, you would need to traverse the list from the beginning until you find an entry with a next pointer that points to your current item. Then you'd have a pointer to each of the items that you'd have to modify to remove the current item from the list - simply set previous->next to current->next then delete current.

    edit: Kimbo beat me to it by less than a minute!

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

    The following code will create a LL, n then ask the user to give the pointer to the node to be deleted. it will the print the list after deletion It does the same thing as is done by copying the node after the node to be deleted, over the node to be deleted and then delete the next node of the node to be deleted. i.e

    a-b-c-d

    copy c to b and free c so that result is

    a-c-d

    struct node  
    {
        int data;
        struct node *link;
     };
    
    void populate(struct node **,int);
    
    void delete(struct node **);
    
    void printlist(struct node **);
    
    void populate(struct node **n,int num)
    {
    
        struct node *temp,*t;
        if(*n==NULL)
        {
            t=*n;
            t=malloc(sizeof(struct node));
            t->data=num;
            t->link=NULL;
            *n=t;
        }
        else
        {
            t=*n;
            temp=malloc(sizeof(struct node));
            while(t->link!=NULL)
                t=t->link;
            temp->data=num;
            temp->link=NULL;
            t->link=temp;
        }
    }
    
    void printlist(struct node **n)
    {
        struct node *t;
        t=*n;
        if(t==NULL)
            printf("\nEmpty list");
    
        while(t!=NULL)
        {
            printf("\n%d",t->data);
            printf("\t%u address=",t);
            t=t->link;
        }
    }
    
    
    void delete(struct node **n)
    {
        struct node *temp,*t;
        temp=*n;
        temp->data=temp->link->data;
        t=temp->link;
        temp->link=temp->link->link;
        free(t);
    }    
    
    int main()
    {
        struct node *ty,*todelete;
        ty=NULL;
        populate(&ty,1);
        populate(&ty,2);
        populate(&ty,13);
        populate(&ty,14);
        populate(&ty,12);
        populate(&ty,19);
    
        printf("\nlist b4 delete\n");
        printlist(&ty);
    
        printf("\nEnter node pointer to delete the node====");
        scanf("%u",&todelete);
        delete(&todelete);
    
        printf("\nlist after delete\n");
        printlist(&ty);
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题