【问题标题】:Deleting a node at a given position in Linked List删除链表中给定位置的节点
【发布时间】:2016-07-29 12:37:22
【问题描述】:

给定一个单链表和一个位置,我试图删除一个特定位置的链表节点。 代码:

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node* next;
};

void printList(struct node* head_ref)
{
    //struct node* head_ref = (struct node*)malloc(sizeof(struct node));

    if(head_ref == NULL)
    printf("The list is empty");

    while(head_ref!=NULL)
    {
        printf("%d\n",head_ref->data);
        head_ref = head_ref->next;  
    }
}

void insert_beg(struct node **head_ref,int new_data)
{
    struct node* new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = new_data;
    new_node->next = *head_ref;
    *head_ref = new_node;
}

void delete(struct node **head_ref,int position)
{
    int i=1;
    if(*head_ref == NULL)
    return;

    struct node *tails,*temp = *head_ref;
    if(position == 0)
    {

        *head_ref = temp->next;
        free(temp);
        return;
    }

    while(temp->next!=NULL)
    { 
        tails = temp->next;
        temp = temp->next;

        if(i == position)
        {
            tails->next = temp->next;
            free(temp);
            return;     
        }

        i++;
    }

}

int main()
{
    struct node *head = NULL;
    insert_beg(&head,36);
    insert_beg(&head,35);
    insert_beg(&head,34);
    insert_beg(&head,33);

    printList(head);
    int position;
    printf("Enter the position of the node u wanna delete\n");
    scanf("%d",&position);

    delete(&head,position);
    printf("\n");
    printList(head);
}

每当我尝试删除位置 0 以上的节点时,我在该特定位置得到 0 而不是什么都没有。我能知道我哪里出错了吗? 例如,我的清单是:33 34 35 36 我的输出:33 0 35 36(尝试删除节点 1 时) 有效输出:33 35 36

【问题讨论】:

    标签: c pointers linked-list singly-linked-list


    【解决方案1】:

    由于这个错误的说法而出现问题

    while(temp->next!=NULL)
    { 
        tails = temp->next;
        ^^^^^^^^^^^^^^^^^^^
        temp = temp->next;
    

    在这种情况下,tails 和 temp 是相同的节点。如果 temp 被删除,则将已删除节点的数据成员 next 设置为 temp->next

        if(i == position)
        {
            tails->next = temp->next;
            ^^^^^^^^^^^^^^^^^^^^^^^^^
    

    这里的tails是要被删除的节点。

    您应该在删除节点之前更改节点的数据成员。所以错误的语句应该像这样更新

    while(temp->next!=NULL)
    { 
        tails = temp;
        ^^^^^^^^^^^^^
        temp = temp->next;
    

    对于我来说,我会按照以下方式编写函数

    int delete( struct node **head, size_t position )
    {
        struct node *prev = NULL;
    
        size_t i = 0;
    
        while ( i != position && *head != NULL ) 
        {
            prev = *head;
            head = &( *head )->next;
            ++i;
        }
    
        int success = *head != NULL;
    
        if ( success )
        {
            struct node *tmp = *head;
    
            if ( prev == NULL )
            {
                *head = ( *head )->next;
            }
            else
            {
                prev->next = ( *head )->next;
            }
    
            free( tmp );
        }
    
        return success;
    }   
    

    【讨论】:

      【解决方案2】:

      进入你的删除函数while循环tailstemp从同一个地址开始同时向前移动。该节点不会被删除,因为您总是分配相同的值(换句话说,您每次只确认下一个指针值)。

      这意味着,在您取消后,由于其中一个节点的 freed 内存,打印输出为 UB。

      更正您的代码:

      void delete(struct node **head_ref,int position)
      {
          int i=1;
          if(*head_ref == NULL)
          return;
      
          struct node *temp = *head_ref;
          if(position == 0)
          {
              *head_ref = temp->next;
              free(temp);
              return;
          }
      
          struct node *tails = *head_ref;
      
          while(temp->next!=NULL)
          {
              temp = temp->next;
      
              if(i == position)
              {
                  tails->next = temp->next;
                  free(temp);
                  return;
              }
      
              tails = tails->next;
      
              i++;
          }    
      }
      

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-11
        • 1970-01-01
        • 2019-02-23
        相关资源
        最近更新 更多