如何在不使用 c 中的数组的情况下洗牌一副结构卡

How to shuffle a deck of struct cards without using an array in c

我有这个代码。我在函数 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;
};

@user3386109 的评论员提出了他的建议。

为了帮助您入门,这里有一小段代码可以完成您需要做的事情。

void shuffleDeck(cards *deck)
{
    cards *shuffled = NULL;
    cards *end = NULL;
    int numberOfCards = countDeck(deck); //Count number of cards in your deck
    srand(time(NULL));  //Seeds the random number generator with the current 
    while (numberOfCards > 0)
    {
        int index = rand()%numberOfCards;
        cards *previousCard = NULL;
        cards *selectedCard;
        //Iterate over linked list until you arrive at index 'x'.
        if (!shuffled)
            end = shuffled = selectedCard;
        else
            end->next = selectedCard;

        if (previousCard)
            previousCard->next = selectedCard->next;
        end->next = NULL;
        --numberOfCards;
    }
}

请注意,此代码是故意不完整的,只是为了让您了解如何操作。

编辑:我真的不应该这样做,但这个 shuffle 代码对我有用。

cards *shuffleDeck(cards *deck)
{
    cards *shuffled = NULL;
    cards *end = NULL;
    int numberOfCards = countDeck(deck); //Count number of cards in your deck
    srand(time(NULL));  //Seeds the random number generator with the current
    while (numberOfCards > 0)
    {
        int index = rand()%numberOfCards;
        cards *previousCard = NULL;
        cards *selectedCard = deck;
    
        //Iterate over linked list until you arrive at index 'x'.
        for (int i = 0; i < index && selectedCard; ++i)
        {
            previousCard = selectedCard;
            selectedCard = selectedCard->pNext;
        }
    
        if (!shuffled)
            shuffled = selectedCard;
        else if (end)
            end->pNext = selectedCard;
        end = selectedCard;
    
        if (selectedCard)
        {
            if (previousCard)
                previousCard->pNext = selectedCard->pNext;  //Closes the gap between the previous element and the next element.
            else
                deck = selectedCard->pNext;                 //Resets the anchor element.
            selectedCard->pNext = NULL;
        }
        --numberOfCards;
    }
    
    return shuffled;
}

int main()
{
    MyDeckOutput(shuffleDeck(createDeck()));
    return 0;
};

填空题就交给你了(怎么算牌数)

另请注意,您的 'creating a card' 有问题。 在您的作业周围使用大括号 ({}),或者更好的是,使用专用函数。

或者,我建议阅读有关 'switch/case' 语句的内容。