C-如何为结构内的链表分配内存

时间:2018-10-23 03:12:23

标签: c

我只是希望在调用newGame时创建一副纸牌。如何将卡值插入链接列表drawPile(位于游戏结构内)?系统要求我实现此ADT,所以请不要添加功能。任何帮助将不胜感激。

typedef enum {RED,BLACK} color;
typedef enum {HEARTS,DIAMONDS,CLUBS,SPADES} suit;
typedef enum {ONE,TWO,THREE,FOUR} value;

typedef struct _card { //struct of cards
    color color;
    suit suit;
    value value;
}card;

typedef struct _drawPile{ //drawPile linked list
    card *card;
    struct _drawPile *next;
}drawPile;

//game struct to store the state of the game.e.g. what cards are in the drawPile.
typedef struct _game{ 
    struct drawPile *game_drawPile;
}*Game;

//creates a deck of cards when called(with specific cards passed in parameters,example of main below)
Game newGame(int deckSize, value values[], color colors[], suit suits[]){

    for(int i = 0;i < deckSize; i++){
        Game nGame = malloc(sizeof(Game));
//this gives me a compile error, 
//how should i allocate memory for drawPile inside my nGame struct?
        nGame->game_drawPile->card->color = colors[i];
        nGame->game_drawPile->card->suit = suits[i];
        nGame->game_drawPile->card->value = values[i];
        nGame->game_drawPile = nGame->game_drawPile->next;
    }
        return nGame;
}

下面是一个示例主要功能,该示例说明newGame函数将如何仅用4张卡运行。

int main (void){

     init_deck();

}
static void init_deck(void){
    int deck_size = 4;

    value values[] = {ONE, TWO, THREE, FOUR};
    color colors[] = {RED, BLACK, RED, BLACK};
    suit suits[] = {HEARTS, DIAMONDS, CLUBS, SPADES};

    Game game = newGame(deck_size, values, colors, suits);
 }

2 个答案:

答案 0 :(得分:1)

您不能在game循环中声明for并在外部引用它:

    Game nGame;
    for(int i = 0;i < deckSize; i++){
        nGame = malloc(sizeof(Game));
        nGame->game_drawPile->card->color = colors[i];
        nGame->game_drawPile->card->suit = suits[i];
        nGame->game_drawPile->card->value = values[i];
        nGame->game_drawPile = nGame->game_drawPile->next;
    }
    return nGame;

另外,由于您使用了typedef,因此必须引用drawPile,而不能使用前面的struct关键字:

typedef struct _game{ 
    drawPile *game_drawPile;
}*Game;

答案 1 :(得分:1)

#include <stdlib.h>
#include <stdio.h>

typedef enum { RED, BLACK } color;
char const * color_names[] = { "RED", "BLACK" };

typedef enum { HEARTS, DIAMONDS, CLUBS, SPADES } suit;
char const * suit_names[] = { "HEARTS", "DIAMONDS", "CLUBS", "SPADES" };

typedef enum { ONE, TWO, THREE, FOUR } value;
char const * value_names[] = { "ONE", "TWO", "THREE", "FOUR" };


typedef struct _card {
    color color;
    suit suit;
    value value;
} card;

typedef struct _drawPile {
    card *card;
    struct _drawPile *next;
} drawPile;

typedef struct _game {
    int deckSize;
    drawPile *game_drawPile;
} *Game;

Game newGame(int deckSize, value values[], color colors[], suit suits[])
{
    Game nGame = malloc(sizeof(*nGame));
    nGame->game_drawPile = NULL;
    nGame->deckSize = deckSize;
    suit s = 0;
    color c = 0;
    value v = 0;
    int const num_cards = deckSize * deckSize * deckSize;
    int const cards_per_suit = deckSize * deckSize;
    drawPile *current = NULL;
    for (int i = 0; i < num_cards; i++)
    {
        if (!nGame->game_drawPile) {
            nGame->game_drawPile = malloc(sizeof(drawPile));
            nGame->game_drawPile->next = NULL;
            nGame->game_drawPile->card = malloc(sizeof(card));
            nGame->game_drawPile->card->suit = s;
            nGame->game_drawPile->card->color = c;
            nGame->game_drawPile->card->value = v;
            current = nGame->game_drawPile;
        }
        else {
            current->next = malloc(sizeof(drawPile));
            current = current->next;
            current->next = NULL;
            current->card = malloc(sizeof(card));
            current->card->suit = s;
            current->card->color = c;
            current->card->value = v;
        }

        if ((i + 1) % cards_per_suit == 0) ++s;
        if (s % cards_per_suit == 0) s = 0;

        if ((i + 1) % deckSize == 0) ++c;
        if (c % deckSize == 0) c = 0;

        ++v;
        if (v % deckSize == 0) v = 0;
    }
    return nGame;
}

