/* Obter tamanho da lista */ LIS_tpCondRet LIS_ObterTamanho( LIS_tppLista pLista,int * num) { #ifdef _DEBUG CNT_CONTAR( "ObterTamanho" ) ; #endif *num = pLista->numElem; return LIS_CondRetOK ; } /* Fim função: LIS &Obter referência para o tamanho da lista*/
void AssertNode(Node *n, Graph *g) { CNT_CONTAR("AssertNode - Verificando assertivas"); IrInicioLista(g->nodes); do{ CNT_CONTAR("AssertNode - Procurando valor"); if (n == LIS_ObterValor(LIS_ObterValor( g->nodes ))){ CNT_CONTAR("AssertNode - Valor encontrado"); assert( n->delData == g->delData ); return; } } while(LIS_CondRetOK == LIS_AvancarElementoCorrente ( g->nodes , 1)); /* Nao deveria cruzar esse ponto: Significa que nao foi encontrado * n na lista g->nodes */ assert( 0 ); }
enum GRA_Ret searchData(LIS_tppLista l, void *data) { Node* n; CNT_CONTAR("searchData - Inicializao"); IrInicioLista(l); do{ CNT_CONTAR("searchData - Procurando dado"); n = LIS_ObterValor(LIS_ObterValor(l)); if(data == n->data){ CNT_CONTAR("searchData - Achou o dado"); return GRA_Ok; } } while(LIS_CondRetOK == LIS_AvancarElementoCorrente (l, 1)); CNT_CONTAR("searchData - Finalizacao"); return GRA_InvalidArgNode; }
void delLink (Link *l) { static int RecursionCnt = 0; RecursionCnt++; IrInicioLista( l->n2->links ); CNT_CONTAR("delLink - Inicio"); if ( RecursionCnt <= 1 && LIS_CondRetOK == LIS_ProcurarValor(l->n2->links,l->brother) ){ CNT_CONTAR("delLink - Achou: Deletando Lista de Links"); LIS_ExcluirElemento(l->n2->links); } CNT_CONTAR("delLink - Finalizando"); free (l); RecursionCnt--; }
void *GRA_NodesGetNext (Graph *g) { void *ret; LIS_tppLista l; CNT_CONTAR("GRA_NodesGetNext - inicio"); #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ if (!g->currentNode){ return NULL; } CNT_CONTAR("GRA_NodesGetNext - guardando g->nodes"); l = g->nodes; if (!l){ CNT_CONTAR("GRA_NodesGetNext - Nao existe g->nodes"); return NULL; } CNT_CONTAR("GRA_NodesGetNext - Obtendo dado"); ret = ((Node*)LIS_ObterValor (LIS_ObterValor(l)))->data; if (g->nodesOld == ret){ /* Se esse dado ja' repitiu, ja terminou a lista */ CNT_CONTAR("GRA_NodesGetNext - Dado repetido, fim da lista"); return NULL; } CNT_CONTAR("GRA_NodesGetNext - Avanando elemento corrente"); g->nodesOld = ret; LIS_AvancarElementoCorrente (l, 1); #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ CNT_CONTAR("GRA_NodesGetNext - Finalizando"); return ret; }
void AssertGraph(Graph *g) { int i; CNT_CONTAR("AssertGraph - Verificando assertivas"); assert( g->nOfNodes == 0 || g->currentNode !=NULL ); assert( g->nOfLinks % 2 == 0 ); IrInicioLista(g->nodes); for ( i=1; LIS_CondRetOK == LIS_AvancarElementoCorrente ( g->nodes , 1); i++ ){ CNT_CONTAR("AssertGraph - Avanando elemento corrente"); } CNT_CONTAR("AssertGraph - Verificando assertivas, finalizando"); assert( i == g->nOfNodes || g->nOfNodes == 0 ); }
void delNode (Graph *g, void *n_) { Node * n=n_; CNT_CONTAR("delNode - Destruindo lista de links"); LIS_DestruirLista( n->links ); if ( g->delData ){ CNT_CONTAR("delNode - Existe g->delData"); ( *g->delData )( n->data ); } #ifdef _DEBUG IrFinalLista( g->currentNode ); free(LIS_ObterValor( g->currentNode )); IrInicioLista( g->currentNode ); #endif /* _DEBUG */ CNT_CONTAR("delNode - Finalizando"); free (n); }
LIS_tpCondRet LIS_VerificarLista( LIS_tppLista lista ) { tpElemLista * pElem; if ( LIS_VerificarCabeca( lista ) != LIS_CondRetOK ) { CNT_CONTAR( "LIS_VerificarLista_Cabeca" ) ; return LIS_CondRetErroEstrutura; } /* if */ pElem = lista->pElemCorr; //pElem = lista->pOrigemLista ; /* Assegura que cada par de elementos adjacentes em uma lista sejam adjacentes somente entre si */ while ( pElem != NULL ) { if ( ElemLiberado( pElem ) ) { CNT_CONTAR( "LIS_VerificarLista_ElemFree" ) ; return LIS_CondRetErroEstrutura ; } /* if */ if ( pElem->pProx != NULL ) { if ( pElem->pProx->pAnt != pElem ) { CNT_CONTAR( "LIS_VerificarLista_ProxAnt" ) ; return LIS_CondRetErroEstrutura ; } /* if */ CNT_CONTAR( "LIS_VerificarLista_Prox" ) ; } /* if */ if ( pElem->pAnt != NULL ) { if ( pElem->pAnt->pProx != pElem ) { CNT_CONTAR( "LIS_VerificarLista_AntProx" ) ; return LIS_CondRetErroEstrutura ; } /* if */ CNT_CONTAR( "LIS_VerificarLista_Ant" ) ; } /* if */ CNT_CONTAR( "LIS_VerificarLista_While" ) ; pElem = pElem->pProx ; } /* while */ CNT_CONTAR( "LIS_VerificarLista" ) ; return LIS_CondRetOK ; } /* Fim função: LIS &Verificar uma lista */
int LST_VerificaAssertivaListaVazia ( TpLista *pLista ) { if ( LST_RetornaNumElementos (pLista) == 0 ) { CNT_CONTAR("LST_AssertivaListaVazia_if1"); if ( pLista -> pNoCorrente != NULL || pLista -> pOrigemLista != NULL || pLista -> pFimLista != NULL ) { CNT_CONTAR("LST_AssertivaListaVazia_Erro"); return 1; } else { CNT_CONTAR("LST_AssertivaListaVazia_Ok"); } /* if */ return 0; } else { CNT_CONTAR("LST_AssertivaListaVazia_else1"); } return 0; }/* Fim função: LST Verifica Assertiva Lista Vazia */
LIS_tpCondRet LIS_ProcurarValor( LIS_tppLista lista , void * pValor ) { tpElemLista * pElem ; #ifdef _DEBUG assert( lista != NULL ) ; #endif if ( lista->pElemCorr == NULL ) { #ifdef _DEBUG CNT_CONTAR( "LIS_ProcurarValor_Vazia" ) ; #endif return LIS_CondRetListaVazia ; } /* if */ for ( pElem = lista->pElemCorr ; pElem != NULL ; pElem = pElem->pProx ) { if ( pElem->pValor == pValor ) { #ifdef _DEBUG CNT_CONTAR( "LIS_ProcurarValor_ForAchou" ) ; #endif lista->pElemCorr = pElem ; return LIS_CondRetOK ; } /* if */ #ifdef _DEBUG CNT_CONTAR( "LIS_ProcurarValor_For" ) ; #endif } /* for */ #ifdef _DEBUG CNT_CONTAR( "LIS_ProcurarValor" ) ; #endif return LIS_CondRetNaoAchou ; } /* Fim função: LIS &Procurar elemento contendo valor */
int LST_VerificaAssertivaListaPreenchida ( TpLista *pLista ) { if ( LST_RetornaNumElementos (pLista) > 1 ) { CNT_CONTAR("LST_AssertivaListaPreenchida_if1"); if ( pLista -> pNoCorrente == NULL || pLista -> pFimLista == NULL || pLista -> pOrigemLista == NULL ) { printf("\nLista Preenchida: erro pNoCorrente ou pFimLista ou pOrigemLista nulo(s)\n"); CNT_CONTAR("LST_AssertivaListaPreenchida_if2"); return 1; } else { CNT_CONTAR("LST_AssertivaListaPreenchida_else2"); }/* if */ return 0; } else { CNT_CONTAR("LST_AssertivaListaPreenchida_else1"); } return 0; }/* Fim função: LST Verifica Assertiva Lista Preenchida */
int LST_VerificaAssertivaTipoListaElementos ( TpLista *pLista ) { TpNoLista* aux = pLista->pOrigemLista; while(aux != NULL) { CNT_CONTAR("LST_AssertivaTipoElemento_while"); if(aux->pValor == NULL) { CNT_CONTAR("LST_AssertivaTipoElemento_Null_ComErro"); printf("\nErro: Ponteiro para conteudo da celula nulo!\n"); return 1; } else { CNT_CONTAR("LST_AssertivaTipoElemento_NotNull_Ok"); } if(aux->tipoElemento != pLista->tipoElemento) { CNT_CONTAR("LST_AssertivaTipoElemento_ComErro"); printf("\nErro: Tipo do conteudo da celula diferente do registrado na cabeca da lista!\n"); return 1; } else { CNT_CONTAR("LST_AssertivaTipoElemento_Ok"); } aux = aux->pProx; } CNT_CONTAR("LST_AssertivaTipoElemento_Fim"); return 0; }
Graph *GRA_New (FDelData fdd) { Graph *g; CNT_CONTAR("GRA_New - Inicializao"); g = (Graph *) malloc(sizeof(Graph)); if (!g){ CNT_CONTAR("GRA_New - Nao alocou g"); return NULL; } g->nodes = LIS_CriarLista((FDelData) LIS_DestruirLista ); CNT_CONTAR("GRA_New - Alocando nodes"); if (!g->nodes){ CNT_CONTAR("GRA_New - Nao alocou nodes"); free (g); return NULL; } g->delData = fdd; g->currentNode = NULL; CNT_CONTAR("GRA_New - Atribuies, finalizao"); #ifdef _DEBUG g->nOfNodes = 0; g->nOfLinks = 0; CNT_CONTAR("GRA_New - Chamando AssertGraph, finalizando"); AssertGraph(g); #endif /* _DEBUG */ return g; }
int LST_VerificaAssertivaListaElemUnico ( TpLista *pLista ) { if ( LST_RetornaNumElementos (pLista) == 1 ) { CNT_CONTAR("LST_AssertivaListaElemUnico_if1"); if ( pLista -> pNoCorrente != pLista -> pOrigemLista || pLista -> pNoCorrente != pLista -> pFimLista ) { printf("\n Lista com Um elemento e ponteiro corrente diferente do ponteiro origem ou do ponteiro fim\n"); CNT_CONTAR("LST_AssertivaListaElemUnico_if2"); return 1; } else { CNT_CONTAR("LST_AssertivaListaElemUnico_else2"); }/* if */ if ( pLista -> pNoCorrente-> pProx != NULL || pLista -> pNoCorrente-> pAnt != NULL ) { printf("\n Lista com Um elemento e ponteiro proximo/anterior do no corrente nao eh nulo\n"); CNT_CONTAR("LST_AssertivaListaElemUnico_if3"); return 1; } else { CNT_CONTAR("LST_AssertivaListaElemUnico_else3"); }/* if */ return 0; } else { CNT_CONTAR("LST_AssertivaListaElemUnico_else1"); } return 0; }/* Fim função: LST Verifica Assertiva Lista Elemento Unico */
enum GRA_Ret GRA_CCurrent (Graph *g, void *newCurrent) { CNT_CONTAR("GRA_CCurrent - Inicio"); #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ if (!newCurrent){ CNT_CONTAR("GRA_CCurrent - Nao existe novo corrente"); return GRA_InvalidArgNode; } if (GRA_Ok != searchData(g->nodes,newCurrent)){ CNT_CONTAR("GRA_CCurrent - Nao encontrou no grafo novo corrente"); return GRA_InvalidArgNode; } CNT_CONTAR("GRA_CCurrent - Seta novo corrente, finalizcaao"); g->currentNode = LIS_ObterValor(g->nodes); #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ return GRA_Ok; }
LIS_tpCondRet LIS_IrFinalLista( LIS_tppLista lista ) { #ifdef _DEBUG assert( lista != NULL ) ; CNT_CONTAR( "LIS_IrFinalLista" ) ; #endif lista->pElemCorr = lista->pFimLista ; return LIS_CondRetOK; } /* Fim função: LIS &Ir para o elemento final */
void IrFinalLista( LIS_tppLista pLista ) { #ifdef _DEBUG assert( pLista != NULL ) ; #endif #ifdef _DEBUG CNT_CONTAR( "IrFinalLista" ) ; #endif pLista->pElemCorr = pLista->pFimLista ; } /* Fim função: LIS &Ir para o elemento final */
static tpElemLista * CriarElemento( LIS_tppLista lista , void * pValor #ifdef _DEBUG , LIS_tpTipo tipo #endif ) { tpElemLista * pElem ; pElem = ( tpElemLista * ) malloc( sizeof( tpElemLista )) ; if ( pElem == NULL ) { #ifdef _DEBUG CNT_CONTAR( "CriarElemento_FaltouMemoria" ) ; #endif return NULL; } /* if */ pElem->pValor = pValor ; pElem->pAnt = NULL ; pElem->pProx = NULL ; #ifdef _DEBUG CED_DefinirTipoEspaco( pElem , LIS_EspacoElemento ) ; CNT_CONTAR( "CriarElemento" ) ; pElem->pTipo = tipo ; pElem->pCabeca = lista ; pElem->tamElem = sizeof( tpElemLista ) ; pElem->tamValor = DefineTamValor( tipo ) ; #endif lista->numElem ++ ; return pElem; } /* Fim função: LIS Criar o elemento */
/*********************************************************************** * * $FC Função: PILHA Pop Pilha * * $FV Valor retornado * PILHA_CondRetNaoExiste Caso a PILHA nao exista * PILHA_CondRetOk * $ED Descrição do tipo * tira um elemento do topo da pilha ***********************************************************************/ PILHA_tpCondRet PILHA_Pop(PILHA_tppPilha p, CRT_tppCarta * carta){ int crt[2]; int condRet = 0; CRT_tppCarta crt_aux = NULL; if (p == NULL){ #ifdef _DEBUG CNT_CONTAR("PILHA_Pop-PilhaInexistente"); #endif return PILHA_CondRetPilhaInexistente; } if (p->topo == NULL){ #ifdef _DEBUG CNT_CONTAR("PILHA_Pop-PilhaTopoInexistente"); #endif return PILHA_CondRetPilhaInexistente; } //LIS_AvancarElementoCorrente(p->topo); crt_aux = (CRT_tppCarta)LIS_ObterValor(p->topo); CRT_ObtemCarta(crt_aux, &crt[0], &crt[1]); condRet = LIS_ExcluirElemento(p->topo); if (condRet == 1){ #ifdef _DEBUG CNT_CONTAR("PILHA_Pop-PilhaTopoInexistente-2"); #endif p = NULL; return PILHA_CondRetPilhaInexistente; } CRT_CriaCarta(&crt_aux, crt[0], crt[1]); *carta = crt_aux; p->quantidade -= 1; return PILHA_CondRetOK; } /* Fim função: PILHA POP Pilha */
LIS_tpCondRet LIS_DefinirFuncaoExcluir( LIS_tppLista lista, void (* pExcluir)( void * pValor)) { #ifdef _DEBUG assert( lista != NULL ) ; CNT_CONTAR( "LIS_DefinirFuncaoExcluir" ) ; #endif lista->pExcluir = pExcluir ; return LIS_CondRetOK; } /* Fim função: LIS &Definir Função Excluir */
void IrInicioLista( LIS_tppLista pLista ) { #ifdef _DEBUG assert( pLista != NULL ) ; #endif #ifdef _DEBUG CNT_CONTAR( "IrInicioLista" ) ; #endif pLista->pElemCorr = pLista->pOrigemLista ; } /* Fim função: LIS &Ir para o elemento inicial */
static void LimparCabeca( LIS_tppLista lista ) { #ifdef _DEBUG CNT_CONTAR( "LimparCabeca" ) ; #endif lista->pOrigemLista = NULL ; lista->pFimLista = NULL ; lista->pElemCorr = NULL ; lista->numElem = 0 ; lista->pExcluir = NULL ; } /* Fim função: LIS Limpar a cabeça da lista */
/*********************************************************************** * * $FC Função: PILHA Push Pilha * * $FV Valor retornado * PILHA_CondRetNaoExiste Caso a PILHA nao exista * PILHA_CondRetOk * $ED Descrição do tipo * bota um elemento no topo da pilha ***********************************************************************/ PILHA_tpCondRet PILHA_Push(PILHA_tppPilha p, CRT_tppCarta carta){ if (!p){ #ifdef _DEBUG CNT_CONTAR("PILHA_Push-pilhaInexistente"); #endif return PILHA_CondRetPilhaInexistente; } IrInicioLista(p->topo); LIS_InserirElementoAntes(p->topo, carta, "carta"); p->quantidade += 1; return PILHA_CondRetOK; } /* Fim função: PILHA Push Pilha */
LIS_tpCondRet LIS_DestruirLista( LIS_tppLista lista ) { #ifdef _DEBUG assert( lista != NULL ) ; CNT_CONTAR( "LIS_DestruirLista" ) ; #endif LIS_EsvaziarLista( lista ) ; free( lista ) ; return LIS_CondRetOK; } /* Fim função: LIS &Destruir lista */
void LIS_DestruirLista( LIS_tppLista pLista ) { #ifdef _DEBUG assert( pLista != NULL ) ; #endif #ifdef _DEBUG CNT_CONTAR( "DestruirLista" ) ; #endif LIS_EsvaziarLista( pLista ) ; free( pLista ) ; } /* Fim função: LIS &Destruir lista */
LIS_tpCondRet LIS_ObterValor(LIS_tppLista lista, void ** pValor) { #ifdef _DEBUG assert( lista != NULL ) ; assert( pValor != NULL ) ; #endif if ( lista->pElemCorr == NULL ) { #ifdef _DEBUG CNT_CONTAR( "LIS_ObterValor_NaoAchou" ) ; #endif return LIS_CondRetNaoAchou; } /* if */ #ifdef _DEBUG CNT_CONTAR( "LIS_ObterValor_Achou" ) ; #endif *pValor = lista->pElemCorr->pValor; return LIS_CondRetOK; } /* Fim função: LIS &Obter referência para o valor contido no elemento */
enum GRA_Ret GRA_AddLink (Graph *g, void *n) { Node *n1, *n2; enum GRA_Ret ret; CNT_CONTAR("GRA_AddLink - Inicio"); #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ if (!n){ CNT_CONTAR("GRA_AddLink - Nao existe n"); return GRA_NullData; } if (!g->currentNode){ CNT_CONTAR("GRA_AddLink - Nao existe g->currentNode"); return GRA_InvalidCurrentNode; } n1 = LIS_ObterValor(g->currentNode); assert(n1); CNT_CONTAR("GRA_AddLink - Procurando no"); if (searchData (g->nodes, n) != GRA_Ok){ CNT_CONTAR("GRA_AddLink - Nao encontrou no"); return GRA_InvalidArgNode; } n2 = LIS_ObterValor(LIS_ObterValor(g->nodes)); if (n2 == n1){ CNT_CONTAR("GRA_AddLink - Tentando criar link de n1 pra n1"); return GRA_InvalidArgNode; } #ifdef _DEBUG AssertNode(n1,g); AssertNode(n2,g); #endif /* _DEBUG */ CNT_CONTAR("GRA_AddLink - Ligando 2 nos"); ret = linkTwoNodes(n1, n2); #ifdef _DEBUG if (ret == GRA_Ok){ CNT_CONTAR("GRA_AddLink - Ligou os nos"); g->nOfLinks += 2; } AssertNode(n1,g); AssertNode(n2,g); AssertGraph(g); #endif /* _DEBUG */ return ret; }
void AssertLink(Link *l) { Link *b = l->brother; CNT_CONTAR("AssertLink - Verificando assertivas"); assert( b->brother == l ); assert( l != b ); assert( l->n1 == b->n2 ); assert( l->n2 == b->n1 ); assert( l->n1->links == b->n2->links ); assert( l->n2->links == b->n1->links ); IrInicioLista( l->n1->links ); IrInicioLista( b->n1->links ); assert( LIS_CondRetOK == LIS_ProcurarValor( l->n1->links , l) ); assert( LIS_CondRetOK == LIS_ProcurarValor( b->n1->links , b) ); }
LIS_tpCondRet LIS_VerificarElemento(LIS_tppLista lista) { LIS_tpElemLista *elem = lista->pElemCorr; CNT_CONTAR("Verificar elemento"); if (elem == NULL) { CNT_CONTAR("Elemento nulo"); TST_NotificarFalha("Tentou verificar elemento nulo."); return LIS_CondRetErroEstrutura; } CNT_CONTAR("Elemento nao nulo"); if (TST_CompararInt(4, /*TAB_TipoEspacoElemento*/ CED_ObterTipoEspaco(elem), "Tipo do espaco de dados nao é um elemento.") != TST_CondRetOK ) { CNT_CONTAR("Tipo elemento invalido"); return LIS_CondRetErroEstrutura; } CNT_CONTAR("Tipo elemento valido"); if (elem->pProx == NULL && elem->pAnt == NULL) { CNT_CONTAR("Elemento solto"); TST_NotificarFalha("Elemento esta solto"); return LIS_CondRetErroEstrutura; } CNT_CONTAR("Elemento ligado"); CED_MarcarEspacoAtivo(elem); CNT_CONTAR("Acaba verificar elemento"); return LIS_CondRetOK; } /* Fim funcao: LIS &Verificar elemento */
void *GRA_LinksGetNext (Graph *g) { void *ret; LIS_tppLista l; CNT_CONTAR("GRA_LinksGetNext - inicio"); #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ CNT_CONTAR("GRA_LinksGetNext - Inicializao"); if (!g->currentNode){ CNT_CONTAR("GRA_LinksGetNext - Nao existe currentNode"); return NULL; } CNT_CONTAR("GRA_LinksGetNext - Salvando lista de links"); l = ((Node*)LIS_ObterValor (g->currentNode))->links; if (!LIS_ObterValor(l)){ CNT_CONTAR("GRA_LinksGetNext - Nao acessou lista de links"); return NULL; } CNT_CONTAR("GRA_LinksGetNext - Salvando dados"); ret = ((Link*)LIS_ObterValor(l))->n2->data; if (g->linksOld == ret){ /* Se esse dado ja' repitiu, ja terminou a lista */ CNT_CONTAR("GRA_LinksGetNext - Dado repetido, fim da lista"); return NULL; } CNT_CONTAR("GRA_LinksGetNext - Avanando elemento corrente"); g->linksOld = ret; LIS_AvancarElementoCorrente (l, 1); #ifdef _DEBUG AssertGraph(g); #endif /* _DEBUG */ CNT_CONTAR("GRA_LinksGetNext - Finalizando"); return ret; }