实现链接列表,但生成一个新列表的函数仅包含偶数数字是错误的
我实现了一个链接列表。 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于初学者,指针
l1
没有初始化,因此该程序具有不确定的行为,因此具有不确定的值。
您需要像
这样的条件像这样的条件
来初始化它是没有意义的。由于条件,列表的最后一个节点被忽略。
您需要使用以下条件,
例如函数
print_list
应该看起来像请注意函数(以及MAIN)中的
函数
filter_even
之内再次 有错字。使用了一个非初始化的指针,您必须编写
循环
,并且必须代替
,此外,函数返回类型为
list
,但是函数返回0
(这是一个零指针),这是没有意义的。返回语句必须看起来像
,您应该写作
,不要忘记释放列表。
For starters the pointer
l1
was not initialized and has an indeterminate valueAs a result the program has undefined behavior.
You need to initialize it like
The condition like this in the while statement
does not make a sense. Due to the condition the last node of the list is ignored.
You need to use the following condition
For example the function
print_list
should look likePay attention to that within the function (and within main) there is a typo
Again within the function
filter_even
there is used an uninitialized pointerYou have to write
And the while loop has to be
instead of
And moreover the function return type is
list
but the function returns0
(that is a null pointer) that does not make a sense.The return statement must look like
And in main you should write
And do not forget to free the lists.