C 如何使用用户输入对链表进行排序?
我有一个项目,它生成一个链表,删除它们,并将其显示给用户。现在,我想对列表进行排序。 我的结构:C 如何使用用户输入对链表进行排序?,c,linked-list,C,Linked List,我有一个项目,它生成一个链表,删除它们,并将其显示给用户。现在,我想对列表进行排序。 我的结构: 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 Monste
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升序”时,程序将按CardType2和升序对列表进行排序
在这种情况下,是用血法。也可以按其他结构内容(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);
}
#包括“stdio.h”
#包括“stdlib.h”
#包括“string.h”
#包括“time.h”
类型定义结构YugiohCard{
字符名[100];
char-CardType[20];
智力水平;
整数秩;
内摆台;
int链接;
int ATK;
int-DEF;
字符属性[20];
字符类型[40];
charcardtype2[30];
字符描述[500];
结构YugiohCard*pNext;
结构YugiohCard*pPrev;
}struYugiohCard;
布尔输出列表(struYugiohCard*pStart)
{
整数计数=0;
struYugiohCard*current=pStart;//初始化当前
while(当前!=NULL)
{
计数++;
当前=当前->pNext;
}
答案[265];
将被输出的int countetCards;
printf(“你想花多少张Yugioh卡?0表示全部,
否则,数量将计数。列表中的元素数量:%i输入:“,
计数);
fgets(答案265,标准DIN);
CounteTCardsThat将被输出=atoi(答案);
int countOutputetCards=0;
如果(countetcardshattwillbeoutput>count)
{
printf(“请输入正确的数字!”);
系统(“暂停”);
返回false;
}
else if(countetCardshattwillbeoutput==0)
{
countetcardshattwillbeoutputet=计数;
}
系统(“cls”);
printf(“%10s%20s%10s%10s%20s%10s%10s%10s%20s%20s%20s%20s%20s%20s\n”,
“姓名”、“卡片类型”、“等级”、“等级”、“摆台”、“链接”、“ATK”,
“DEF”、“Property”、“MonsterType”、“CardType2”、“Description”);
用于(struYugiohCard*pOut=pStart;pOut!=NULL;pOut=pOut->pNext)
{
printf(“%10s%20s%10i%10i%20i%10i%10i%10i%20s%20s
%20秒\n“,撅嘴->姓名,撅嘴->卡片类型,撅嘴->级别,撅嘴-
>等级,撅嘴->彭德隆舞台,撅嘴->链接,撅嘴->ATK,撅嘴->定义,
撅嘴->属性,撅嘴->怪物类型,撅嘴->卡片类型2,撅嘴-
>说明);
CountOutputCards++;
如果(CountOutputCards==CounteTCardsThat将被输出)
{
打破
}
}
系统(“暂停”);
}
无效删除列表(struYugiohCard**标题参考)
{
结构YugiohCard*prev=*头部参考;
while(*head\u ref)
{
*head\u ref=(*head\u ref)->pNext;
免费(上);
上一页=*标题参考;
}
}
struYugiohCard*CreateList()
{
系统(“cls”);
答案[265];
int计数卡;
printf(“\n要创建多少张Yugioh卡?请输入
只输入数字,否则会崩溃。“);
fgets(答案265,标准DIN);
countedCards=atoi(答案);
struYugiohCard*pFirst=NULL;
对于(int i=0;iName[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->属性[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';
如果(pFirst!=NULL)
{
pNew->pNext=pFirst;
}
其他的
{
pNew->pNext=NULL;
}
pFirst=pNew;
}
返回pFirst;
}
int main()
{
struYugiohCard*pStart=NULL;
printf(“\n重要:请最大化窗口,否则它将不会
正确地代表一切。”);
做
{
系统(“cls”);
printf(“\n是否要创建一个Yugioh卡列表(YKE)以
删除Yugioh卡列表(YKL),一张Yugioh卡
删除(EYKL),对列表进行排序(YKS),Yugioh-
输出卡列表(YKA)或程序
关闭(Prsc):“”;
答案[265];
fgets(答案265,标准DIN);
if(strcmp(回答“YKE\n”)==0)
{
pStart=CreateList();
}
else if(strcmp(回答“YKS\n”)==0)
{
//SortList(pStart);
}
else if(strcmp(回答“EYKL\n”)==0)
{
//DeleteOneCard(pStart);
}
否则如果(strcmp(回答“YKL\n”)==0)
{
删除列表(&pStart);
}
else if(strcmp(回答“YKA\n”)==0)
{
输出列表(pStart);
}
否则如果(strcmp(回答“Prsc\n”)==0)
{
返回0;
}
其他的
{
printf(“请输入快捷方式!”);
}
}虽然(正确);
}
制作一个数组
创建一个指针数组。让数组成员指向列表成员。代码使用了一个小技巧,以便列表的最后一个元素将其
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
,有很多例子,但诀窍在于编写一个适当的比较函数。重要的是要认识到这一点
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);
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];
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);