求助,关于c语言链表排序的问题

做了两个链表 每个节点内有一个学生的编号和成绩,现在要求把两个链表合并,然后按照学号升序排列 不知道为啥 第65行总是报错 我整了一个小时了 没发现哪里错了,貌似运行时候显示说我引用了一个不可以引用的地址?求助大神谢谢了

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

#define LEN sizeof(struct Person)

struct Person {
    int num;
    float score;
    struct Person *next;
};

struct Person *createPerson();

void printLink(struct Person *);

struct Person *merge(struct Person *, struct Person *);

struct Person *mergeAndSort(struct Person *, struct Person *);

void sort(struct Person *[], int);

int main() {
    struct Person *p1 = createPerson();
    struct Person *p2 = createPerson();
//    struct Person *pPerson = merge(p1, p2);
    struct Person *pPerson = mergeAndSort(p1, p2);
    printLink(pPerson);

    return 0;
}

void sort(struct Person *arr[], int n) {
    struct Person *tmp;
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (arr[i]->num > arr[j]->num) {
                //swap
                tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
        }
    }
}

struct Person *mergeAndSort(struct Person *a, struct Person *b) {
    struct Person *arr[64];
    struct Person *head;
    int i = 0, x;
    while (a != NULL) {
        arr[i] = a;
        a = a->next;
        i++;
    }
    while (b != NULL) {
        arr[i] = b;
        b = b->next;
        i++;
    }
    sort(arr, i);

    //reCreateLink
    head = arr[0];
    head->next = arr[1];
    for (x = 1; x < i - 1; x++) {
        (arr[i])->next = arr[i + 1];
    }
    arr[x]->next = NULL;

    return head;

}

struct Person *merge(struct Person *a, struct Person *b) {
    struct Person *tmp = a;
    while (tmp->next != NULL) {
        tmp = tmp->next;
    }
    tmp->next = b;
    return a;
}

void printLink(struct Person *p) {
    while (p != NULL) {
        printf("%d\t%f\n", p->num, p->score);
        p = p->next;
    }

}

struct Person *createPerson() {
    struct Person *p1, *p2, *head;
    p1 = p2 = (struct Person *) malloc(LEN);
    scanf("%d %f", &p1->num, &p1->score);
    int size = 0;
    while (p1->num != 0) {
        size++;
        if (size == 1) {
            head = p1;
        } else {
            p2->next = p1;
        }
        p2 = p1;
        p1 = (struct Person *) malloc(LEN);
        scanf("%d %f", &p1->num, &p1->score);
    }
    p2->next = NULL;
    printf("Establish complete\n");
    return head;
};

1个回答

struct Person *createPerson() {
    struct Person *p1, *p2, *head;
    p1 = p2 = (struct Person *) malloc(LEN);
        p1->next = NULL;
    scanf("%d %f", &p1->num, &p1->score);
    int size = 0;
    while (p1->num != 0) {
        size++;
        if (size == 1) {
            head = p1;
        } else {
            p2->next = p1;
        }
        p2 = p1;
        p1 = (struct Person *) malloc(LEN);
                p1->next = NULL;
        scanf("%d %f", &p1->num, &p1->score);
    }
    p2->next = NULL;
    printf("Establish complete\n");
    return head;
};

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问