2016-07-29 93 views
1

给定单向链表和位置,我试图删除特定位置上的链接列表节点。 CODE:删除链接列表中给定位置的节点

#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

回答

0

出现此问题是由于这种错误说法

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

在这种情况下尾巴和温度是相同的节点。如果临时被删除,那么您将在下一个被删除节点的数据成员TEMP->下一

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

这里尾数将要删除的节点。

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

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; 
} 
0

进入您的删除功能whiletailstemp向前移动一个同一时间从同一个地址开始。节点不会被删除,因为您始终分配相同的值(换句话说,您只是每次确认下一个指针值)。

这意味着,在您取消之后,由于其中一个节点的free d内存,打印输出为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++; 
    }  
}