gpt4 book ai didi

c - 如何使用用户输入对链接列表进行排序?

转载 作者:行者123 更新时间:2023-12-04 10:47:23 27 4
gpt4 key购买 nike

我有一个项目,它生成一个链接列表,删除它们并在用户处显示它。现在,我想对列表进行排序。
我的结构:

typedef struct YugiohCard {
char Name[100];
char CardType[20];
int Level;
int Rank;
int PendulumStage;
int Link;
int ATK;
int DEF;
char Property[20];
char MonsterType[40];
char CardType2[30];
char Description[500];
struct YugiohCard* pNext;
struct YugiohCard* pPrev;
} struYugiohCard;

当用户说:“CardType2 Ascending”时,程序按 CardType2 和 Ascending 对列表进行排序。

在这种情况下。也可以按其他结构内容(Monstertyp、ATK、DEF 等)进行排序。升序或降序。

如果没有来自 C++ 的东西,我怎么能做到呢?

对不起,我的英语不好。我不太擅长这个。

编辑:
这是我的完整代码:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"

typedef struct YugiohCard {
char Name[100];
char CardType[20];
int Level;
int Rank;
int PendulumStage;
int Link;
int ATK;
int DEF;
char Property[20];
char MonsterType[40];
char CardType2[30];
char Description[500];
struct YugiohCard* pNext;
struct YugiohCard* pPrev;
} struYugiohCard;

bool OutputList(struYugiohCard* pStart)
{
int count = 0;
struYugiohCard* current = pStart; // Initialize current
while (current != NULL)
{
count++;
current = current->pNext;
}

char answer[265];
int CountetCardsThatWillBeOutputet;
printf("How many Yugioh cards would you like to spend? 0 means all,
otherwise the number counts. Number of elements in list: %i Input:",
count);
fgets(answer, 265, stdin);
CountetCardsThatWillBeOutputet = atoi(answer);
int countOutputetCards = 0;

if (CountetCardsThatWillBeOutputet > count)
{
printf("Please enter a correct number!");
system("pause");
return false;
}
else if (CountetCardsThatWillBeOutputet == 0)
{
CountetCardsThatWillBeOutputet = count;
}

system("cls");
printf("%10s %20s %10s %10s %20s %10s %10s %10s %20s %20s %20s %20s\n",
"Name", "CardType", "Level", "Rank", "PendulumStage", "Link", "ATK",
"DEF", "Property", "MonsterType", "CardType2", "Description");
for (struYugiohCard* pOut = pStart; pOut != NULL; pOut = pOut->pNext)
{
printf("%10s %20s %10i %10i %20i %10i %10i %10i %20s %20s %20s
%20s\n", pOut->Name, pOut->CardType, pOut->Level, pOut-
>Rank, pOut->PendelumStage, pOut->Link, pOut->ATK, pOut->DEF,
pOut->Property, pOut->MonsterType, pOut->CardType2, pOut-
>Description);
countOutputetCards++;
if (countOutputetCards == CountetCardsThatWillBeOutputet )
{
break;
}
}
system("pause");
}

void DeleteList(struYugiohCard** head_ref)
{
struct YugiohCard* prev = *head_ref;

while (*head_ref)
{
*head_ref = (*head_ref)->pNext;
free(prev);
prev = *head_ref;
}
}

