实现链接列表,但生成一个新列表的函数仅包含偶数数字是错误的

发布于 2025-02-05 13:06:17 字数 1681 浏览 3 评论 0原文

我实现了一个链接列表。 Filter_even功能应创建一个仅包括数字的新链接列表。但是,以某种方式调用打印功能是第二次,新列表还包括两个随机数。输出看起来像这样:

1 2 3 4 4 2 6422356 1528349827。

任何人都可以向我解释我出错的地方吗?

struct le{
    int value;
    struct le *next;
};

typedef struct le listenelement;

typedef listenelement *list; 

void insert(int v, list * l){
    listenelement *new;
    new = malloc(sizeof(listenelement));
    new->value = v;
    new->next = *l;
    *l = new;
}
void print_list(list l){
    if (l == NULL) printf("Die Liste ist leer");
    else
        while (l->next != NULL){
            printf("%d\t", l->value);
            l = l->next;
        }
    zv;
}

void delete_all(list * l){
    list next;
    while (*l != NULL){
        next = (*l)->next;
        free(*l);
        *l = next;
    }
}

int position_of(int v, list l){
    int i = 0;
    while (l->next != NULL){
        if (v == l->value){printf("Der Wert %d erscheint in der Liste an der %d. Stelle (Index i = %d).", v, i + 1, i);
        return i;
        }
        i = i + 1;
        l = l->next;
    }
    printf("Der Wert %d erscheint nicht in der Liste.", v);
    return -1;
}

list filter_even(list l){
    list l_even; 
    int e;
    while (l->next != NULL){
        if ((l->value)%2 == 0){
            e = l->value;
            insert(e, &l_even);     
        }
        l = l->next;
    }
    print_list(l_even);  
    return 0;
}
int main(){

    list l1;
    int a = 4;
    insert(a, &l1);
    int b = 3;
    insert(b, &l1);
    insert(2, &l1);
    insert(1, &l1);
    print_list(l1);

    filter_even(l1);
    zv;

    return 0;
}

I implemented a linked list. the filter_even-function should creat a new linked list which only includes even numbers. But somehow, when calling the print function is second time the new list also includes two random numbers. Output looks like this:

1 2 3 4
4 2 6422356 1528349827.

Can anybody please explain to me where I went wrong?

struct le{
    int value;
    struct le *next;
};

typedef struct le listenelement;

typedef listenelement *list; 

void insert(int v, list * l){
    listenelement *new;
    new = malloc(sizeof(listenelement));
    new->value = v;
    new->next = *l;
    *l = new;
}
void print_list(list l){
    if (l == NULL) printf("Die Liste ist leer");
    else
        while (l->next != NULL){
            printf("%d\t", l->value);
            l = l->next;
        }
    zv;
}

void delete_all(list * l){
    list next;
    while (*l != NULL){
        next = (*l)->next;
        free(*l);
        *l = next;
    }
}

int position_of(int v, list l){
    int i = 0;
    while (l->next != NULL){
        if (v == l->value){printf("Der Wert %d erscheint in der Liste an der %d. Stelle (Index i = %d).", v, i + 1, i);
        return i;
        }
        i = i + 1;
        l = l->next;
    }
    printf("Der Wert %d erscheint nicht in der Liste.", v);
    return -1;
}

list filter_even(list l){
    list l_even; 
    int e;
    while (l->next != NULL){
        if ((l->value)%2 == 0){
            e = l->value;
            insert(e, &l_even);     
        }
        l = l->next;
    }
    print_list(l_even);  
    return 0;
}
int main(){

    list l1;
    int a = 4;
    insert(a, &l1);
    int b = 3;
    insert(b, &l1);
    insert(2, &l1);
    insert(1, &l1);
    print_list(l1);

    filter_even(l1);
    zv;

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2025-02-12 13:06:28

对于初学者,指针l1没有初始化,

list l1;

因此该程序具有不确定的行为,因此具有不确定的值。

您需要像

list l1 = NULL;

这样的条件像这样的条件

    while (l->next != NULL){

来初始化它是没有意义的。由于条件,列表的最后一个节点被忽略。

您需要使用以下条件,

    while ( l != NULL){

例如函数print_list应该看起来像

void print_list(list l){
    if (l == NULL) printf("Die Liste ist leer");
    else
        while ( l != NULL){
            printf("%d\t", l->value);
            l = l->next;
        }
}

请注意函数(以及MAIN)中的

zv;

函数filter_even之内再次 有错字。使用了一个非初始化的指针,

list l_even; 

您必须编写

list l_even = NULL;

循环

while (l != NULL){

,并且必须代替

while (l->next != NULL){

,此外,函数返回类型为list ,但是函数返回0(这是一个零指针),这是没有意义的。

return 0;

返回语句必须看起来像

return l_even;

,您应该写作

list l_even = filter_even(l1);

,不要忘记释放列表。

For starters the pointer l1 was not initialized and has an indeterminate value

list l1;

As a result the program has undefined behavior.

You need to initialize it like

list l1 = NULL;

The condition like this in the while statement

    while (l->next != NULL){

does not make a sense. Due to the condition the last node of the list is ignored.

You need to use the following condition

    while ( l != NULL){

For example the function print_list should look like

void print_list(list l){
    if (l == NULL) printf("Die Liste ist leer");
    else
        while ( l != NULL){
            printf("%d\t", l->value);
            l = l->next;
        }
}

Pay attention to that within the function (and within main) there is a typo

zv;

Again within the function filter_even there is used an uninitialized pointer

list l_even; 

You have to write

list l_even = NULL;

And the while loop has to be

while (l != NULL){

instead of

while (l->next != NULL){

And moreover the function return type is list but the function returns 0 (that is a null pointer) that does not make a sense.

return 0;

The return statement must look like

return l_even;

And in main you should write

list l_even = filter_even(l1);

And do not forget to free the lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文