从链接列表中删除节点

时间:2014-03-27 09:57:38

标签: c list linked-list

#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

3 个答案:

答案 0 :(得分:0)

removeNode(head, index);应为removeNode(&head, index);

并且printList(ptrHead);应为printList(*ptrHead);(在removeNode中)我觉得这段代码很疯狂,这就是为什么你的应用不再响应的原因。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

在您更新的代码中:

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

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

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


  • 缺少标题文件。
  • 声明和定义中的功能签名之间缺少匹配。
  • 您永远不会将next设置为NULL。
  • 退出前不要释放清单。
  • ...

代码中有一些评论:

/* 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;
}
相关问题