简体   繁体   English

从LinkedList删除节点

[英]Removing Node from LinkedList

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

I successfully revamp my code and now I am able to remove the node and display out, but the whole program just crash after my printList function. 我成功修改了代码,现在可以删除节点并显示出来,但是整个程序在我的printList函数之后崩溃了。 It does not go back to remove node and I cant continue removing other indexes. 它不会返回删除节点,并且我无法继续删除其他索引。

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

removeNode(head, index); should be removeNode(&head, index); 应该是removeNode(&head, index);

And printList(ptrHead); printList(ptrHead); should be printList(*ptrHead); 应该是printList(*ptrHead); (in removeNode ) I have the feeling this piece of code goes crazy that's why your app is not responding anymore. (在removeNode )我感到这段代码很疯狂,这就是为什么您的应用程序不再响应的原因。

What compiler do you use? 您使用什么编译器? It should have warned you. 它应该警告过您。

when you enter index = 0, the inner if block of removenode function will not be executed as the findnode functin returns NULL. 当输入索引= 0,内ifremovenode功能不会被执行作为findnode载体作用返回NULL。 But in your output for index = 0 you got node 2 removed. 但是在索引为0的输出中,删除了节点2。 How did u get that? 你是怎么得到的?

In your updated code: 在更新的代码中:

  • You still do not set next to NULL. 没有设置next为NULL。
  • You never increment index when you fill list. 填充列表时,您永远不会增加index
  • You do not check if scanf() succeeds. 您不检查scanf()成功。
  • There is no need to use pointer to pointer in removeNode() , you can use: 无需在removeNode()使用指向指针的指针,可以使用:

     int removeNode(ListNode *ptrHead, int index); 
  • You have added removeNode() in printList() which is really out of place. 您已经在printList()添加了removeNode() ,这实际上是printList()的。
  • You do not have any free function for the list. 您没有该列表的免费功能。
  • ... ...

Original answer to original code: 原始代码的原始答​​案:


  • Missing header file. 头文件丢失。
  • Miss match between function signatures in declaration and definition. 声明和定义中的函数签名之间缺少匹配。
  • You never set next to NULL. 您永远不会在NULL next设置。
  • You do not free list before exit. 您没有在退出前释放列表。
  • ... ...

Code with some comments: 带有一些注释的代码:

/* 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM