小编Sha*_*med的帖子

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

我试图按字母顺序对这个链接列表中的名称进行排序,但我不确定采取哪种方法是正确的。我创建了一个方法来比较列表中的名称并每次更新我的当前指针。我不断收到错误。谁能建议一种更好的方法来对名称进行排序?我是 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 …
Run Code Online (Sandbox Code Playgroud)

c sorting

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×1

sorting ×1