通过用C递归循环名单 [英] Looping through recursive list in C

查看:145
本文介绍了通过用C递归循环名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始用C和我想整个指针/的malloc /自由是快把我逼疯了。我试图通过它来定义一个简单的线性递归数据结构和循环,打印,我已经遍历每个元素。 (code如下)。

I've just started out with C and I think the whole pointers/malloc/free is driving me mad. I tried to define a simple linear recursive data structure and loop through it, printing each element that I've looped through. (Code as below).

不过,我得到段错误:一旦11我尝试移动到下一个元素,以插入一个新元素

However, I'm getting Segmentation Fault: 11 as soon as I try to move to the next element to "insert" a new element

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

 typedef struct number_list {
     int num;
     struct number_list *next_num;
 } numlist;

 int main() {
     numlist *cur, *pointer;
     numlist *NewList=NULL;

     cur = NewList;

     cur = malloc(sizeof(numlist));
     cur->num=5; // this operation is never reached too
     cur = cur->next_num // Must I malloc? 
     printf("Reached."); // Is never reached.
     cur->num=9;

     pointer=NewList;

     while (pointer!=NULL) {
         printf("%d", pointer->num);
         pointer=pointer->next_num;
     }
     return 0;
 }

此外,我还有另一个较大的程序,同时,循环功能完全一样,而环这里除了一个装递归结构。因此,我实际上并不需要创建任何新的元素,只需要运行通过并打印每个元素的。再11:然而,在这种时刻,环路打印的最后一个元素,它崩溃与分段错误。我猜可能是因为我试图做指针=指针 - &GT; next_num 。我该如何正确,然后通过这样的数据结构上运行C正确呢?

Furthermore, I have on another larger program a while-loop that functions exactly like this while-loop here except on a "filled" recursive structure. Hence I don't actually need to create any new element, just run through and print each element out. However, in that moment where the loop prints the last element, it crashes with Segmentation Fault: 11 again. I'm guessing it's likely because I tried to do pointer = pointer->next_num. How do I then correctly run through such a data structure correctly on C anyway?

推荐答案

您有几个概念问题。首先,你需要知道的指针,它指向的内存之间的区别;

You have a few conceptual issues. First you need to know the distinction between the pointer and the memory it points to;

int a1 = 0;
int a2 = 0;
int *b = &a1;
*b = 3;
// now a1 = 3, a2 = 0
b = &a2;
*b = 2;
// now a1 = 3, a2 = 2

这意味着,在以下两行:

That means that in these two lines:

cur = NewList;
cur = malloc(sizeof(numlist));

第二完全取代第一个任务。

the second completely replaces the first assignment.

现在这一行:

cur = cur->next_num;

next_num 还没有确定,所以CUR设置为垃圾内存(所以它甚至不是空malloc不为零的内存)。你应该做的是;

Well next_num hasn't been set yet, so cur is set to garbage memory (malloc doesn't zero the memory so it's not even NULL). What you should do is;

cur->next_num = malloc( sizeof( numlist ) );
cur = cur->next_num;
cur->next_num = NULL; // explicitly NULL-cap, because of issue mentioned above.

最后,NewList仍是在年底NULL。您应该使用 NewList = CUR。你的第一行的malloc后

在实际code,你干脆把这个最成可重用的功能,但对于学习这些补丁应该足够了。

In real code, you would put most of this into reusable functions, but for learning those fixes should suffice.

另外打印,你提的是,因为归零问题,有可能是最后一个元素之后的崩溃。你的,而循环本身是好的,据我所看到的。

Also the crash after printing the last element which you mention is likely because of the NULLing issue. Your while loop itself is fine as far as I can see.

这篇关于通过用C递归循环名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