【问题标题】:How to change recursive into a while loop statement(i.e iteratative)如何将递归更改为 while 循环语句(即迭代)
【发布时间】:2021-03-07 04:04:06
【问题描述】:

我正在研究一个链表代码,发现除了使用递归之外,还可以使用 for 或 while 循环。因此,我尝试将递归更改为 while 循环(即迭代),但似乎我的逻辑完全不合适..

我只想更改以下代码中的listing 函数。你能帮帮我吗?

递归代码如下(这是我想要的结果的代码):

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

typedef struct linkedlist{
    char first;
    struct linkedlist *second;
}linkedlist;
typedef linkedlist *NODE;


NODE listing(char string[]){  //This is the function that needs to change using a loop.
    NODE head;
    if(string[0] == '\0')
    return NULL;
    else{
        head = (struct linkedlist *)malloc(sizeof(linkedlist));
        head -> first = string[0];
        head -> second = listing(string + 1);
        return head;
    } 
} 

void print(NODE head){
    if(head == NULL)
    printf("NULL\n");
    else{
        printf("%c -> ", head -> first);
        print(head -> second);
    }
}

int main(void){
    char in_string[10];
    NODE head;
    printf("insert a string : ");
    scanf("%s", in_string);
    head = listing(in_string);
    printf("result of the list...\n");
    print(head);
    return 0;
}

递归代码的结果是,如果我输入trial,它会给我t -&gt; r -&gt; i -&gt; a -&gt; l -&gt; NULL的结果。

我尝试更改并最终导致错误的代码如下:

NODE listing(char string[]){
   NODE head;
   int i = 0;
   while(1){
      if(string[i] == '\0'){
         return NULL;
      }
      else{
         head = (struct linkedlist *)malloc(sizeof(ELEMENT));
         head -> first = string[i];
         i++;
         return head;
      }
   }
} 

我真的为此苦苦挣扎了好几个小时...请你帮我改一下,这样我得到相同的结果吗?

【问题讨论】:

  • 您从未在迭代函数中的任何位置指定second,您希望它如何工作?

标签: c recursion while-loop linked-list singly-linked-list


【解决方案1】:

试试这样的:

NODE listing(char string[]){ 
    NODE head;
    head = (struct linkedlist *)malloc(sizeof(linkedlist));
    NODE cur = head;
    int i = 0;
    while(string[i] != '\0')
    {
        cur->first = string[i];
        if (string[i+1] != '\0') cur->second = (struct linkedlist *)malloc(sizeof(linkedlist));
        else cur->second = NULL;
        cur = cur->second;
        i++;
    } 
    return head;
}

int main(void){
    char in_string[10] = {0};
    memcpy(in_string, "trial", 5);
    NODE head = listing(in_string);
    print(head);     //==> t -> r -> i -> a -> l -> NULL
    return 0;
}

【讨论】:

    【解决方案2】:

    使用 while 循环定义非递归函数就足够简单了。

    NODE listing( const char string[] )
    {
        NODE head = NULL;
        NODE *current = &head;
    
        while ( *string )
        {
            *current = ( struct linkedlist * )malloc( sizeof( linkedlist ) );
            ( *current ) -> first = *string++;
            ( *current ) -> second = NULL;
            current = &( *current )->second;
        }
    
        return head; 
    } 
    

    注意对这样的指针使用 typedef

    typedef linkedlist *NODE;
    

    不是一个好主意。它只会让代码的读者感到困惑。

    【讨论】:

      猜你喜欢
      • 2020-12-24
      • 1970-01-01
      • 2020-08-28
      • 2015-03-14
      • 2010-12-19
      • 2019-03-01
      • 2013-04-15
      • 2020-03-24
      相关资源
      最近更新 更多