C 中的链接列表按字母顺序排序?

Sha*_*med 4 c sorting

我试图按字母顺序对这个链接列表中的名称进行排序,但我不确定采取哪种方法是正确的。我创建了一个方法来比较列表中的名称并每次更新我的当前指针。我不断收到错误。谁能建议一种更好的方法来对名称进行排序?我是 C 语言的新手,我正在努力寻找更好的方法来实现这一点。任何帮助将不胜感激。

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

#define HOW_MANY  7

char *names[HOW_MANY] = { "Ben", "Chris", "RDJ", "Mark", "Scarlet", "Samuel", "Tom" };
int ages[HOW_MANY] = { 22, 24, 50, 26, 18, 32, 24 };

/* declare your struct for a person here */
struct person {
    char *name;
    int age;
    struct person *next;
};

static struct person *compare_people(struct person *headptr, struct person *headptr) {
    int didSwap = 1, limit = HOW_MANY - 1;
    struct person *temp;
    struct person *previous = headptr;
    struct person *new = headptr -> next;

    while (didSwap) {
        didSwap = 0;
        for (int i = 0; i < limit; i++) {
            if (strcmp(previous->name, new->name) > 0) {
                temp = previous;
                previous = new;
                new = temp;
                didSwap = 1;
            }
        }
        limit--;
    }
    return temp;
}

static struct person *insert_sorted(struct person *headptr, char *name, int age) {
    struct person *ptr;
    // Allocate heap space for a record
    ptr = malloc(sizeof(struct person));
    if (ptr == NULL)
        abort();
    // Assign to structure fields
    ptr->name = name;
    ptr->age = age;
    ptr->next = NULL;

    if (headptr == NULL) {
        ptr->next = headptr;
        headptr = ptr;
    } else {
        struct person *currptr = headptr;

        while (currptr != NULL) {
            currptr = compare_people(headptr, headptr);
        }
        headptr = currptr;
    }
    return headptr;
}

int main(int argc, char **argv) {
    // initialise the pointer  to be empty
    struct person *headptr = NULL;

    // To insert all the info in the array
    for (int i = 0; i < HOW_MANY ; i++) {
        headptr = insert_sorted(headptr, names[i], ages[i]);
    }

    struct person *current = headptr;
    while (current != NULL) {
        printf("The person's name is %s and the age is %d.\n", current->name, current->age);
        current = current->next;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

chq*_*lie 5

您的方法太复杂:比较函数执行某种排序,插入函数也执行某种排序。比较函数应该返回一个int其值为负数、0 或正数的值,例如strcmp(),并且 insert_sorted 应该person使用简单的迭代方法将新值插入到列表中的适当位置。

这是一个更简单的版本:

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

#define HOW_MANY  7

char *names[HOW_MANY] = { "Ben", "Chris", "RDJ", "Mark", "Scarlet", "Samuel", "Tom" };
int ages[HOW_MANY] = { 22, 24, 50, 26, 18, 32, 24 };

/* declare your struct for a person here */
struct person {
    char *name;
    int age;
    struct person *next;
};

static int compare_people(const struct person *a, const struct person *b) {
     return strcmp(a->name, b->name);
}

static struct person *insert_sorted(struct person *headptr, char *name, int age) {
    // Allocate heap space for a record
    struct person *ptr = malloc(sizeof(struct person));
    if (ptr == NULL) {
        abort();
    }

    // Assign to structure fields
    ptr->name = name;
    ptr->age = age;
    ptr->next = NULL;

    struct person **pp = &headptr;
    while (*pp != NULL && compare_people(ptr, *pp) >= 0) {
        pp = &(*pp)->next;
    }
    ptr->next = *pp;
    *pp = ptr;

    return headptr;
}

int main(int argc, char **argv) {
    // initialise the list to be empty
    struct person *headptr = NULL;

    // To insert all the info in the array
    for (int i = 0; i < HOW_MANY; i++) {
        headptr = insert_sorted(headptr, names[i], ages[i]);
    }

    struct person *current = headptr;
    while (current != NULL) {
        printf("The person's name is %s and the age is %d.\n", current->name, current->age);
        current = current->next;
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编辑:下面是带有简单指针的替代版本。您可以看到,我需要对空列表和开头插入进行特殊处理。

static struct person *insert_sorted(struct person *headptr, char *name, int age) {
    // Allocate heap space for a record
    struct person *ptr = malloc(sizeof(struct person));
    if (ptr == NULL) {
        abort();
    }
    ptr->name = name;
    ptr->age = age;
    ptr->next = NULL;

    if (headptr == NULL || compare_people(ptr, headptr) < 0) {
        ptr->next = headptr;
        return ptr;
    } else {
        struct person *cur = headptr;
        while (cur->next != NULL && compare_people(ptr, cur->next) >= 0) {
            cur = cur->next;
        }
        ptr->next = cur->next;
        cur->next = ptr;
        return headptr;
    }
}
Run Code Online (Sandbox Code Playgroud)