【发布时间】:2022-01-15 01:10:24
【问题描述】:
我有这个代码。我在函数 createDeck 中创建了 10 张卡片,我想在函数 MyDeckOutput 中洗牌而不使用数组。有人可以帮忙吗??
我不知道,我的老师想要这样。我不允许使用 c++ 或外部的东西。 :/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctime>
typedef struct Card
{
char name[50];
int maxAlter;
float maxGewicht;
double maxLaenge;
struct Card* pNext;
} cards;
cards* createDeck()
{
cards* pStart = NULL;
cards* pLast = NULL;
cards* pNew = (cards*)malloc(sizeof(cards));
for (int iElm = 0; iElm < 10; iElm++) {
cards* pNew = (cards*)malloc(sizeof(cards));
if (iElm == 0) { strcpy_s(pNew->name, "Ameisenbaer"); pNew->maxAlter = 14; pNew->maxGewicht = 39; pNew->maxLaenge = 0.90; pNew->pNext = NULL; }
if (iElm == 1) { strcpy_s(pNew->name, "Biber"); pNew->maxAlter = 21; pNew->maxGewicht = 30; pNew->maxLaenge = 1.02; pNew->pNext = NULL; }
if (iElm == 2) { strcpy_s(pNew->name, "Brauenbaer"); pNew->maxAlter = 30; pNew->maxGewicht = 600; pNew->maxLaenge = 1.50; pNew->pNext = NULL; }
if (iElm == 3) { strcpy_s(pNew->name, "Delfin"); pNew->maxAlter = 45; pNew->maxGewicht = 150; pNew->maxLaenge = 7.00; pNew->pNext = NULL; }
if (iElm == 4) { strcpy_s(pNew->name, "Elefant"); pNew->maxAlter = 70; pNew->maxGewicht = 6000; pNew->maxLaenge = 3.00; pNew->pNext = NULL; }
if (iElm == 5) { strcpy_s(pNew->name, "Esel"); pNew->maxAlter = 14; pNew->maxGewicht = 39; pNew->maxLaenge = 0.90; pNew->pNext = NULL; }
if (iElm == 6) { strcpy_s(pNew->name, "Federmaus"); pNew->maxAlter = 21; pNew->maxGewicht = 30; pNew->maxLaenge = 1.02; pNew->pNext = NULL; }
if (iElm == 7) { strcpy_s(pNew->name, "Fuchs"); pNew->maxAlter = 30; pNew->maxGewicht = 600; pNew->maxLaenge = 1.50; pNew->pNext = NULL; }
if (iElm == 8) { strcpy_s(pNew->name, "Gorilla"); pNew->maxAlter = 45; pNew->maxGewicht = 150; pNew->maxLaenge = 7.00; pNew->pNext = NULL; }
if (iElm == 9) { strcpy_s(pNew->name, "Giraffe"); pNew->maxAlter = 70; pNew->maxGewicht = 6000; pNew->maxLaenge = 3.20; pNew->pNext = NULL; }
pNew->pNext = NULL;
if (pStart == NULL) pStart = pNew;
if (pLast != NULL) pLast->pNext = pNew;
pLast = pNew;
}
return pStart;
}
/*void MyDeckOutput(cards* pStart)
{
int iEle = 0;
for (cards* pOut = pStart; pOut != NULL; pOut = pOut->pNext)
{
iEle++;
if (iEle < 6) printf("name = %s\n", pOut->name);
}
}*/
void MyDeckOutput(cards* pStart)
{
for (cards* pOut = pStart; pOut != NULL; pOut = pOut->pNext) printf("name = %s\n", pOut->name);
}
void shuffleDeck(cards* pStart)
{
cards* shuffled = NULL;
cards* end = NULL;
int numberOfCards = 10; // cards number
srand(time(NULL)); // seeds the random nr generator with the current
while (numberOfCards > 0)
{
int index = rand() % numberOfCards;
cards* previousCard = NULL;
cards* selectedCard = pStart->pNext;
// iterate over linked list
if (!shuffled)
end = shuffled = selectedCard;
else
end->pNext = selectedCard;
if (previousCard)
previousCard->pNext = selectedCard->pNext;
end->pNext = NULL;
--numberOfCards;
printf("name = %s%i\n", selectedCard->name, index);
}
}
int main()
{
cards* pStart = createDeck();
MyDeckOutput(pStart);
printf("\n\nShuffel:\n");
shuffleDeck(pStart);
system("pause");
return 0;
};
【问题讨论】:
-
您的老师希望您了解链表。随机选择一张卡片。将其从原始链表中删除。将其添加到混洗链表中。重复直到原始链表为空。在
main你应该有cards* pShuffled = shuffleDeck(pStart); -
循环中每个
if后面都缺少{ },因此if只会影响strcpy_s而不会影响每一行的其余部分。 (您似乎在稍后的MyDeckOutput中使用了与for循环类似的语法——它之所以有效,是因为循环主体中只有一个语句。请使用{}和缩进而不是这个。) -
注释'迭代链表'不应该意味着'这就是这条线所做的',而是应该提示你在这个位置插入它。你知道,以支持学习过程。
标签: c function pointers struct typedef