2016-09-16 68 views
0

我试图做一些编程谜题来学习C,并且我在删除头节点时遇到了链接列表删除工作的问题。我认为这个问题非常简单,但我找不到它!我遇到的问题是使用delete()函数,当我尝试删除链接列表的头部时,它不会将其删除,而是将其更改为垃圾值。链接列表垃圾值c

任何人都可以帮助我吗?非常感谢!

这里的示例输出:

Generating list... 
    Inserted: 0 
    Inserted: 1 
    Inserted: 2 
    Inserted: 3 
    Inserted: 4 
List: 
0 1 2 3 4 
    Deleted: 4 
List: 
0 1 2 3 
    Deleted: 0 
List: 
8344720 1 2 3 

这里是我的源代码:

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


typedef struct Node { 
    int value; 
    struct Node* next; 
} node; 

// Append a node to the end of the linked list 
int insert(node* head, int value) { 
    node* current = head; 

    /* Check for sentinel value. If first element inserted, overwrite  head instead of appending. */ 
    if (head->value == 420) { 
     head->value = value; 
     head->next = NULL; 
     printf("\tInserted:\t%d\n", head->value); 
     return 0; 
    } 

    /* Traverse to end to append node */ 
    while (current->next != NULL) 
     current = current->next; 

    /* Build new node and append to tail*/ 
    current->next = malloc(sizeof(node)); 
    current->next->value = value; 
    current->next->next = NULL; 

    printf("\tInserted:\t%d\n", current->next->value); 
    return 0; 
} 

/* Accept a number and delete all nodes containing that value */ 
int del(node* head, int value){ 
    node* curr = head; 
    node* prev = NULL; 
    node* del = NULL; 

    printf("\tDeleted:\t%d\n", value); 

    if (head == NULL) { 
     printf("Can't delete value from empty list!\n"); 
     return 1; 
    } 

    /* Search list remove all instances of value. Watch for edge cases. */ 
    while (curr != NULL) { 
     if (curr->value == value){ 
      /* Head case (lol) */ 
      if (curr == head) { 
       del  = head; 
       head  = head->next; 
       curr  = head; 
       free(del); 
      } 
      /* Tail case */ 
      else if (curr->next == NULL) { 
       del  = curr; 
       curr  = prev; 
       curr->next = NULL; 
       free(del); 
       return 0;  /* End of list, break out of loop to avoid segfaulting */ 
      } 
      /* Body case (base case) */ 
      else { 
       del  = curr; 
       curr  = curr->next; 
       prev->next = curr; 
       free(del); 
      } 
     } 
     prev = curr; 
     curr = curr->next; 
    } 

    return 0; 
} 

/* Accept head pointer and print until end of list */ 
int traverse(node* head) { 
    node* current = head; 

    if (head == NULL){ 
     printf("Can't traverse null list!\n"); 
     return 1; 
    } 

    printf("List:\n"); 
    while(current != NULL) { 
     printf(" %d ", current->value); 
     current = current->next; 
    } 
    printf("\n"); 

    return 0; 
} 

/* Let's begin our crazy experiment.... */ 
int main() { 
    node* head = NULL; 
    head  = malloc(sizeof(node)); 
    head->value = 420; 
    head->next = NULL; 


    printf("Generating list...\n"); 

    int value; 
    for (value = 0; value < 5; value++) 
     insert(head, value); 

    traverse(head); 

    del(head, 4); 
     traverse(head); 

    del(head, 0); 
     traverse(head); 

    return 0; 
} 
+2

标准初学者的错误。 'head = head-> next'。 C是按价值传递的。因此该行不会更改调用者看到的原始“head”值,而只会更改“head”值的*本地副本。 – kaylum

+0

[如何修改已传递到C中的函数的指针]的可能重复(http://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has -been-传入到-A-函数式-c)的 – kaylum

回答

1

要修改的德尔()函数内部的头部和使用旧的头从main()。您需要传递头部地址以删除并修改它,以便更改将反映在main中。你可能需要这样的东西。

int del(node **head, int value){ node* curr = *head; ....

并从主

del(&head);