实现递归函数时出现问题

时间:2019-07-25 03:07:16

标签: c recursion linked-list

对于作业,我为该代码提供了指令,以实现一个递归函数,该函数在main列表中的下一个节点上调用自身,除非当前节点为NULL或当前节点的值等于'target'< / p>

我最近的尝试是下面代码的受限制部分。我可以将其打印为0,但这不是整个列表。我不确定自己在做什么错,因为我对链接列表和节点没有太多的经验。

typedef struct node {
  struct node* next;
  unsigned short index;
  char* value;
} node;

node* create_node(unsigned short index, char* value) {
  node* n = malloc(sizeof(node));
  n->value = value;
  n->index = index;
  n->next = NULL;
  return n;
}

node* create_nodes(char* values[], unsigned short index, unsigned short num_values) {
  if(num_values == 0) {
    return NULL;
  }

  node* n = create_node(index, values[0]);

  n->next = create_nodes(values + 1, index + 1, num_values - 1);
  return n;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node* find_match(node* cur, char* target) {
  if(cur == NULL){
    return NULL;
  }
    if(cur->value != NULL){
      node* result = create_node(cur->index, cur->value);
      result->next = find_match(cur->next, cur->value);
    }else{
    return find_match(cur->next, cur->value);
    }
  }
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int main() {
  char* values[] = {
    "foo", // 0
    "bar", // 1
    "baz", // 2
    "duh", // 3
    "dunk", // 4
    "derp" // 5
  };

  node* head = create_nodes(values, 0, 6);

  node* target = find_match(head, "dunk");

  printf("%d\n", target->index);

  return 0;
}

没有给出任何错误消息,只是我已经“修复”了先前的分段错误,但我认为它应该打印整个列表。

2 个答案:

答案 0 :(得分:2)

您可以一次插入一个元素,然后使用循环发送每个元素,因为您知道数组的大小。然后,您的代码几乎不需要更改。

struct linkList {
       int data;
       linkList* next;
  }node;

 node create(int val){
       node tmp;
       tmp = (node)malloc(sizeof(struct linkList));
       tmp->data = val;
       return tmp;
  }
 node* insertNodeAtHead(linkList* llist,int data) {
        node tmp;
        tmp = create(data);
        tmp->next = llist;
        return tmp;
     }

然后,您可以使用键进行搜索,就像打印列表中的所有元素一样

void print(linkList* head) {
     while(head !=NULL){
     printf("%d\n",head->data); // check here is this your key or Not 
     head = head->next;
   }
 } 

但是这个问题是已知的,在发布任何问题之前,请确保您尝试并在Google !!中搜索足够!希望您能想到并以自己的方式实施。

答案 1 :(得分:1)

代码中有一些问题。

  1. 问题出在findmatch函数中。按照问题说明,如果存在目标节点,则应返回目标节点,否则应返回NULL。可以通过以下方式实现。

    node* find_match(node* cur, char* target) {
      if(cur == NULL){
        return NULL;
      }
        if(strcmp(cur->value,target) ==0){
            return (cur);
        }else if (cur->value != NULL){
            return find_match(cur->next, target);
        }
        else {
            return NULL;
        }
      }
    

其他要点

  1. create_node函数中,您正在直接复制字符串指针。在这种特定情况下,这可能会起作用,但是理想情况下,您应该为value字段单独分配内存。

    node* create_node(unsigned short index, char* value) {
      node* n = malloc(sizeof(node));
      n->value = strdup(value);
      n->index = index;
      n->next = NULL;
      return n;
    }
    
  2. 在打印值时,应检查从findmatch返回的值是否为NULL

    node* target = find_match(head, "dunk");
    if (target != NULL) {
      printf("%d\n", target->index);
    }
    else {
      printf (" Not found\n");
    }