简体   繁体   English

如何在递归函数中编辑指向列表节点的指针?

[英]How can I edit a pointer to a list node from a function in a recursion?

I have been writing a program that is quite complex compared to what I have dealt with until now. 直到现在,我一直在编写一个相当复杂的程序。 Anyways at some point I am supposed to write a function that will manipulate a struct list. 无论如何,我应该编写一个操作结构列表的函数。 I'm trying to make this question as simple as possible so I wrote down a pretty simple piece of code just for reference. 我试图让这个问题尽可能简单,所以我写下了一段非常简单的代码供参考。

Here is the thing: at first I call testf from another function providing it with a valid current as well as an i with a value of 0. This means that testf will call itself about 100 times before it starts accessing the rest of the code. 事情是这样的:首先我从另一个函数调用testf ,为它提供有效current以及值为0的i 。这意味着testf在开始访问其余代码之前会调用自己大约100次。 This is when all the generated instances of testf will start getting resolved. 这是所有生成的testf实例开始得到解决的时候。

 void testf(listnode *current, int *i) {
   wordwagon *current2;

   current2 = current;
   if (*i < 100) {
     *i = *i + 1;
     current2 = current2->next;
     testf(current2, i);
   }


   current = current->next;
   return;
 }

If, let's say, I have enough connected list nodes at my disposal, is current = current->next; 如果,假设我有足够的连接列表节点,那么current = current->next; the correct way for the "last" testf function to access and edit the caller's current2 value (which is this function's current ), or am I horribly wrong? “最后”testf函数访问和编辑调用者的current2值(这是该函数的current )的正确方法,还是我可怕的错误? If I am, what is the way to make changes to the caller function's variables from inside the called function and be sure they won't go away as soon as the called function returns? 如果我是,从被调用函数内部更改调用函数的变量的方法是什么,并确保它们不会在被调用函数返回后立即消失? I find it kind of hard to get a good grasp on how pointers work. 我发现很难掌握指针的工作原理。

It is very likely that I have left out important information or that I haven't asked my question clearly enough. 我很可能遗漏了重要信息,或者我没有清楚地问我的问题。 Please inform me if that is the case so I can edit in whatever you need. 如果是这种情况请通知我,以便我可以根据您的需要进行编辑。

Thanks in advance. 提前致谢。

You can pass pointer to a pointer in your function, and derefrence it to get a listnode pointer back , here is how the code will look like after that ( not tested for compilation ) : 您可以将指针传递给函数中的指针,并将其解析以获取listnode指针,这是代码在此之后的样子(未经过编译测试):

void testf(listnode **current, int *i) {  // accept pointer to listnode pointer
   wordwagon *current2;

   current2 = *current;   // retreive pointer value by dereferece
   if (*i < 100) {
     *i = *i + 1;
     current2 = current2->next;
     testf(&current2, i);  // recursively call by reference to the pointer
   }

   *current = (*current)->next; /* change the current pointer next pointer, CORRECTED as suggested by Azure */
   return;
 }

Here is a list of really good articles for learning pointers : 这是学习指针的非常好的文章列表:

a) http://cslibrary.stanford.edu/102/PointersAndMemory.pdf a) http://cslibrary.stanford.edu/102/PointersAndMemory.pdf

b) http://cslibrary.stanford.edu/103/LinkedListBasics.pdf b) http://cslibrary.stanford.edu/103/LinkedListBasics.pdf

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用给定的递归在单链表中插入节点:1. 指向头节点的指针 2. 插入新节点的索引 - How can I insert a node in a singly linked list using recursion given: 1. a pointer to the head node 2. the index to insert the new node 如何使用单个指针从链表中删除节点? - How can I delete an node from a linked list with a single pointer? 如何将功能分配给功能指针 - How Can I Assign a Function to a Function Pointer 如何在递归函数中将指针变量从被调用函数传递给调用函数? - how to pass a pointer variable from the called function to the calling function in a recursion function? 如果我收到带有指针的指针的指针地址,是否可以通过函数修改指针值? - Can I modify a pointer value, from a function, if i receive the pointer address, with a pointer to pointer? 如何消除以下函数中的尾递归(从两个递归调用到一个)? - How can I eliminate tail recursion in the following function (from two recursive calls to one)? 为什么`(*)`可以从函数参数列表中的函数指针中省略? - Why can `(*)` be omitted from a function pointer inside a function parameter list? 如何在结构中存储函数指针? - How can I store a function pointer in a structure? 如何访问链表的 null 指针(节点)? - How do I access a null pointer (node) of a linked list? 我如何从该列表中删除一个节点然后进行打印? - How can i remove a node from this list and then print it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM