【问题标题】:Removing Node from LinkedList从 LinkedList 中删除节点
【发布时间】:2014-03-27 09:57:37
【问题描述】:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
typedef struct _listnode
{
    int item;
    struct _listnode *next;
} ListNode;

int removeNode(ListNode **ptrHead, int index);
void printList(ListNode *head);
ListNode * findNode(ListNode *head, int index);

int main()
{
    ListNode *head = NULL, *temp=NULL;
    int i = 0;
    int index = 0;
    while (1)
    {
        printf("Enter a integer: ");
        scanf("%d", &i);
        if (i == -1)
            break;
        if (head == NULL)
        {
            head = malloc(sizeof(ListNode));
            temp = head;
        }
        else{
            temp->next = malloc(sizeof(ListNode));
            temp = temp->next;
        }
        temp->item = i;
    }
    removeNode(&head, index);
    return 0;
}

void printList(ListNode *head)
{
    int i = 0;
    if (head == NULL)
        return;
    while (head != NULL)
    {
        printf("%d ", head->item);
        head = head->next;
    }
    printf("\n");
}
ListNode * findNode(ListNode *head, int index)
{
        if (head == NULL || index   <   0)
        return  NULL;

        while (index    >   0){
            head = head->next;
            if (head == NULL)
                return  NULL;
            index--;
        }
        return  head;
}

int removeNode(ListNode **ptrHead, int index)
{
    ListNode *pre, *cur,*temp;
    if (index >= 0)
    {
        printf("Enter index to remove: ");
        scanf("%d", &index);
        if ((pre = findNode(*ptrHead, index - 1)) != NULL)
        {
            cur = pre->next;
            temp = cur;
            pre->next = cur->next;
            free(temp);
            printList(*ptrHead);
        }

    }
    return -1;
}

我成功地修改了我的代码,现在我可以删除节点并显示出来,但是整个程序在我的 printList 函数之后崩溃了。它不会返回删除节点,我无法继续删除其他索引。

Output:
Enter a value: 2 
Enter a value: 4 
Enter a value: 6 
Enter a value: 8 
Enter a value: -1 
Enter index to remove: 2 
Current list: 2 4 8 
Enter index to remove: 0 
Current list: 4 8 
Enter index to remove: -1

【问题讨论】:

  • 请添加findNode函数定义。
  • 如果findNode返回最后一个节点,则不应使用pre-&gt;next
  • 有趣的谜题......如果你有一个 addNode 或类似的东西......会很高兴看到
  • 发布完整代码SSCCE

标签: c list linked-list


【解决方案1】:

当您输入 index = 0 时,removenode 函数的内部 if 块将不会被执行,因为 findnode 函数返回 NULL。但是在 index = 0 的输出中,您删除了节点 2。你是怎么知道的?

【讨论】:

    【解决方案2】:

    removeNode(head, index); 应该是removeNode(&amp;head, index);

    printList(ptrHead); 应该是printList(*ptrHead);(在removeNode 中)我感觉这段代码发疯了,这就是你的应用不再响应的原因。

    你使用什么编译器?它应该警告过你。

    【讨论】:

      【解决方案3】:

      在您更新的代码中:

      • 仍然next设置为NULL。
      • 在填写列表时,您永远不会增加 index
      • 你不检查scanf()是否成功。
      • removeNode()中的指针不需要使用指针,可以使用:

        int removeNode(ListNode *ptrHead, int index);
        
      • 您在printList() 中添加了removeNode(),这真的不合适。
      • 您没有任何可用的列表功能。
      • ...

      原始代码的原始答​​案:


      • 缺少头文件。
      • 声明和定义中的函数签名不匹配。
      • 您永远不会将 next 设置为 NULL。
      • 退出前未释放列表。
      • ...

      带有一些 cmets 的代码:

      /* MISSING: <stdio.h> */
      #include <stdlib.h>
      typedef struct _listnode
      {
          int item;
          struct _listnode *next;
      } ListNode;
      
      /* Signature miss-match */
      int removeNode(ListNode **ptrHead, int index);
      void printList(ListNode *head);
      ListNode *findNode(ListNode *head, int index);
      
      int main()
      {
          ListNode *head = NULL, *temp=NULL;
          int i = 0;
          int index = 0;
          while (i != -1)
          {
              printf("Enter a integer: ");
              scanf("%d", &i);
              /* If -1 entered, -1 will be added to list. */
              if (head == NULL)
              {
                  head = malloc(sizeof(ListNode));
                  temp = head;
              }
              else{
                  temp->next = malloc(sizeof(ListNode));
                  temp = temp->next;
              }
              temp->item = i;
              /* temp->next never set to NULL */
          }
          /* Miss match between function signature and call. */
          removeNode(head, index);
      
          /* No freeing of list before exit. */
          return 0;
      }
      
      void printList(ListNode *head)
      {
          /* Redundant check of head != NULL */
          if (head == NULL)
              return;
          while (head != NULL)
          {
              printf("%d", head->item);
              head = head->next;
          }
      }
      ListNode *findNode(ListNode *head, int index)
      {
              if (head == NULL || index   <   0)
              return  NULL;
      
              while (index    >   0){
                  head = head->next;
                  if (head == NULL)
                      return  NULL;
                  index--;
              }
              return  head;
      }
      
      /* Why pass index as argument when it is not used? */
      int removeNode(ListNode **ptrHead, int index)
      {
          ListNode *pre, *cur,*temp;
          printf("Enter index to remove: ");
          scanf("%d", &index);
          /* Here you should check < 0, not  != -1. 
             What if user enters -9999 ? */
          if (index != -1)
          {
              if ((pre = findNode(*ptrHead, index - 1)) != NULL)
              {
                  cur = pre->next;
                  temp = cur;
                  pre->next = cur->next;
                  free(temp);
                  /* Bad return statement, should be int */
                  return;
              }
              /* You only print list if none was removed. */
              /* Miss match between function signature and call. */
              printList(ptrHead);
          }
          return -1;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-02
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-18
        • 2013-11-24
        • 1970-01-01
        相关资源
        最近更新 更多