void printCard(card *c)
{
    printf("%s %s %s\n", suit_names[c->suit], color_names[c->color], value_names[c->value]);
}

void printGame(Game game)
{
    for (drawPile *current = game->game_drawPile; current; current = current->next)
        printCard(current->card);
}

void destroyGame(Game game)
{
    for (drawPile *current = game->game_drawPile, *temp; current; current = temp) {
        temp = current->next;
        free(current->card);
        free(current);
        current = temp;
    }
    free(game);
}

void init_deck(void)
{
    int deck_size = 4;
    value values[] = { ONE, TWO, THREE, FOUR };
    color colors[] = { RED, BLACK, RED, BLACK };
    suit  suits [] = { HEARTS, DIAMONDS, CLUBS, SPADES };

    Game game = newGame(deck_size, values, colors, suits);
    printGame(game);
    destroyGame(game);
}

int main(void)
{
    init_deck();
}

输出:

HEARTS RED ONE
HEARTS RED TWO
HEARTS RED THREE
HEARTS RED FOUR
HEARTS BLACK ONE
HEARTS BLACK TWO
HEARTS BLACK THREE
HEARTS BLACK FOUR
HEARTS HEARTS ONE
HEARTS HEARTS TWO
HEARTS HEARTS THREE
HEARTS HEARTS FOUR
HEARTS DIAMONDS ONE
HEARTS DIAMONDS TWO
HEARTS DIAMONDS THREE
HEARTS DIAMONDS FOUR
DIAMONDS RED ONE
DIAMONDS RED TWO
DIAMONDS RED THREE
DIAMONDS RED FOUR
DIAMONDS BLACK ONE
DIAMONDS BLACK TWO
DIAMONDS BLACK THREE
DIAMONDS BLACK FOUR
DIAMONDS HEARTS ONE
DIAMONDS HEARTS TWO
DIAMONDS HEARTS THREE
DIAMONDS HEARTS FOUR
DIAMONDS DIAMONDS ONE
DIAMONDS DIAMONDS TWO
DIAMONDS DIAMONDS THREE
DIAMONDS DIAMONDS FOUR
CLUBS RED ONE
CLUBS RED TWO
CLUBS RED THREE
CLUBS RED FOUR
CLUBS BLACK ONE
CLUBS BLACK TWO
CLUBS BLACK THREE
CLUBS BLACK FOUR
CLUBS HEARTS ONE
CLUBS HEARTS TWO
CLUBS HEARTS THREE
CLUBS HEARTS FOUR
CLUBS DIAMONDS ONE
CLUBS DIAMONDS TWO
CLUBS DIAMONDS THREE
CLUBS DIAMONDS FOUR
SPADES RED ONE
SPADES RED TWO
SPADES RED THREE
SPADES RED FOUR
SPADES BLACK ONE
SPADES BLACK TWO
SPADES BLACK THREE
SPADES BLACK FOUR
SPADES HEARTS ONE
SPADES HEARTS TWO
SPADES HEARTS THREE
SPADES HEARTS FOUR
SPADES DIAMONDS ONE
SPADES DIAMONDS TWO
SPADES DIAMONDS THREE
SPADES DIAMONDS FOUR
  

在我的示例中,主卡只有4张。 (一颗红心,两颗黑钻石等)

然后通过valuescolorssuits毫无价值。

Game newGame(int deckSize)
{
    Game nGame = malloc(sizeof(*nGame));
    nGame->game_drawPile = NULL;
    nGame->deckSize = deckSize;
    drawPile *current = NULL;
    for (int i = 0; i < deckSize; i++)
    {
        if (!nGame->game_drawPile) {
            nGame->game_drawPile = malloc(sizeof(drawPile));
            nGame->game_drawPile->next = NULL;
            nGame->game_drawPile->card = malloc(sizeof(card));
            nGame->game_drawPile->card->suit  = suits[i];
            nGame->game_drawPile->card->color = colors[i];
            nGame->game_drawPile->card->value = values[i];
            current = nGame->game_drawPile;
        }
        else {
            current->next = malloc(sizeof(drawPile));
            current = current->next;
            current->next = NULL;
            current->card = malloc(sizeof(card));
            current->card->suit  = suits[i];
            current->card->color = colors[i];
            current->card->value = values[i];
        }
    }
    return nGame;
}