在释放先前节点后添加节点时程序崩溃

时间:2017-08-04 22:26:31

标签: c linked-list

新手问题。我有这个功能,

void removeNodes(Node *start)
{
    Node *temp;
    int counter = 0;

    while(start)
    {
        temp = start;
        start = start->next;

        free(temp);
        counter++;
    }

    printf("%d node/s has been removed\n\n", counter);
}

在我的main()中,我有选择权。

  1. 添加节点
  2. 删除所有节点
  3. 退出程序
  4. 第一组循环没有问题,直到我决定释放我刚用选项2创建的节点。问题从这里开始。释放它们之后我想添加另一组节点,在我再次进入第一个节点后,程序崩溃了。我做的第一件事就是将start中的main()重置为NULL。但它仍在打破这个计划。

    我正在阅读的这本书没有解释为什么会这样。或者也许他们拥有它,但它们在最后几页。我在第340/632页=)..新手在这里。也许我只是错过了其他老手的一些简单的东西。请帮助我。 TNX ..

    顺便说一下,这只是我main()函数的一个示例。我没有包括所有内容。

        Node *start = NULL;
        int choice;
        int pos = 1;
        int data;
        int node_qty = 0;
    
        while(1)
        {
            printf("1. Add node\n2. Delete all nodes\n3. Quit    ");
            scanf(" %d", &choice);
    
            if(choice == 1)
            {
                if(!node_qty)
                {
                   printf("Enter the value of the first node: ");
                   scanf(" %d", &data);
                }
                else
                {
                    printf("Enter a value: ");
                    scanf(" %d", &data);
    
                    do
                    {
                        printf("Enter the position: ");
                        scanf(" %d", &pos);
    
                        if(pos > node_qty + 1)
                        {
                            printf("Invalid input. Current node quantity: %d\n", node_qty);
                            printf("\n\n\n\n\n\n\n\n\n");
                            system("PAUSE");
                            system("cls");
                        }
                    }while(pos < 1 || pos > node_qty + 1);
                }
    
                insertNode(&start, pos, data);
    
                printf("\n\n\n\n\n\n\n\n\n");
                system("PAUSE");
                system("cls");
    
                node_qty++;
            }
            else if(choice == 2)
            {
                removeNodes(start);
    
                //reset
                start = NULL;
                node_qty = 0;
    
                printf("\n\n\n\n\n\n\n\n\n");
                system("PAUSE");
                system("cls");
            }
            else if(choice == 3)
            {
                printf("Program ends");
                break;
            }
        }
    
        //then after all of that, I am making sure that it will free the nodes
        removeNodes(start); //I don't think the problem is here.
    

    这是我的添加节点功能

    void insertNode(Node **start, int pos, int data)
    {
        Node *temp1 = malloc(sizeof(Node));
        temp1->data = data;
    
        Node *temp2 = *start;
    
        if(pos == 1)
        {
            temp1->next = *start;
            *start = temp1;
        }
        else
        {
            for(int i = 0; i < pos - 2; i++)
            {
                temp2 = temp2->next;
            }
    
            temp1->next = temp2->next;
            temp2->next = temp1;
        }
    }
    
    //and for checking, here's my print function
    void printNode(Node *start)
    {
        while(start)
        {
            printf("%d ", start->data);
    
            start = start->next;
        }
    
        printf("\n\n");
    }
    

1 个答案:

答案 0 :(得分:0)

如果在删除程序中的所有节点后立即创建新节点,则在start时访问NULL。 您应该找到一种方法,以阻止在-> start NULL insertNode()时使用if(*start == NULL) { *start = temp1; return; } 运算符。

你可以添加

temp1
创建pos之后

,这是新节点。 当列表为空时,只有第一个位置可供插入,因此next的值无关紧要。


另一件事是PeterJ指出,您在列表末尾添加的新节点的NULL未在insertNode()函数中生成printNode()。<登记/> 如果没有这个,当你使用temp1->next = NULL;函数遍历列表时,无法判断何时到达终点。

所以在temp中创建insertNode()后立即添加v-for

相关问题