我正在尝试按字母顺序对此链接列表中的名称进行排序,但我不确定哪种方法是正确的。我创建了一个方法来比较列表中的名称,并每次更新我的当前指针。我一直在收到错误。任何人都可以建议一个更好的方式来排序名称?我是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;
}
答案 0 :(得分:2)
你的方法过于复杂:比较函数也执行某种排序和插入功能。比较函数应返回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;
}
下面的编辑:是一个带有简单指针的备用版本。您可以看到我需要在空白列表和开头插入时进行特殊情况。
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;
}
}