struYugiohCard* CreateList()
{
system("cls");
char answer[265];
int countedCards;
printf("\nHow many Yugioh cards would you like to create? Please enter
only enter numbers, otherwise you'll crash.");
fgets(answer, 265, stdin);
countedCards = atoi(answer);

struYugiohCard* pFirst = NULL;

for (int i = 0; i < countedCards; i++)
{
struYugiohCard* pNew =
(struYugiohCard*)malloc(sizeof(struYugiohCard));
if (pNew == NULL) break;
pNew->Name[0] = 'A' + rand() % 26;
pNew->Name[1] = '\0';
pNew->CardType[0] = 'A' + rand() % 26;
pNew->CardType[1] = '\0';
pNew->Level = 1 + rand() % 12;
pNew->Rank = 1 + rand() % 13;
pNew->PendulumStage = 1 + rand() % 12;
pNew->Link = 1 + rand() % 8;
pNew->ATK = rand() % 10001;
pNew->DEF = rand() % 10001;
pNew->Property[0] = 'A' + rand() % 26;
pNew->Property[1] = '\0';
pNew->MonsterType[0] = 'A' + rand() % 26;
pNew->MonsterType[1] = '\0';
pNew->CardType2[0] = 'A' + rand() % 26;
pNew->CardType2[1] = '\0';
pNew->Description[0] = 'A' + rand() % 26;
pNew->Description[1] = '\0';
if (pFirst != NULL)
{
pNew->pNext = pFirst;
}
else
{
pNew->pNext = NULL;
}
pFirst = pNew;
}
return pFirst;
}

int main()
{
struYugiohCard* pStart = NULL;
printf("\nIMPORTANT: Please maximize the window, otherwise it will not
represents everything correctly.");
do
{
system("cls");
printf("\nDo you want to create a Yugioh card list (YKE) that
Delete Yugioh card list(YKL), a single Yugioh card
delete(EYKL), sort the list(YKS), the Yugioh-
Output card list(YKA) or the program
close(Prsc):");
char answer[265];
fgets(answer, 265, stdin);
if (strcmp(answer, "YKE\n") == 0)
{
pStart = CreateList();
}
else if (strcmp(answer, "YKS\n") == 0)
{
//SortList(pStart);
}
else if (strcmp(answer, "EYKL\n") == 0)
{
//DeleteOneCard(pStart);
}
else if (strcmp(answer, "YKL\n") == 0)
{
DeleteList(&pStart);
}
else if (strcmp(answer, "YKA\n") == 0)
{
OutputList(pStart);
}
else if (strcmp(answer, "Prsc\n") == 0)
{
return 0;
}
else
{
printf("Please enter a shortcut!");
}
} while (true);
}

最佳答案

做一个数组

创建一个指针数组。让数组成员指向列表成员。代码使用了一个小技巧,使列表的最后一个元素得到它的 pNext成员正确设置为 NULL。

struYugiohCard *arr[size_of_list + 1], **arrp = arr, *iter;

for (iter = list; iter != NULL; iter = iter->pNext) {
*arrp++ = iter;
}
*arrp = NULL;

写一个比较函数,然后调用 qsort
有很多如何使用的例子 qsort ,但诀窍是编写一个适当的比较函数。重要的是要意识到 qsort将传入它正在比较的数组元素的地址。由于我们的数组包含指针,比较函数将传递指向指针的指针。

在你的情况下,我猜 CardType2 Ascending可以使用 strcmp 实现, 其返回值与 qsort 匹配期望(如果 a 小于 b 为负,大于则为正,如果相等则为零):
int cmp_CardType2_Ascending(const void *a, const void *b) {
const struYugiohCard * const *aa = a;
const struYugiohCard * const *bb = b;
return strcmp((*aa)->CardType2, (*bb)->CardType2);
}

//...

qsort(arr, size_of_list, sizeof(*arr), cmp_CardType2_Ascending);

修复您的 list
现在,将您的列表重新连接到排序顺序。请注意 pNext最后一次迭代使用设置为 NULL 的额外数组成员。
arr[0]->pNext = arr[1];
arr[0]->pPrev = NULL;
for (int i = 1; i < size_of_list; ++i) {
arr[i]->pNext = arr[i+1];
arr[i]->pPrev = arr[i-1];
}
list = arr[0];

将其放入函数中

下面是一个将大部分逻辑放入单个函数的函数。传入排序函数,然后把这个函数传给 qsort .
void sort_YugiohCard(struYugiohCard **pList, int size_of_list,
int (*By)(const void *, const void *)) {

if (size_of_list == 0) return;

struYugiohCard *list = *pList;
struYugiohCard *arr[size_of_list+1], **arrp = arr, *iter;

for (iter = list; iter != NULL; iter = iter->pNext)
*arrp++ = iter;
*arrp = NULL;

qsort(arr, size_of_list, sizeof(*arr), By);

arr[0]->pNext = arr[1];
arr[0]->pPrev = NULL;
for (int i = 1; i < size_of_list; ++i) {
arr[i]->pNext = arr[i+1];
arr[i]->pPrev = arr[i-1];
}
list = arr[0];

*pList = list;
}

然后,您可以像这样调用此函数:
sort_YugiohCard(&list, 5, cmp_CardType2_Ascending);

并且列表将按照比较函数确定的排序顺序返回。

一个演示

Try it online!

关于c - 如何使用用户输入对链接列表进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59641705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com