2016-07-29 96 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++; 
    }  
}