LIS_tpCondRet LIS_InserirElementoOrdenado( LIS_tppLista pLista , void *pValor, int (*pComp) (void *a, void *b)) { LIS_tpElemLista *pElem ; #ifdef _DEBUG assert( pLista != NULL ) ; #endif /* Criar elemento a inserir antes */ pElem = CriarElemento( pLista , pValor ) ; if ( pElem == NULL ) { return LIS_CondRetFaltouMemoria ; } /* if */ /* Ir para o comeco da lista */ IrInicioLista (pLista); /* Percorrer a lista até encontrar um valor superior ou igual */ while (LIS_AvancarElementoCorrente(pLista, 1) != LIS_CondRetFimLista) { if (pComp(LIS_ObterValor(pLista), pValor) > 0) { break ; } /* if */ } /* while */ /* Encadear o elemento antes do elemento superior */ return LIS_InserirElementoAntes(pLista, pValor); } /* Fim funcao: LIS &Inserir elemento antes */
/*************************************************************************** * * Função: PIL &Adicionar Carta * ****/ PIL_tpCondRet PIL_AdicionarCarta (PIL_tpPilha pPilha, CAR_tpCarta carta) { LIS_tpCondRet retorno; CAR_tpCarta copiaCarta; CAR_tpValor copiaValor; CAR_tpNaipe copiaNaipe; CAR_tpEstado copiaEstado; CAR_tpCondRet condRetCarta; int tornarInvisivel = 0; if (pPilha == NULL) return PIL_CondRetPilhaNaoExiste; if (carta == NULL) return PIL_CondRetCartaNaoExiste; condRetCarta = CAR_ObterEstado(carta,&copiaEstado); if (condRetCarta != CAR_CondRetOK) return PIL_CondRetCartaNaoExiste; if (copiaEstado == CAR_EstadoInvisivel){ CAR_TornarVisivel(carta); tornarInvisivel = 1; } condRetCarta = CAR_ObterValor(carta,&copiaValor); if (condRetCarta != CAR_CondRetOK) return PIL_CondRetCartaNaoExiste; condRetCarta = CAR_ObterNaipe(carta,&copiaNaipe); if (condRetCarta != CAR_CondRetOK) return PIL_CondRetCartaNaoExiste; condRetCarta = CAR_CriarCarta(&copiaCarta,copiaValor,copiaNaipe); if(condRetCarta == CAR_CondRetFaltouMemoria) return PIL_CondRetFaltouMemoria; if (tornarInvisivel) CAR_TornarInvisivel(copiaCarta); retorno = LIS_InserirElementoAntes( pPilha->cabeca ,copiaCarta); if (retorno == LIS_CondRetFaltouMemoria) return PIL_CondRetFaltouMemoria; return PIL_CondRetOK; }
/*********************************************************************** * * $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 */
Tabuleiro *TAB_criar() { int x, y; Tabuleiro *tabuleiro = (Tabuleiro*)malloc(sizeof(Tabuleiro)); if(!tabuleiro) return NULL; //Tratamento de espaço dinamico #ifdef _DEBUG CED_DefinirTipoEspaco(tabuleiro, TAB_Tabuleiro); #endif tabuleiro->lista = LIS_CriarLista(ListaExcluirLista); if(!tabuleiro->lista) { free(tabuleiro); return NULL; } for(y = 0; y < TabuleiroAltura; ++y) { LIS_tppLista lista = LIS_CriarLista(ListaExcluirPeca); if(!lista) { TAB_destruir(tabuleiro); return NULL; } if(LIS_InserirElementoAntes(tabuleiro->lista, lista) != LIS_CondRetOK) { TAB_destruir(tabuleiro); return NULL; } for(x = 0; x < TabuleiroLargura; ++x){ if(LIS_InserirElementoAntes(lista, NULL) != LIS_CondRetOK) { TAB_destruir(tabuleiro); return NULL; } } } return tabuleiro; }/* Fim função: TAB &Criar tabuleiro */
TST_tpCondRet TST_EfetuarComando( char * ComandoTeste ) { int inxLista = -1 , numLidos = -1 , CondRetEsp = -1 ; char StringDado[ DIM_VALOR ] ; int ValEsp = -1 ; int i ; int numElem = -1 ; StringDado[ 0 ] = 0 ; /* Efetuar reset de teste de lista */ if ( strcmp( ComandoTeste , RESET_LISTA_CMD ) == 0 ) { for( i = 0 ; i < DIM_VT_LISTA ; i++ ) { vtListas[ i ] = NULL ; } /* for */ return TST_CondRetOK ; } /* fim ativa: Efetuar reset de teste de lista */ /* Testar CriarLista */ else if ( strcmp( ComandoTeste , CRIAR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , VAZIO ))) { return TST_CondRetParm ; } /* if */ vtListas[ inxLista ] = LIS_CriarLista( DestruirValor ) ; return TST_CompararPonteiroNulo( 1 , vtListas[ inxLista ] , "Erro em ponteiro de nova lista." ) ; } /* fim ativa: Testar CriarLista */ /* Testar Esvaziar lista lista */ else if ( strcmp( ComandoTeste , ESVAZIAR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO ))) { return TST_CondRetParm ; } /* if */ LIS_EsvaziarLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: Testar Esvaziar lista lista */ /* Testar Destruir lista */ else if ( strcmp( ComandoTeste , DESTRUIR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO ))) { return TST_CondRetParm ; } /* if */ LIS_DestruirLista( vtListas[ inxLista ] ) ; vtListas[ inxLista ] = NULL ; return TST_CondRetOK ; } /* fim ativa: Testar Destruir lista */ /* Testar inserir elemento antes */ else if ( strcmp( ComandoTeste , INS_ELEM_ANTES_CMD ) == 0 ) { struct user *u = (struct user*) malloc (sizeof (struct user)); LIS_tpCondRet lret; if (!u) return TST_CondRetMemoria; numLidos = LER_LerParametros( "isssi" , &inxLista , u->nome, u->tel, u->ender , &CondRetEsp ) ; if ( ( numLidos != 5 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ lret = LIS_InserirElementoAntes( vtListas[ inxLista ] , u ) ; if ( lret != LIS_CondRetOK ) { free( u ) ; } /* if */ return TST_CompararInt( CondRetEsp , lret , "Condicao de retorno errada ao inserir antes." ) ; } /* fim ativa: Testar inserir elemento antes */ /* Testar inserir elemento apos */ else if ( strcmp( ComandoTeste , INS_ELEM_APOS_CMD ) == 0 ) { struct user *u = (struct user *)malloc (sizeof (struct user)); LIS_tpCondRet lret; if (!u) return TST_CondRetMemoria; numLidos = LER_LerParametros( "isssi", &inxLista, u->nome, u->tel, u->ender, &CondRetEsp ) ; if ( ( numLidos != 5 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ lret = LIS_InserirElementoApos( vtListas[ inxLista ] , u ) ; if ( lret != LIS_CondRetOK ) { free( u ) ; } /* if */ return TST_CompararInt( CondRetEsp , lret, "Condicao de retorno errada ao inserir antes.") ; } /* fim ativa: Testar inserir elemento apos */ /* Testar excluir simbolo */ else if ( strcmp( ComandoTeste , EXC_ELEM_CMD ) == 0 ) { numLidos = LER_LerParametros( "ii" , &inxLista , &CondRetEsp ) ; if ( ( numLidos != 2 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ return TST_CompararInt( CondRetEsp , LIS_ExcluirElemento( vtListas[ inxLista ] ) , "Condição de retorno errada ao excluir." ) ; } /* fim ativa: Testar excluir simbolo */ /* Testar obter valor do elemento corrente */ else if ( strcmp( ComandoTeste , OBTER_NOME_CMD ) == 0 ) { struct user *u; numLidos = LER_LerParametros( "isi" , &inxLista , StringDado , &ValEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ u = (struct user*) LIS_ObterValor( vtListas[ inxLista ] ) ; if ( ValEsp == 0 ) { return TST_CompararPonteiroNulo( 0 , u, "Valor não deveria existir." ) ; } /* if */ if ( u == NULL ) { return TST_CompararPonteiroNulo( 1 , u, "Dado tipo um deveria existir." ) ; } /* if */ return TST_CompararString( StringDado , u->nome, "Valor nome do elemento esta' errado." ) ; } /* fim ativa: Testar obter valor do elemento corrente */ /* Testar obter valor do elemento corrente */ else if ( strcmp( ComandoTeste, OBTER_TEL_CMD ) == 0 ) { struct user *u; numLidos = LER_LerParametros( "isi" , &inxLista , StringDado , &ValEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ u = (struct user*) LIS_ObterValor( vtListas[ inxLista ] ) ; if ( ValEsp == 0 ) { return TST_CompararPonteiroNulo( 0 , u, "Valor não deveria existir." ) ; } /* if */ if ( u == NULL ) { return TST_CompararPonteiroNulo( 1 , u, "Dado tipo um deveria existir." ) ; } /* if */ return TST_CompararString( StringDado , u->tel, "Valor nome do elemento esta' errado." ) ; } /* fim ativa: Testar obter valor do elemento corrente */ /* Testar obter valor do elemento corrente */ else if ( strcmp( ComandoTeste , OBTER_ENDER_CMD ) == 0 ) { struct user *u; numLidos = LER_LerParametros( "isi" , &inxLista , StringDado , &ValEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ u = (struct user*) LIS_ObterValor( vtListas[ inxLista ] ) ; if ( ValEsp == 0 ) { return TST_CompararPonteiroNulo( 0 , u, "Valor não deveria existir." ) ; } /* if */ if ( u == NULL ) { return TST_CompararPonteiroNulo( 1 , u, "Dado tipo um deveria existir." ) ; } /* if */ return TST_CompararString( StringDado , u->ender, "Valor nome do elemento esta' errado." ) ; } /* fim ativa: Testar obter valor do elemento corrente */ /* Testar ir para o elemento inicial */ else if ( strcmp( ComandoTeste , IR_INICIO_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ IrInicioLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: Testar ir para o elemento inicial */ /* LIS &Ir para o elemento final */ else if ( strcmp( ComandoTeste , IR_FIM_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ IrFinalLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: LIS &Ir para o elemento final */ /* LIS &Avançar elemento */ else if ( strcmp( ComandoTeste , AVANCAR_ELEM_CMD ) == 0 ) { numLidos = LER_LerParametros( "iii" , &inxLista , &numElem , &CondRetEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ return TST_CompararInt( CondRetEsp , LIS_AvancarElementoCorrente( vtListas[ inxLista ] , numElem ) , "Condicao de retorno errada ao avancar" ) ; } /* fim ativa: LIS &Avançar elemento */ return TST_CondRetNaoConhec ; } /* Fim função: TLIS &Testar lista */
TST_tpCondRet TST_EfetuarComando( char * ComandoTeste ) { /* Variáveis para retorno de operações com lista */ /* Devem ser inicializadas com qualquer valor */ LIS_tpCondRet LIS_CondRetObtido = LIS_CondRetOK ; LIS_tpCondRet LIS_CondRetEsperada = LIS_CondRetFaltouMemoria ; int indexFocoLista = '0'; int NumLidos = -1 ; int i = 0; int resposta = 0; int numPassos = 0; char * caracter; char stringDado [2] ; TST_tpCondRet Ret = TST_CondRetOK ; stringDado[1]='\0'; /* Efetuar reset de teste de lista */ if ( strcmp (ComandoTeste, RESET_LIS_CMD) == 0 ) { for ( i = 0; i < DIM_VT_LISTA; i++ ) vtListas[ i ] = NULL; return TST_CondRetOK; } /* Fim ativa: Efetuar reset de teste de lista */ /* Testar LIS Criar lista */ else if ( strcmp( ComandoTeste , CRIAR_LIS_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ii" , &indexFocoLista, &LIS_CondRetEsperada ) ; if ( NumLidos != 2 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm ; } /* if */ LIS_CondRetObtido = LIS_CriarLista( &vtListas[ indexFocoLista ], DestruirConteudoLista ); return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao criar lista." ); } /* Fim ativa: Testar LIS Criar lista */ /* Testar LIS Inserir elemento na lista antes do elemento corrente */ else if ( strcmp( ComandoTeste , INSERIR_ANTES_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ici" , &indexFocoLista, stringDado, &LIS_CondRetEsperada ) ; if ( NumLidos != 3 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm ; } /* if */ caracter = ( char * ) malloc( strlen( stringDado ) + 1 ) ; if ( caracter == NULL ) { return TST_CondRetMemoria ; } /* if */ strcpy( caracter , stringDado ) ; LIS_CondRetObtido = LIS_InserirElementoAntes ( vtListas[ indexFocoLista ], caracter ); return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao inserir à esquerda (antes do elemento corrente da lista)." ); } /* fim ativa: Testar LIS Inserir elemento na lista antes do elemento corrente */ /* Testar LIS Inserir elemento na lista depois do elemento corrente */ else if ( strcmp( ComandoTeste , INSERIR_APOS_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ici" , &indexFocoLista, stringDado, &LIS_CondRetEsperada ) ; if ( NumLidos != 3 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm ; } /* if */ caracter = ( char * ) malloc( strlen( stringDado ) + 1 ) ; if ( caracter == NULL ) { return TST_CondRetMemoria ; } /* if */ strcpy( caracter , stringDado ) ; LIS_CondRetObtido = LIS_InserirElementoApos ( vtListas[ indexFocoLista ], caracter ); return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao inserir à direita (após elemento corrente)." ); } /* fim ativa: Testar LIS Inserir elemento na lista depois do elemento corrente */ /* Testar LIS Ir para elemento inicial (origem da lista) */ else if ( strcmp( ComandoTeste , IR_INICIOLISTA_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ii" , &indexFocoLista, &LIS_CondRetEsperada ) ; if ( NumLidos != 2 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm ; } /* if */ LIS_CondRetObtido = LIS_IrInicioLista( vtListas[ indexFocoLista ] ) ; return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao ir para o início da lista." ); } /* fim ativa: Testar LIS Ir para elemento inicial (origem da lista) */ /* Testar LIS Ir para elemento final (fim da lista) */ else if ( strcmp( ComandoTeste , IR_FIMLISTA_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ii" , &indexFocoLista, &LIS_CondRetEsperada ) ; if ( NumLidos != 2 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm ; } /* if */ LIS_CondRetObtido = LIS_IrFinalLista( vtListas[ indexFocoLista ] ) ; return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao ir para o fim da lista." ); } /* fim ativa: Testar LIS Ir para elemento final (fim da lista) */ /* Testar LIS Percorre lista */ else if ( strcmp( ComandoTeste , PERCORRE_LIS_CMD ) == 0 ) { NumLidos = LER_LerParametros( "iii" , &indexFocoLista, &numPassos, &LIS_CondRetEsperada ) ; if ( NumLidos != 3 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm ; } /* if */ LIS_CondRetObtido = LIS_PercorreLista( vtListas[ indexFocoLista ], numPassos ) ; return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao percorrer a lista." ); } /* fim ativa: Testar LIS Percorre lista */ /* Testar LIS Obter elementos existentes na lista */ else if ( strcmp ( ComandoTeste, OBTER_NUMELEMLISTA_CMD ) == 0 ) { NumLidos = LER_LerParametros ( "ii", &indexFocoLista, &LIS_CondRetEsperada ) ; if ( NumLidos != 2 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm; } LIS_CondRetObtido = LIS_ObterNumeroElementos ( vtListas[ indexFocoLista ], &resposta ); if ( LIS_CondRetObtido == LIS_CondRetOK ) { printf ( "\nNumero de elementos na lista: %d\n", resposta ); } return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao requisitar número de elementos de lista." ); } /* fim ativa: Testar LIS Obter elementos existentes na lista */ /* Testar LIS Excluir elemento corrente da lista */ else if ( strcmp ( ComandoTeste, EXC_ELEM_CMD ) == 0 ) { NumLidos = LER_LerParametros ( "ii", &indexFocoLista, &LIS_CondRetEsperada ); if ( NumLidos != 2 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA )) { return TST_CondRetParm; } LIS_CondRetObtido = LIS_ExcluirElementoCorrente ( vtListas[ indexFocoLista ] ); return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao excluir elemento corrente da lista." ); } /* fim ativa: Testar LIS Excluir elemento corrente da lista */ /* Testar LIS Procurar valor nos elementos da lista */ else if ( strcmp ( ComandoTeste, PROCURA_VALISTA_CMD ) == 0 ) { NumLidos = LER_LerParametros ( "ici", &indexFocoLista, stringDado, &LIS_CondRetEsperada ); if ( NumLidos != 3 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA )) { return TST_CondRetParm; } LIS_CondRetObtido = LIS_ProcurarValor ( vtListas[ indexFocoLista ], (void**)&stringDado, ComparaConteudo ); return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao procurar elemento da lista." ); } /* fim ativa: Testar LIS Procurar valor nos elementos da lista */ /* Testar LIS Obter valor armazenado no elemento corrente */ else if ( strcmp( ComandoTeste , OBTER_VALISTA_CMD ) == 0 ) { NumLidos = LER_LerParametros( "ici" , &indexFocoLista, stringDado, &LIS_CondRetEsperada ); if ( NumLidos != 3 || !ValidarIndex( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm ; } /* if */ LIS_CondRetObtido = LIS_ObterValorCorrente ( vtListas[ indexFocoLista ], (void**)&caracter ) ; if ( LIS_CondRetObtido == LIS_CondRetOK ) { printf ("\nCaracter obtido: %c \n", *caracter ); return TST_CompararChar( stringDado[0], *caracter, "Caracteres diferentes" ); } else { return TST_CompararInt( LIS_CondRetEsperada, LIS_CondRetObtido, "Retorno errado para obter elemento do elemento corrente da lista." ); } } /* fim ativa: Testar LIS Obter valor armazenado no elemento corrente */ /* Testar LIS Esvaziar lista */ else if ( strcmp( ComandoTeste , ESVAZIAR_LIS_CMD ) == 0 ) { NumLidos = LER_LerParametros ( "ii", &indexFocoLista, &LIS_CondRetEsperada ); if (NumLidos != 2 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm; } LIS_CondRetObtido = LIS_EsvaziarLista ( vtListas[ indexFocoLista ] ) ; return TST_CompararInt ( LIS_CondRetEsperada, LIS_CondRetObtido, "Retorno de erro ao esvaziar lista." ); } /* fim ativa: Testar LIS Esvaziar lista */ /* Testar LIS Destruir lista */ else if ( strcmp( ComandoTeste , DESTROI_LIS_CMD ) == 0 ) { NumLidos = LER_LerParametros ( "ii", &indexFocoLista, &LIS_CondRetEsperada ); if ( NumLidos != 2 || !ValidarIndex ( indexFocoLista, DIM_VT_LISTA ) ) { return TST_CondRetParm; } LIS_CondRetObtido = LIS_DestruirLista( &vtListas[ indexFocoLista ] ) ; return TST_CompararInt( LIS_CondRetEsperada , LIS_CondRetObtido , "Retorno errado ao destruir lista." ); } /* fim ativa: Testar LIS Destruir lista */ return TST_CondRetNaoConhec ; } /* Fim função: TLIS Efetuar operações de teste específicas para lista de caracteres */
TST_tpCondRet TST_EfetuarComando(char * ComandoTeste) { int inxLista = -1, numLidos = -1, CondRetEsp = -1; TST_tpCondRet CondRet; char StringDado[DIM_VALOR]; char * pDado; int IntDado[DIM_VALOR_INT]; int* pDadoInt; int ValEsp = -1; int i; int numElem = -1; int inxStoredPtr; for(i = 0; i < DIM_VT_LISTA; i++) storedPtr[i][0] = NULL; StringDado[0] = 0; /* Efetuar reset de teste de lista */ if(strcmp(ComandoTeste, RESET_LISTA_CMD) == 0) { for(i = 0; i < DIM_VT_LISTA; i++) { vtListas[i] = NULL; storedPtrIndex[i] = 1; } return TST_CondRetOK; } /* fim ativa: Efetuar reset de teste de lista */ /* Testar CriarLista */ else if(strcmp(ComandoTeste, CRIAR_LISTA_CMD) == 0) { numLidos = LER_LerParametros("i", &inxLista); if((numLidos != 1) || (!ValidarInxLista(inxLista, VAZIO))) return TST_CondRetParm; vtListas[inxLista] = LIS_CriarLista(DestruirValor); return TST_CompararPonteiroNulo(1, vtListas[inxLista], "Erro em ponteiro de nova lista."); } /* fim ativa: Testar CriarLista */ /* Testar Esvaziar lista lista */ else if(strcmp(ComandoTeste, ESVAZIAR_LISTA_CMD) == 0) { numLidos = LER_LerParametros("i", &inxLista); if((numLidos != 1) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; LIS_EsvaziarLista(vtListas[inxLista]); return TST_CondRetOK; } /* fim ativa: Testar Esvaziar lista lista */ /* Testar Destruir lista */ else if(strcmp(ComandoTeste, DESTRUIR_LISTA_CMD) == 0) { numLidos = LER_LerParametros("i", &inxLista); if((numLidos != 1) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; LIS_DestruirLista(vtListas[inxLista]); vtListas[inxLista] = NULL; return TST_CondRetOK; } /* fim ativa: Testar Destruir lista */ /* Testar inserir elemento antes */ else if(strcmp(ComandoTeste, INS_ELEM_ANTES_CMD) == 0) { numLidos = LER_LerParametros("isi", &inxLista, StringDado, &CondRetEsp); if((numLidos != 3) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; if(!ValidarInxStoredPtr(storedPtrIndex[inxLista])) return TST_CondRetMemoria; pDado = (char *) malloc(strlen(StringDado) + 1); if(pDado == NULL) return TST_CondRetMemoria; storedPtr[inxLista][storedPtrIndex[inxLista]++] = pDado; strcpy(pDado, StringDado); CondRet = LIS_InserirElementoAntes(vtListas[inxLista], pDado); if(CondRet != LIS_CondRetOK) free(pDado); return TST_CompararInt(CondRetEsp, CondRet, "Condicao de retorno errada ao inserir antes."); } /* fim ativa: Testar inserir elemento antes */ /* Testar inserir elemento antes INTEIRO */ else if(strcmp(ComandoTeste, INS_ELEM_ANTES_CMD_INT) == 0) { numLidos = LER_LerParametros("iiii", &inxLista, &IntDado[0], &IntDado[1], &CondRetEsp); if((numLidos != 4) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; if(!ValidarInxStoredPtr(storedPtrIndex[inxLista])) return TST_CondRetMemoria; pDadoInt = (int *) malloc(DIM_VALOR_INT*sizeof(int)); if(pDadoInt == NULL) return TST_CondRetMemoria; storedPtr[inxLista][storedPtrIndex[inxLista]++] = pDadoInt; for(i=0;i<DIM_VALOR_INT;i++) pDadoInt[i]=IntDado[i]; CondRet = LIS_InserirElementoAntes(vtListas[inxLista], pDadoInt); if(CondRet != LIS_CondRetOK) free(pDadoInt); return TST_CompararInt(CondRetEsp, CondRet, "Condicao de retorno errada ao inserir antes."); } /* fim ativa: Testar inserir elemento antes INTEIRO*/ /* Testar inserir elemento apos */ else if(strcmp(ComandoTeste, INS_ELEM_APOS_CMD) == 0) { numLidos = LER_LerParametros("isi", &inxLista, StringDado, &CondRetEsp); if((numLidos != 3) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; if(!ValidarInxStoredPtr(storedPtrIndex[inxLista])) return TST_CondRetMemoria; pDado = (char*)malloc(strlen(StringDado) + 1); if(pDado == NULL) return TST_CondRetMemoria; storedPtr[inxLista][storedPtrIndex[inxLista]++] = pDado; strcpy(pDado, StringDado); CondRet = LIS_InserirElementoApos(vtListas[inxLista], pDado); if(CondRet != LIS_CondRetOK) free(pDado); return TST_CompararInt(CondRetEsp, CondRet, "Condicao de retorno errada ao inserir apos."); } /* fim ativa: Testar inserir elemento apos */ /* Testar inserir elemento apos INTEIRO*/ else if(strcmp(ComandoTeste, INS_ELEM_APOS_CMD_INT) == 0) { numLidos = LER_LerParametros("iiii", &inxLista, &IntDado[0], &IntDado[1], &CondRetEsp); if((numLidos != 4) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; if(!ValidarInxStoredPtr(storedPtrIndex[inxLista])) return TST_CondRetMemoria; pDadoInt = (int *) malloc(DIM_VALOR_INT * sizeof(int)); if(pDadoInt == NULL) return TST_CondRetMemoria; storedPtr[inxLista][storedPtrIndex[inxLista]++] = pDadoInt; for(i=0;i<DIM_VALOR_INT;i++) pDadoInt[i]=IntDado[i]; CondRet = LIS_InserirElementoApos(vtListas[inxLista], pDadoInt); if(CondRet != LIS_CondRetOK) free(pDadoInt); return TST_CompararInt(CondRetEsp, CondRet, "Condicao de retorno errada ao inserir apos."); } /* fim ativa: Testar inserir elemento apos INTEIRO*/ /* Testar excluir simbolo */ else if(strcmp(ComandoTeste, EXC_ELEM_CMD) == 0) { numLidos = LER_LerParametros("ii", &inxLista, &CondRetEsp); if((numLidos != 2) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; return TST_CompararInt(CondRetEsp, LIS_ExcluirElemento(vtListas[inxLista]), "Condição de retorno errada ao excluir."); } /* fim ativa: Testar excluir simbolo */ /* Testar obter valor do elemento corrente */ else if(strcmp(ComandoTeste, OBTER_VALOR_CMD) == 0) { numLidos = LER_LerParametros("isi", &inxLista, StringDado, &ValEsp); if((numLidos != 3) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; pDado = (char *) LIS_ObterValor(vtListas[inxLista]); if(ValEsp == 0) return TST_CompararPonteiroNulo(0, pDado, "Valor não deveria existir."); if(pDado == NULL) return TST_CompararPonteiroNulo(1, pDado, "Dado tipo um deveria existir."); return TST_CompararString(StringDado, pDado, "Valor do elemento errado."); } /* fim ativa: Testar obter valor do elemento corrente */ /* Testar obter valor do elemento corrente INTEIRO*/ else if(strcmp(ComandoTeste, OBTER_VALOR_CMD_INT) == 0) { numLidos = LER_LerParametros("iiii", &inxLista, &IntDado[0], &IntDado[1], &ValEsp); if((numLidos != 4) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; pDadoInt = (int *)LIS_ObterValor(vtListas[inxLista]); if(ValEsp == 0) return TST_CompararPonteiroNulo(0, pDadoInt, "Valor não deveria existir."); if(pDadoInt == NULL) return TST_CompararPonteiroNulo(1, pDadoInt, "Dado tipo um deveria existir."); return TST_CompararEspaco(IntDado, pDadoInt, DIM_VALOR_INT * sizeof(int), "Valor do elemento errado."); } /* fim ativa: Testar obter valor do elemento corrente INTEIRO*/ /* Testar ir para o elemento inicial */ else if(strcmp(ComandoTeste, IR_INICIO_CMD) == 0) { numLidos = LER_LerParametros("i", &inxLista); if((numLidos != 1) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; LIS_IrInicioLista(vtListas[inxLista]); return TST_CondRetOK; } /* fim ativa: Testar ir para o elemento inicial */ /* LIS &Ir para o elemento final */ else if(strcmp(ComandoTeste, IR_FIM_CMD) == 0) { numLidos = LER_LerParametros("i", &inxLista); if((numLidos != 1) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; LIS_IrFinalLista(vtListas[inxLista]); return TST_CondRetOK; } /* fim ativa: LIS &Ir para o elemento final */ /* LIS &Avançar elemento */ else if(strcmp(ComandoTeste, AVANCAR_ELEM_CMD) == 0) { numLidos = LER_LerParametros("iii", &inxLista, &numElem, &CondRetEsp); if((numLidos != 3) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; return TST_CompararInt(CondRetEsp, LIS_AvancarElementoCorrente(vtListas[inxLista], numElem), "Condicao de retorno errada ao avancar"); } /* fim ativa: LIS &Avançar elemento */ /* Testar Ir Indice */ else if(strcmp(ComandoTeste, IR_INDICE_CMD) == 0) { numLidos = LER_LerParametros("iii", &inxLista, &numElem, &CondRetEsp); if((numLidos != 3) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; return TST_CompararInt(CondRetEsp, LIS_IrIndice(vtListas[inxLista], numElem), "Condicao de retorno errada ao ir para o índice"); } /* fim ativa: Testar Ir Indice */ /* Testar Setar Valor INTEIRO*/ else if(strcmp(ComandoTeste, SETAR_VALOR_CMD_INT) == 0) { numLidos = LER_LerParametros("iiii", &inxLista, &IntDado[0], &IntDado[1], &CondRetEsp); if((numLidos != 4) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; if(!ValidarInxStoredPtr(storedPtrIndex[inxLista])) return TST_CondRetMemoria; pDadoInt = (int *) malloc(DIM_VALOR_INT * sizeof(int)); if(pDadoInt == NULL) return TST_CondRetMemoria; storedPtr[inxLista][storedPtrIndex[inxLista]++] = pDadoInt; for(i=0;i<DIM_VALOR_INT;i++) pDadoInt[i]=IntDado[i]; return TST_CompararInt(CondRetEsp, LIS_SetarValor(vtListas[inxLista], pDadoInt), "Condicao de retorno errada ao ir marcar o valor"); } /* fim ativa: LIS Setar Valor INTEIRO*/ /* Testar Procurar Valor */ else if(strcmp(ComandoTeste, PROCURAR_VALOR_CMD) == 0) { numLidos = LER_LerParametros("iii", &inxLista, &inxStoredPtr, &CondRetEsp); if((numLidos != 3) || (!ValidarInxLista(inxLista, NAO_VAZIO))) return TST_CondRetParm; if(!ValidarInxStoredPtr(inxStoredPtr)) return TST_CondRetParm; return TST_CompararInt(CondRetEsp, LIS_ProcurarValor(vtListas[inxLista], storedPtr[inxLista][inxStoredPtr]), "Condicao de retorno errada ao procurar valor"); } /* fim ativa: LIS Procurar Valor */ return TST_CondRetNaoConhec; } /* Fim função: TLIS &Testar lista */
TST_tpCondRet TST_EfetuarComando( char * ComandoTeste ) { int inxLista = -1 , numLidos = -1 , CondRetEsp = -1 ; TST_tpCondRet CondRet ; int CondRetObtido = LIS_CondRetOK; int det, falhasEsperadas, falhasObtidas; char StringDado[ DIM_VALOR ] ; char * pDado ; int ValEsp = -1 ; int i,num ; int numElem = -1 ; StringDado[ 0 ] = 0 ; /* Efetuar reset de teste de lista */ if ( strcmp( ComandoTeste , RESET_LISTA_CMD ) == 0 ) { for( i = 0 ; i < DIM_VT_LISTA ; i++ ) { vtListas[ i ] = NULL ; } /* for */ return TST_CondRetOK ; } /* fim ativa: Efetuar reset de teste de lista */ /* Testar CriarLista */ else if ( strcmp( ComandoTeste , CRIAR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , VAZIO ))) { return TST_CondRetParm ; } /* if */ vtListas[ inxLista ] = LIS_CriarLista( DestruirValor ) ; return TST_CompararPonteiroNulo( 1 , vtListas[ inxLista ] , "Erro em ponteiro de nova lista." ) ; } /* fim ativa: Testar CriarLista */ /* Testar Esvaziar lista lista */ else if ( strcmp( ComandoTeste , ESVAZIAR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO ))) { return TST_CondRetParm ; } /* if */ LIS_EsvaziarLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: Testar Esvaziar lista lista */ /* Testar Destruir lista */ else if ( strcmp( ComandoTeste , DESTRUIR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO ))) { return TST_CondRetParm ; } /* if */ LIS_DestruirLista( vtListas[ inxLista ] ) ; vtListas[ inxLista ] = NULL ; return TST_CondRetOK ; } /* fim ativa: Testar Destruir lista */ /* Testar inserir elemento antes */ else if ( strcmp( ComandoTeste , INS_ELEM_ANTES_CMD ) == 0 ) { numLidos = LER_LerParametros( "isi" , &inxLista , StringDado , &CondRetEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ pDado = ( char * ) malloc( strlen( StringDado ) + 1 ) ; if ( pDado == NULL ) { return TST_CondRetMemoria ; } /* if */ strcpy( pDado , StringDado ) ; CondRet = LIS_InserirElementoAntes( vtListas[ inxLista ] , pDado ) ; if ( CondRet != LIS_CondRetOK ) { free( pDado ) ; } /* if */ return TST_CompararInt( CondRetEsp , CondRet , "Condicao de retorno errada ao inserir antes." ) ; } /* fim ativa: Testar inserir elemento antes */ /* Testar inserir elemento apos */ else if ( strcmp( ComandoTeste , INS_ELEM_APOS_CMD ) == 0 ) { numLidos = LER_LerParametros( "isi" , &inxLista , StringDado , &CondRetEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ pDado = ( char * ) malloc( strlen( StringDado ) + 1 ) ; if ( pDado == NULL ) { return TST_CondRetMemoria ; } /* if */ strcpy( pDado , StringDado ) ; CondRet = LIS_InserirElementoApos( vtListas[ inxLista ] , pDado ) ; if ( CondRet != LIS_CondRetOK ) { free( pDado ) ; } /* if */ return TST_CompararInt( CondRetEsp , CondRet , "Condicao de retorno errada ao inserir apos." ) ; } /* fim ativa: Testar inserir elemento apos */ /* Testar excluir simbolo */ else if ( strcmp( ComandoTeste , EXC_ELEM_CMD ) == 0 ) { numLidos = LER_LerParametros( "ii" , &inxLista , &CondRetEsp ) ; if ( ( numLidos != 2 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ return TST_CompararInt( CondRetEsp , LIS_ExcluirElemento( vtListas[ inxLista ] ) , "Condição de retorno errada ao excluir." ) ; } /* fim ativa: Testar excluir simbolo */ /* Testar obter valor do elemento corrente */ else if ( strcmp( ComandoTeste , OBTER_VALOR_CMD ) == 0 ) { numLidos = LER_LerParametros( "isi" , &inxLista , StringDado , &ValEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ pDado = ( char * ) LIS_ObterValor( vtListas[ inxLista ] ) ; if ( ValEsp == 0 ) { return TST_CompararPonteiroNulo( 0 , pDado , "Valor não deveria existir." ) ; } /* if */ if ( pDado == NULL ) { return TST_CompararPonteiroNulo( 1 , pDado , "Dado tipo um deveria existir." ) ; } /* if */ return TST_CompararString( StringDado , pDado , "Valor do elemento errado." ) ; } /* fim ativa: Testar obter valor do elemento corrente */ /* Testar ir para o elemento inicial */ else if ( strcmp( ComandoTeste , IR_INICIO_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ IrInicioLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: Testar ir para o elemento inicial */ /* LIS &Ir para o elemento final */ else if ( strcmp( ComandoTeste , IR_FIM_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ IrFinalLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: LIS &Ir para o elemento final */ /* LIS &Avançar elemento */ else if ( strcmp( ComandoTeste , AVANCAR_ELEM_CMD ) == 0 ) { numLidos = LER_LerParametros( "iii" , &inxLista , &numElem , &CondRetEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ return TST_CompararInt( CondRetEsp , LIS_AvancarElementoCorrente( vtListas[ inxLista ] , numElem ) , "Condicao de retorno errada ao avancar" ) ; } /* fim ativa: LIS &Avançar elemento */ /* LIS &Testar Obter Tamanho Lista */ else if (strcmp ( ComandoTeste , OBTER_TAMANHO_CMD ) == 0 ) { numLidos = LER_LerParametros( "iii" ,&inxLista,&ValEsp, &CondRetEsp ) ; if ( ( numLidos != 3 )||( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ CondRetObtido = LIS_ObterTamanho( vtListas[ inxLista ], &num ); CondRet = TST_CompararInt( ValEsp , num ,"Valor do elemento errado, diferente do esperado" ) ; if ( CondRet != TST_CondRetOK ) return CondRet ; return TST_CondRetOK; } /* Fim testar Obter Tamanho */ #ifdef _DEBUG /* Testar Deturpar */ else if (strcmp ( ComandoTeste , DETURPA_CMD ) == 0 ) { numLidos = LER_LerParametros( "ii" ,&inxLista, &det) ; if ( numLidos != 2 ) { return TST_CondRetParm ; } /* if */ DeturpaLista(vtListas[ inxLista ], det); return TST_CondRetOK; } else if (strcmp ( ComandoTeste , VERIFICA_CMD ) == 0 ) { numLidos = LER_LerParametros( "ii" ,&inxLista, &falhasEsperadas) ; if ( numLidos != 2 ) { return TST_CondRetParm ; } /* if */ verificaLista(vtListas[ inxLista ], &falhasObtidas); CondRet = TST_CompararInt( falhasEsperadas , falhasObtidas ,"Numero de falhas esperadas diferente de obtida" ) ; if ( CondRet != TST_CondRetOK ) return CondRet ; return TST_CondRetOK; } #endif return TST_CondRetNaoConhec ; } /* Fim função: TLIS &Testar lista */
void PDCRT_Push( LIS_tppCabeca * cb, Crt * carta ) { LIS_IrInicioLista( cb ) ; LIS_InserirElementoAntes( cb, carta ) ; } /* Fim função: PDCRT Push */
GRA_tpCondRet GRA_BuscarCaminho( GRA_tppGrafo pGrafo , int idVerticeOrigem, int idVerticeDestino, LIS_tppLista * pLista ) { tpVertice * v = NULL; tpVertice * u = NULL; tpVertice * origem1 = NULL; tpVertice * origem2 = NULL; int lenV = 0; LIS_tppLista Q = NULL; //FILA LIS_tppLista arestas = NULL; LIS_tppLista retorno = NULL; int t; int len = 0; int achou = 0; int ok = 0; int i,j,in; int lenD; int alt = 0; int* visitados = NULL; // Vetor de vertices visitados int* vizinhos = NULL; int* idAux = NULL; Dist** dists = NULL; Dist* dist = NULL; //aux; Dist* currDist = NULL; lenD = 1; v = get_by_id(pGrafo, idVerticeOrigem); u = get_by_id(pGrafo, idVerticeDestino); if(v == NULL || u == NULL) { return GRA_CondRetNaoEhVertice; } origem1 = ObterOrigem(pGrafo, v); origem2 = ObterOrigem(pGrafo, u); if (origem1 != origem2) { return GRA_CondRetNaoEhConexo; }//Else: É conexo, devia retornar Ok. for (;;) { dists = (Dist**)calloc(LIS_NumeroDeElementos(pGrafo->vertices)+1, sizeof(Dist*)); if (dists == NULL) {break;} dists[0] = newDist(idVerticeOrigem, 0); retorno = LIS_CriarLista(free); if (retorno == NULL) { break; } else if (v == u) { if( LIS_InserirElementoApos(retorno, newInt(idVerticeOrigem)) == LIS_CondRetOK) { *pLista = retorno; return GRA_CondRetOK; } else { break; } } visitados = (int*) calloc(LIS_NumeroDeElementos(pGrafo->vertices)+1,sizeof(int)); if (visitados == NULL) { break; } Q = LIS_CriarLista(free); if (Q == NULL) { break; } visitados[0] = idVerticeOrigem; lenV = 1; if (LIS_InserirElementoApos(Q, newInt(idVerticeOrigem)) != LIS_CondRetOK) { break;} //enque ok = 1; break; } if (!ok) { free(dists); LIS_DestruirLista(retorno); free(visitados); LIS_DestruirLista(Q); return GRA_CondRetFaltouMemoria; } while (LIS_NumeroDeElementos(Q) > 0) { //dequeue LIS_IrInicioLista(Q); t = getInt(LIS_ObterValor(Q)); LIS_ExcluirElemento(Q); //Iterar sobre vizinhos GRA_ObterVizinhos(pGrafo, t, &arestas); vizinhos = converteListaParaVetorDeInteiros(arestas, &len); LIS_DestruirLista(arestas); arestas = NULL; currDist = getDist(dists, t); if(!currDist) { return GRA_CondRetFaltouMemoria; } else { } alt = currDist->dist + 1; for (i=0; i < len; i++) { in = 0; for (j=0; j < lenV; j++) { if (visitados[j] == vizinhos[i]) { in = 1; } } if (!in) { dist = getDist(dists, vizinhos[i]); if (dist == NULL) { //infinity dists[lenD] = newDist(vizinhos[i], alt); dists[lenD]->prev = currDist; dist = dists[lenD]; lenD++; } else if (alt < dist->dist) { dist->dist = alt; dist->prev = currDist; } if (idVerticeDestino == vizinhos[i]) { currDist = dist; achou = 1; } visitados[lenV] = vizinhos[i]; lenV++; LIS_InserirElementoAntes(Q, newInt(vizinhos[i])); } } free(vizinhos); if (achou) { currDist = dist; break; } if(lenV == LIS_NumeroDeElementos(pGrafo->vertices)) { break; } } if (achou) { //printf("\n"); // for(i=0; i < lenD; i++) { // printf("endr: %p, id: %d, dist: %d, prev: %p \n", *(dists+i), dists[i]->id, dists[i]->dist, dists[i]->prev); // } while (currDist) { LIS_InserirElementoAntes(retorno, newInt(currDist->id)); currDist = currDist->prev; } } //Limpando a memória for (i=0; i < lenD; i++) { free(dists[i]); } free(dists); free(visitados); LIS_DestruirLista(Q); *pLista = retorno; return GRA_CondRetOK; }
void inicializa_estrutura(LIS_tppCabeca * Princ) { LIS_tppCabeca * cb1, *cb2, *cb3, *cb4, *cb5, *cb6, *cb7, *cb8, *cb9, *cb10, *cb11, *cb12, *cb13, *cb14, *cb15, *cb16, *cb17; cb1 = LIS_CriaLista(DestruirValor); cb2 = LIS_CriaLista(DestruirValor); cb3 = LIS_CriaLista(DestruirValor); cb4 = LIS_CriaLista(DestruirValor); cb5 = LIS_CriaLista(DestruirValor); cb6 = LIS_CriaLista(DestruirValor); cb7 = LIS_CriaLista(DestruirValor); cb8 = LIS_CriaLista(DestruirValor); cb9 = LIS_CriaLista(DestruirValor); cb10 = LIS_CriaLista(DestruirValor); cb11 = LIS_CriaLista(DestruirValor); cb12 = LIS_CriaLista(DestruirValor); cb13 = LIS_CriaLista(DestruirValor); cb14 = LIS_CriaLista(DestruirValor); cb15 = LIS_CriaLista(DestruirValor); cb16 = LIS_CriaLista(DestruirValor); cb17 = LIS_CriaLista(DestruirValor); LIS_InserirElementoAntes(Princ, cb17); LIS_InserirElementoAntes(Princ, cb16); LIS_InserirElementoAntes(Princ, cb15); LIS_InserirElementoAntes(Princ, cb14); LIS_InserirElementoAntes(Princ, cb13); LIS_InserirElementoAntes(Princ, cb12); LIS_InserirElementoAntes(Princ, cb11); LIS_InserirElementoAntes(Princ, cb10); LIS_InserirElementoAntes(Princ, cb9); LIS_InserirElementoAntes(Princ, cb8); LIS_InserirElementoAntes(Princ, cb7); LIS_InserirElementoAntes(Princ, cb6); LIS_InserirElementoAntes(Princ, cb5); LIS_InserirElementoAntes(Princ, cb4); LIS_InserirElementoAntes(Princ, cb3); LIS_InserirElementoAntes(Princ, cb2); LIS_InserirElementoAntes(Princ, cb1); }/* Fim Função: Inicializar Estrutura*/
TST_tpCondRet TST_EfetuarComando( char * ComandoTeste ) { int inxLista = -1 , numLidos = -1 , CondRetEsp = -1 ; TST_tpCondRet CondRet ; char CharDado ; int ValEsp = -1 ; int i ; int numElem = -1 ; /* Efetuar reset de teste de lista */ if ( strcmp( ComandoTeste , RESET_LISTA_CMD ) == 0 ) { for( i = 0 ; i < DIM_VT_LISTA ; i++ ) { vtListas[ i ] = NULL ; } /* for */ return TST_CondRetOK ; } /* fim ativa: Efetuar reset de teste de lista */ /* Testar CriarLista */ else if ( strcmp( ComandoTeste , CRIAR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , VAZIO ))) { return TST_CondRetParm ; } /* if */ vtListas[ inxLista ] = LIS_CriarLista( ) ; return TST_CompararPonteiroNulo( 1 , vtListas[ inxLista ] , "Erro em ponteiro de nova lista." ) ; } /* fim ativa: Testar CriarLista */ /* Testar Esvaziar lista */ else if ( strcmp( ComandoTeste , ESVAZIAR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO ))) { return TST_CondRetParm ; } /* if */ LIS_EsvaziarLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: Testar Esvaziar lista lista */ /* Testar Destruir lista */ else if ( strcmp( ComandoTeste , DESTRUIR_LISTA_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO ))) { return TST_CondRetParm ; } /* if */ LIS_DestruirLista( vtListas[ inxLista ] ) ; vtListas[ inxLista ] = NULL ; return TST_CondRetOK ; } /* fim ativa: Testar Destruir lista */ /* Testar inserir elemento antes */ else if ( strcmp( ComandoTeste , INS_ELEM_ANTES_CMD ) == 0 ) { numLidos = LER_LerParametros( "ici" , &inxLista , &CharDado , &CondRetEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ CondRet = LIS_InserirElementoAntes( vtListas[ inxLista ] , CharDado ) ; return TST_CompararInt( CondRetEsp , CondRet , "Condicao de retorno errada ao inserir antes." ) ; } /* fim ativa: Testar inserir elemento antes */ /* Testar inserir elemento apos */ else if ( strcmp( ComandoTeste , INS_ELEM_APOS_CMD ) == 0 ) { numLidos = LER_LerParametros( "ici" , &inxLista , &CharDado , &CondRetEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ CondRet = LIS_InserirElementoApos( vtListas[ inxLista ] , CharDado ) ; return TST_CompararInt( CondRetEsp , CondRet , "Condicao de retorno errada ao inserir apos." ) ; } /* fim ativa: Testar inserir elemento apos */ /* Testar excluir simbolo */ else if ( strcmp( ComandoTeste , EXC_ELEM_CMD ) == 0 ) { numLidos = LER_LerParametros( "ii" , &inxLista , &CondRetEsp ) ; if ( ( numLidos != 2 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ return TST_CompararInt( CondRetEsp , LIS_ExcluirElemento( vtListas[ inxLista ] ) , "Condição de retorno errada ao excluir." ) ; } /* fim ativa: Testar excluir simbolo */ /* Testar obter valor do elemento corrente */ else if ( strcmp( ComandoTeste , OBTER_VALOR_CMD ) == 0 ) { numLidos = LER_LerParametros( "ic" , &inxLista , &CharDado ) ; if ( ( numLidos != 2 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ return TST_CompararChar( CharDado, LIS_ObterValor( vtListas[ inxLista ] ), "Valor do elemento corrente está errado." ); } /* fim ativa: Testar obter valor do elemento corrente */ /* Testar ir para o elemento inicial */ else if ( strcmp( ComandoTeste , IR_INICIO_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ LIS_IrInicioLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: Testar ir para o elemento inicial */ /* LIS &Ir para o elemento final */ else if ( strcmp( ComandoTeste , IR_FIM_CMD ) == 0 ) { numLidos = LER_LerParametros( "i" , &inxLista ) ; if ( ( numLidos != 1 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ LIS_IrFinalLista( vtListas[ inxLista ] ) ; return TST_CondRetOK ; } /* fim ativa: LIS &Ir para o elemento final */ /* LIS &Avançar elemento */ else if ( strcmp( ComandoTeste , AVANCAR_ELEM_CMD ) == 0 ) { numLidos = LER_LerParametros( "iii" , &inxLista , &numElem , &CondRetEsp ) ; if ( ( numLidos != 3 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ return TST_CompararInt( CondRetEsp , LIS_AvancarElementoCorrente( vtListas[ inxLista ] , numElem ) , "Condicao de retorno errada ao avancar" ) ; } /* fim ativa: LIS &Avançar elemento */ /* Testar obter numero de elementos */ else if ( strcmp( ComandoTeste , OBTER_NUM_ELEM_CMD ) == 0 ) { numLidos = LER_LerParametros( "ii" , &inxLista , &ValEsp ) ; if ( ( numLidos != 2 ) || ( ! ValidarInxLista( inxLista , NAO_VAZIO )) ) { return TST_CondRetParm ; } /* if */ return TST_CompararInt( ValEsp, LIS_ObterNumElem( vtListas[ inxLista ] ), "Numero de elementos da lista esta errado." ); } /* fim ativa: Testar obter numero de elementos */ return TST_CondRetNaoConhec ; } /* Fim função: TLIS &Testar lista */