List chained within Chained List

2

I'm having trouble with linked lists within a linked list. The program is a card game in which there is a doubly chained list of players and within each node of that list, a list (the hand) of cards. I can not add a letter to the list of letters. How to correctly access the player's node and how to access the first position of the list within this node?

My structs:

typedef struct Carta{
    int valor;
    char naipe;
}Carta;

typedef struct Cartas{
    Carta Item;
    struct Cartas *Prox_carta;
    struct Cartas *Primeiro, *Ultimo;
    int Tamanho_Mao;
}Cartas;

typedef struct Jogador {
    int id_jogador;
    Cartas *Primeira_mao;
    struct Jogador *Prox, *Ant;
    struct Jogador *Prim_Jogador, *Ult_Jogador;
    int Tamanho;
}Jogador;

The function to try to add cards:

int Adiciona_Carta(Jogador *jogadores, Cartas *mao, int x, Carta carta){

    Jogador *aux;
    Cartas *novaCarta, *aux2;
    novaCarta = (Cartas*)malloc(sizeof(Cartas));
    novaCarta->Prox_carta = NULL;
    novaCarta->Item.naipe = carta.naipe;
    novaCarta->Item.valor = carta.valor;
    aux = jogadores->Prim_Jogador;

    while (jogadores->id_jogador != x){
        aux = aux->Prox;
    }
    aux2 = aux->Primeira_mao;
    mao->Primeiro = aux->Primeira_mao;
    if (aux->Primeira_mao == NULL){
        Cria_Mao_Carta(mao);
        mao->Primeiro = novaCarta;
        mao->Tamanho_Mao++;
    } 
    if (carta.valor > aux2->Item.valor){
        novaCarta->Prox_carta = mao->Primeiro;
        mao->Primeiro = novaCarta;
        mao->Tamanho_Mao++;
    }
    for (aux2 ;; aux2 = aux2->Prox_carta){
        if(carta.valor > aux2->Prox_carta->Item.valor){
            novaCarta->Prox_carta = aux2->Prox_carta;
            aux2->Prox_carta = novaCarta;
            mao->Tamanho_Mao++;
        }
        if(aux2->Prox_carta == NULL){
            aux2->Prox_carta = novaCarta;
            novaCarta->Prox_carta = NULL;
        }
    }
}
    
asked by anonymous 23.10.2016 / 21:59

1 answer

1

I recommend, first and foremost, to create a separate list structure.

typedef struct Conteudo{
int valor;
char naipe;
}Conteudo; 

typedef struct Cartas{
    Conteudo Item;
    struct Cartas *Prox_carta;
}Carta;

typedef struct ListaCartas {
    carta *primeiro, *ultimo;
    int tamanho;
}

Does the same scheme with the players, it is much clearer than what you are doing. your structure was quite confusing this way.

And for God's sake, do not use aux for anything, give a variable name that someone else reading your code will understand = /

You can not understand your function code without knowing where the game type help was, and you did not specify that the function create_mao_carta does

    
28.10.2016 / 03:58