0

I am new to C and trying to learn function pointer.I am supposed to complete the 'map_list'function which takes a linked list and a function pointer,and return a new list in the same order, but with all the values squared.Please point it out where I did wrong.

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


struct Link {
    struct Link *next;
    int value;
};

void print_list(struct Link *list) {
    for(struct Link *l = list; l != NULL; l = l->next) {
        printf("%d", l->value);

        if(l->next) {
            printf(", ");
        }
    }

    printf("\n");
}


struct Link *append(int x, struct Link *head) {
    struct Link *head_ = (struct Link*)malloc(sizeof(struct Link));
    head_->next = head;
    head_->value = x;

    return head_;
}

struct Link *reverse_list(struct Link *list) {
    struct Link *head = NULL;

    for(struct Link *l = list; l != NULL;) {
        struct Link *next = l->next;
        l->next = head;
        head = l;

        l = next;
    }

    return head;
}

struct Link *map_list(struct Link *link_list,int (*Square)(int)   ) {

    struct Link *new_list = NULL;
    new_list = new_list ->next;
    new_list ->value = (*Square)(link_list ->value);
    return new_list;
}

int square(int x) {
    return x * x;
}

int add3(int x) {
    return x + 3;
}



struct Link *theList() {
    struct Link *l = append(1, NULL);
    l = append(2, l);
    l = append(3, l);
    l = append(5, l);
    return l;
}

int main() {


    struct Link *l = theList();
    print_list(map_list(l, &square));
    ;
    return 0;
}

I got 'Segmentation fault (core dumped)'

11
  • 3
    Think about the lines struct Link *new_list = NULL; and new_list = new_list ->next;. How will that work? And for the rest of the function, how would a new list be created (or the original modified) without any iteration? Commented Jul 22, 2019 at 9:35
  • OT: regarding: struct Link *head_ = (struct Link*)malloc(sizeof(struct Link)); 1) the returned type is void* which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc. 2) always check (!=NULL) the returned value to assure the operation was successful. Commented Jul 22, 2019 at 18:33
  • regarding: struct Link *reverse_list(struct Link *list) { Your question did not mention anything about reversing the order of the list, so why this function? Please post a minimal reproducible example Commented Jul 22, 2019 at 18:37
  • OT: it is a very poor programming practice to include header files those contents are not used: Suggest removing the statements: #include <stdbool.h> #include <string.h> and #include <ctype.h Commented Jul 22, 2019 at 18:42
  • OT: parameter and variable names should indicate content or usage (or better, both) Names like l are meaningless, even in the current context Commented Jul 22, 2019 at 18:45

1 Answer 1

1

If I have understood correctly you have some trouble with writing the function map_list. It can look the following way

struct Link * map_list( const struct Link *link_list, int operation( int )   ) 
{
    struct Link *new_list  = NULL;
    struct Link **new_node = &new_list;

    for ( const struct Link *current = link_list; current != NULL; current = current->next )
    {
        *new_node = malloc( sizeof( struct Link ) );

        ( *new_node )->next  = NULL;
        ( *new_node )->value = operation( current->value );

        new_node = &( *new_node )->next;
    } 

    return new_list;
}

And the function can be called for example like

map_list( l, square );

or

map_list( l, add3 );

The function does not check whether a memory allocation for a node was successfull. You can add such a check yourself.

As for your own function implementation

struct Link *map_list(struct Link *link_list,int (*Square)(int)   ) {

    struct Link *new_list = NULL;
    new_list = new_list ->next;
    new_list ->value = (*Square)(link_list ->value);
    return new_list;
}

then for starters it has undefined behavior

ew_list = new_list ->next;

and does not make sense relative to the assignment.

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

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.