c语言关于结构的编写错误

如图,split代表全部的数,然后如果输入的数中有0, 0之后的都归为after,之前的都归为before。

img
我对于结构中套结构有点不理解,所以发出来

//我写的代码
// Given a list with at least one node, and exactly one 0,
// split the list into a list with everything before the 0,
// and a list with the 0 and everything after.
// Return a malloced split_list struct with each of these lists.
struct split_list *split(struct node *head) {
    struct split_list *p = malloc(sizeof (struct split_list));
    while (head != NULL) {
        if (head->data == 0) {
            while (head != NULL) {
                p->after->data = head->data;
                head = head->next;
                p->after = p->after->next;
            }
        } else {
            p->before->data = head->data;
            head = head->next;
            p->before = p->before->next;
        }
        head = head->next;
    }

    return p;

}
//相关代码
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

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

struct split_list {
    struct node *before;
    struct node *after;
};

struct split_list *split(struct node *head);
struct node *strings_to_list(int len, char *strings[]);
void print_list(struct node *head);

// DO NOT CHANGE THIS MAIN FUNCTION

int main(int argc, char *argv[]) {
    // create linked list from command line arguments
    struct node *head = strings_to_list(argc - 1, &argv[1]);

    struct split_list *list_split = split(head);
    printf("before = ");
    print_list(list_split->before);
    printf("after = ");
    print_list(list_split->after);

    return 0;
}





// DO NOT CHANGE THIS FUNCTION
// create linked list from array of strings
struct node *strings_to_list(int len, char *strings[]) {
    struct node *head = NULL;
    int i = len - 1;
    while (i >= 0) {
        struct node *n = malloc(sizeof (struct node));
        assert(n != NULL);
        n->next = head;
        n->data = atoi(strings[i]);
        head = n;
        i -= 1;
    }   
    return head;
}

// DO NOT CHANGE THIS FUNCTION
// print linked list
void print_list(struct node *head) {
    printf("[");    
    struct node *n = head;
    while (n != NULL) {
        // If you're getting an error here,
        // you have returned an invalid list
        printf("%d", n->data);
        if (n->next != NULL) {
            printf(", ");
        }
        n = n->next;
    }
    printf("]\n");
}

after指针都是空的,你就去操作data,必死无疑
而且原有链表节点已经有现成的,不需要再申请新空间,直接把源链表节点挂到after和before链表就可以了

//我写的代码
// Given a list with at least one node, and exactly one 0,
// split the list into a list with everything before the 0,
// and a list with the 0 and everything after.
// Return a malloced split_list struct with each of these lists.
struct split_list *split(struct node *head) {
    struct split_list *p = malloc(sizeof (struct split_list));
    p->after = NULL
    p->before = NULL;
    struct node * a = p->after;
    struct node * b = p->before;
    while (head != NULL) {
        if (head->data == 0) {
                if(p->after == NULL)
                {
                      p->after = head;
                      a = p->after;
                }
                else
                {
                      a->next = head;
                      a = a->next;
                }
            }
        } else {
           if(p->before == NULL)
                {
                      p->before= head;
                      b = p->before;
                }
                else
                {
                      b->next = head;
                      b = b->next;
                }
        }
        head = head->next;
    }
    return p;
}

//我写的代码
// Given a list with at least one node, and exactly one 0,
// split the list into a list with everything before the 0,
// and a list with the 0 and everything after.
// Return a malloced split_list struct with each of these lists.
struct split_list *split(struct node *head) {
    struct split_list *p = malloc(sizeof (struct split_list));
    p->after = NULL
    p->before = NULL;
    struct node * a = p->after;
    struct node * b = p->before;
    while (head != NULL) {
        if (head->data == 0) {
                if(p->after == NULL)
                {
                      p->after = head;
                      a = p->after;
                }
                else
                {
                      a->next = head;
                      a = a->next;
                }
            }
        } else {
           if(p->before == NULL)
                {
                      p->before= head;
                      b = p->before;
                }
                else
                {
                      b->next = head;
                      b = b->next;
                }
        }
        head = head->next;
    }
    return p;
}