HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Module for making a doubly linked list

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
moduledoublyforlistmakinglinked

Problem

This module makes a doubly linked list and initializes each member. I couldn't do it with a loop, so I gave up and made each node individually.

How can I improve it? Can somebody help me make a loop to chain all these lists together? This is from a Checkers game I'm making.

```
#include
#include
#include
#include
#include
#include "LISTA.h"
#include "PEC.h"

#define TABULEIRO_OWN
#include "TABULEIRO.h"
#undef TABULEIRO_OWN

typedef struct TAB_tagInfoCasa
{
void * pValor1;
void * pValor2;
}TAB_tpInfoCasa ;

typedef struct TAB_tagTabuleiro
{
LIS_tppLista pElem ;
/ elemento de uma lista de listas, ou seja, uma lista /

struct TAB_tagTabuleiro *pProx ;
/ Ponteiro para o elemento sucessor /

struct TAB_tagTabuleiro * pAnt ;
/ Ponteiro para o elemento predecessor /

}TAB_tpTabuleiro ;

/ Dados encapsulados no módulo /

static TAB_tpTabuleiro * pTabuleiro = NULL ;
/ Ponteiro para a cabeça do tabuleiro /

/ Protótipos das funções encapsuladas no módulo /

static void EsvaziarTabuleiro( ) ;

static LIS_tpCondRet InserirInfoCasa( LIS_tppLista pLista, PEC_tpPEC pPEC, int PosCasa ) ;

/ Código das funções exportadas pelo módulo /

/*
* Função TAB Criar tabuleiro
*/

TAB_tpCondRet TAB_CriarTabuleiro( )
{

LIS_tpCondRet CondRet ;
int PosCasa ;
int CasaBranca = -1 ;

/ Declarações dos nós do tabuleiro */
TAB_tpTabuleiro * pTab ;
TAB_tpTabuleiro * pTab2 ;
TAB_tpTabuleiro * pTab3 ;
TAB_tpTabuleiro * pTab4 ;
TAB_tpTabuleiro * pTab5 ;
TAB_tpTabuleiro * pTab6 ;
TAB_tpTabuleiro * pTab7 ;
TAB_tpTabuleiro * pTab8 ;

/ De

Solution

Well, some parts can be converted to loops pretty easily. Just for the most obvious example, your series of code like:

pListaA = LIS_CriarLista( PEC_DestruirPEC ) ;
    if ( pListaA == NULL )
    {
        return TAB_CondRetFaltouMemoria ;
    } /* if */


(repeated for pListaB...pListaH), can be converted to a loop quite easily:

#define num_tabs 8

TAB_tpTabuleiro *Tabs[num_tabs];

for (i=0; i<num_tabs; i++) {
    Tabs[i] = LIS_CriarLista( PEC_DestruirPEC ) ;
    if (Tabs[i] == NULL)
        return TAB_CondRetFaltouMemoria ;
}


Some of the other parts look just irregular enough that the conversion is likely to be a bit more complex. While I could certainly write some code that would produce the same result, I'm pretty sure I'm not following enough of what all this is really supposed to accomplish. I can puzzle out just enough of the meaning of some of the names to convince me that I really don't understand the code as well as I should to produce something that's as clean as possible. I could undoubtedly figure out what it means even without names that are meaningful to me, but having done that many times before, I'm pretty sure it would take more time than I'm willing to spend on it at the moment.

Code Snippets

pListaA = LIS_CriarLista( PEC_DestruirPEC ) ;
    if ( pListaA == NULL )
    {
        return TAB_CondRetFaltouMemoria ;
    } /* if */
#define num_tabs 8

TAB_tpTabuleiro *Tabs[num_tabs];

for (i=0; i<num_tabs; i++) {
    Tabs[i] = LIS_CriarLista( PEC_DestruirPEC ) ;
    if (Tabs[i] == NULL)
        return TAB_CondRetFaltouMemoria ;
}

Context

StackExchange Code Review Q#5088, answer score: 2

Revisions (0)

No revisions yet.