基于列表的程序崩溃,出现0xC0000005

问题描述 投票:1回答:2

我刚上大学,对编程还是比较陌生的。我的任务是开发一个程序(通过使用具有整数元素的指针的函数和链接列表),该程序将执行以下操作:

  • 函数从头开始访问每个元素。如果当前元素加一个是后一个元素的<=(例如,如果第一个是i,第二个是j,j> = i + 1),则必须完成通过将i和j-1之间的所有值相加

  • 列出
  • 如果当前元素后跟一个元素<=,它将被列表删除并添加到形式参数中声明的数组中。您还必须在形式参数中添加一个已删除元素的计数器。

从评论中添加:如果程序运行第二个元素大于第一个元素[,],则通常会崩溃。

我完全不知道我在做什么错。对不起,如果我在语法上有任何错误,英语不是我的母语。

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

struct list {
    int value;
    struct list * next_ptr; 
};

void pre_insert(struct list ** ptrptr, int value) {

    struct list * tmp_ptr;

    tmp_ptr = *ptrptr;
    *ptrptr = (struct list*)malloc(sizeof(struct list));
    (*ptrptr)->value = value;
    (*ptrptr)->next_ptr = tmp_ptr;
}

int complete_list_array(struct list * ptr, int * V, int * count) {

    struct list * succ_ptr;
    int i, k = 0;

    while (ptr != NULL) {  
        if (ptr->next_ptr != NULL) { 
            succ_ptr = ptr->next_ptr; 
            if (ptr->value + 1 <= succ_ptr->value) { 
                for(i = 1; ptr->value + i <= succ_ptr->value; i++) { //
                    succ_ptr = succ_ptr->next_ptr;
                    ptr = ptr->next_ptr;
                    pre_insert(&ptr, ptr->value+ i);
                 } 
                 ptr = ptr->next_ptr;
            }
            else if (ptr->value >= succ_ptr->value) { 
                ptr->next_ptr = succ_ptr->next_ptr; 
                V[k] = succ_ptr->value;
                free(succ_ptr);
                k++;
                (*count)++;
                ptr = ptr->next_ptr;
            }
        }
    }

    return (*count);
}

struct list * create_list(int N) {
    struct list * first_ptr, * ptr;
    int i;

    if(N == 0) {
        first_ptr = NULL;
    }
    else {
        first_ptr = (struct list *)malloc(sizeof(struct list)); 
        printf("Insert the first value:\n");
        scanf("%d", &first_ptr->value);
        ptr = first_ptr; 

        for(i = 2; i <= N; i++) {
            ptr->next_ptr = (struct list *)malloc(sizeof(struct list));
            ptr = ptr->next_ptr;
            printf("Insert element number %d:\n", i);
            scanf("%d", &ptr->value); 
        }

        ptr->next_ptr = NULL;
    }

    return(first_ptr);
}

void visit(struct list * ptr) {
    int i = 1;
    while(ptr != NULL) {
        printf("The element number %d in the list has value %d.\n", i, ptr->value);
        ptr = ptr->next_ptr;
        i++;
    }
}

int main() {

    struct list * first_ptr;
    int N;

    printf("Insert the number N of elements in the list.\n");
    scanf("%d", &N);

    first_ptr = create_list(N);

    printf("Elements of the list.\n");
    visit(first_ptr);

    int * V, count = 0;
    V = (int *)malloc(sizeof(int)* N);

    count = complete_list_array(first_ptr, V, &count);

    printf("count = %d", count);
}
c
2个回答
1
投票

succ_ptr = succ_ptr->next_ptr;

可以将succ_ptr设置为NULL。因此,在下一次迭代中,您会在[

中崩溃
for(i = 1; ptr->value + i <= succ_ptr->value; i++)

我认为您可以将整个for循环替换为:

while (ptr->value + 1 < ptr->next_ptr->value)
    pre_insert(&ptr->next_ptr, ptr->next_ptr->value-1);

0
投票

3条快速建议:

  • 关于您的评论:...我试图按照您的意思做,但是该程序仍然无法正常工作。它在打印所有元素后停止...在下面添加break;语句:

    while (ptr != NULL) { if (ptr->next_ptr != NULL) { succ_ptr = ptr->next_ptr; if (ptr->value + 1 <= succ_ptr->value) { for(i = 1; ptr->value + i <= succ_ptr->value; i++) { // succ_ptr = succ_ptr->next_ptr; ptr = ptr->next_ptr; pre_insert(&ptr, ptr->value+ i); } while (ptr->value + 1 < ptr->next_ptr->value) pre_insert(&ptr->next_ptr, ptr->next_ptr->value-1); ptr = ptr->next_ptr; } else if (ptr->value >= succ_ptr->value) { ptr->next_ptr = succ_ptr->next_ptr; V[k] = succ_ptr->value; free(succ_ptr); k++; (*count)++; ptr = ptr->next_ptr; } } else break;//here }

  • 对于每个malloc / calloc,请调用free(...);语句

  • 不转换malloc / calloc的返回即更改:*ptrptr = (struct list*)malloc(sizeof(struct list));到:*ptrptr = malloc(sizeof(struct list));

© www.soinside.com 2019 - 2024. All rights reserved.