I have a project, that generates a Linked List, delete them and show this at the User. Now, I want to sort the list. My struct:
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;
When the User says: "CardType2 Ascending" then the program sort the list by CardType2 and Ascending.
In this case alphematicaly. It is also possible to sort by the other struct contents(Monstertyp, ATK, DEF, etc.). Ascending or Descending.
How can I do it without things from C++?
Sorry for my bad English. I'm not very well at this.
Edit: Here is my complete Code:
#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);
}