0

For homework I was given this code with the directive to implement a recursive function that calls itself on the next node in the list in main unless the current node is NULL or the value of the current node is equal to 'target'

My recent attempt is the fenced part of the code below. I can get it to print 0, but that's not the whole list. I'm not sure what I'm doing wrong as I've not had much experience with linked lists and nodes.

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;
}

No error messages were given, except a prior segmentation fault I've already 'fixed' but I think it's supposed to print the whole list.

5
  • 1
    A hint about a definitive problem: What does find_match return if cur->value != NULL is true? Commented Jul 25, 2019 at 3:16
  • 1
    Then double check what parameters you pass when you recursively call find_match. (But wait! There's more...) Commented Jul 25, 2019 at 3:20
  • I thought I wouldn't want find_match to return anything until it was done going through the list, which in that case it goes to the else statement when cur->value eventually reaches NULL Commented Jul 25, 2019 at 3:30
  • 1
    If you declare a function to return a value, it must return a value. Otherwise you will have undefined behavior. Just think about your function, how will node* target = find_match(head, "dunk"); ever work if the function doesn't actually return anything? Commented Jul 25, 2019 at 4:10
  • 1
    find_match() doesn't return when curr->value is not NULl, which results in returning trash. For debugging segfaults, I recommend using Valgrind. Commented Jul 25, 2019 at 10:57

2 Answers 2

2

You can insert a single element at a time and send the each element using loop because you know the size of array.Then you have to little change in your code.

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;
     }

Then you can search with your Key just like printing the all element in the List

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

But this question is known and before posting any question make sure you try and Search enough in Google !!. Hope you get the idea and implement it in your own way.

Sign up to request clarification or add additional context in comments.

Comments

1

There are a few issues in the code.

  1. The problem is in the findmatch function. In this as per the problem statement, the target node should be returned if it is present, else NULL should be returned. This can be achieved as below.

    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;
        }
      }
    

Additional points

  1. In the create_node function you are directly copying the string pointer. This may work in this specific case, but you should ideally allocate memory for the value field separately.

    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. While printing the value, you should check if the returned value from findmatch is NULL

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.